0
クエリを使用して値を挿入できません。 データが正しく取得され、作成されます。私は「テーブルパーツに挿入する」問題のみを持っています。他のテーブルからいくつかの値をフェッチするために使用されたクエリを使用してテーブルに挿入
私は、関数と結合のセットからデータを取り出して、結果をテーブルTemp_ParentG_Sales_Totalに挿入しようとしています。
データが正しく取得されているうちに、部分的に挿入すると問題が発生します。続き は、私が使用していますクエリです:
insert into MDM_STAT.dbo.Temp_ParentG_Sales_Totals (PARENT_DESCRIPTION, Total_Sales, Ranking)
select ABALPH, TOTAL, RANKING
from( --- the query from this part to the bottom is working correctly.
USE MDM_STAT
DECLARE @RUNMONTH INT;
DECLARE @RUNYEAR INT;
DECLARE @PERIOD INT;
DECLARE @FISCALRUNYEAR INT;
DECLARE @FISCALRUNYEAR_BEGIN INT;
DECLARE @RANKING_TYPE VARCHAR(100);
DECLARE @RANKING_USE VARCHAR(100);
DECLARE @AUPPER DECIMAL(12,2);
DECLARE @ALOWER DECIMAL(12,2);
DECLARE @BUPPER DECIMAL(12,2);
DECLARE @BLOWER DECIMAL(12,2);
DECLARE @CUPPER DECIMAL(12,2);
DECLARE @CLOWER DECIMAL(12,2);
DECLARE @DUPPER DECIMAL(12,2);
DECLARE @DLOWER DECIMAL(12,2);
SET @RANKING_TYPE = 'CUSTOMER SALES';
SET @RANKING_USE='CUSTOMER SALES RANKING 8_4_2016';
SET @ALOWER =(SELECT LOWER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='A'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @BUPPER =(SELECT UPPER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='B'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @BLOWER =(SELECT LOWER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='B'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @CUPPER =(SELECT UPPER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='C'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @CLOWER =(SELECT LOWER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='C'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @DUPPER =(SELECT UPPER_LIMIT FROM dbo.XREF_ABC_RANKING_RULES WHERE RANKS='D'AND RANKING_TYPE= @RANKING_TYPE AND RANKING_USE = @RANKING_USE);
SET @RUNMONTH=MONTH(GETDATE());
SET @RUNYEAR=YEAR(GETDATE());
SET @PERIOD=
CASE
WHEN @RUNMONTH>3 THEN (@RUNMONTH-3)
ELSE [email protected]
END
;
SET @FISCALRUNYEAR=
CASE
WHEN @RUNMONTH>3 THEN @RUNYEAR
ELSE @RUNYEAR-1
END
;
SET @FISCALRUNYEAR_BEGIN=
CASE
WHEN @PERIOD=12 THEN @FISCALRUNYEAR
ELSE @FISCALRUNYEAR-1
END
;
SELECT F.ABALPH,F.TOTAL, RANKING=
CASE
WHEN F.TOTAL >@ALOWER THEN 'A'
WHEN F.TOTAL >@BLOWER AND F.TOTAL<[email protected] THEN 'B'
WHEN F.TOTAL >@CLOWER AND F.TOTAL<[email protected] THEN 'C'
ELSE 'D'
END
FROM
(
select AB.ABALPH, C.Total from JDE_TEST.TESTDTA.F0101 AB
right join
(
select Sum(B.TOTAL) as Total,G.MAPA8 from JDE_TEST.TESTDTA.F0150 G
left join
(
--DETERMINING 12 MONTHS SALE PER CUSTOMER
SELECT A.CUSTOMER, SUM(A.SALES) AS TOTAL FROM
(
SELECT S.CUSTOMER, S.SALESYEAR, S.SALES, S.PERIOD FROM
(
SELECT DISTINCT CUSTOMER,SALESYEAR,SUM(CAST(Dollar01 AS DECIMAL(11,2))) AS SALES, '1' AS PERIOD
FROM sds.SMarginText GROUP BY CUSTOMER,SALESYEAR
UNION
SELECT DISTINCT CUSTOMER,SALESYEAR,SUM(CAST(Dollar02 AS DECIMAL(11,2))) AS SALES, '2' AS PERIOD
FROM sds.SMarginText GROUP BY CUSTOMER,SALESYEAR
) AS S
WHERE [email protected]
AND S.PERIOD<[email protected]
UNION
SELECT T.CUSTOMER, T.SALESYEAR, T.SALES, T.PERIOD FROM
(
SELECT DISTINCT CUSTOMER,SALESYEAR,SUM(CAST(Dollar01 AS DECIMAL(11,2))) AS SALES, '1' AS PERIOD
FROM sds.SMarginText GROUP BY CUSTOMER,SALESYEAR
UNION
SELECT DISTINCT CUSTOMER,SALESYEAR,SUM(CAST(Dollar02 AS DECIMAL(11,2))) AS SALES, '2' AS PERIOD
FROM sds.SMarginText GROUP BY CUSTOMER,SALESYEAR
) AS T
WHERE [email protected]_BEGIN
AND T.PERIOD>@PERIOD
)
AS A
GROUP BY A.CUSTOMER
) AS B
on cast(B.Customer as int) = G.MAAN8
where B.Customer <> 'Unspec'
Group By G.MAPA8
) As C
on C.MAPA8 = AB.ABAN8
) AS F
order by F.Total ASC)
エラーが表示されますか?または間違った結果ですか?または? –
私はなぜスクリプトの上に 'INSERT'を書く必要があるのか分かりません。一番上の 'SELECT'文のすぐ上に' INSERT'を書けないのはなぜですか? – techspider
ここに詳細を掲載する必要があります。投稿されたとおり、何が正しく機能していないのか、また何をしたいのかについての説明がないテキストの壁だけです。ここから始めましょう。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –