2017-06-28 10 views
0

私は、以下に示すselectステートメントを使用して次のデータを表示しています。しかし、私は私がSQL Serverでどのように転記できますか?

PO1 PO2 CUSTOMER CUST PN1 CUST PN2 PN1 PN2  DESC 1  DESC 2    QTY1 QTY2 SHIP DATE 
001 002  xxx  111  112  100200 100201 description1 description2   24  25 2017-06-27 

出荷日と、顧客フィールドが

同じになり、次のように表示したい顧客とshipbyの日付に基づいて移調して

PO CUSTOMER CUST PN  PN  DESC   QTY SHIP DATE 
001  xxx  111  100200 description1 24 2017-06-27 
002  xxx  112  100201 description2 25 2017-06-27 

を単一の行を作成したいです

ここに私のSQL文

SELECT 
    [PO #], [CUSTOMER], [CUST PN], [PN], [DESC], [QTY], [SHIP DATE] 
FROM 
    (SELECT 
     [PO #], [CUSTOMER], [CUST PN], [PN], [DESC], [QTY], [SHIP DATE] 
    FROM 
     WORKORDERS S 
    WHERE 
     [SHIP BY] = DATEADD(day, -1, CONVERT(VARCHAR(11), GETDATE(), 106))) AS s 
+1

あなたの出力にはデータが作成されているようです。 PO3とPO4はどこから来たのですか?これらの値はサンプルデータにはありません。そして、出荷日はいつですか?ここから始めましょう。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

3回、ダイナミックピボットを望むように見えます...それは意味がありません – scsimon

+0

@SeanLange私の悪い私はそれを修正します。 – user8066749

答えて

1

あなたのパズルに私の答えはここにあります。私は面白いが、SQL Serverとは、列をaggになることを発見した。スクリプトの最後のコメントを参照してください。また、コードの不自然さを許してください。妻に二度呼ばれて「子供」を手に入れました。 ...

DECLARE @workorders AS TABLE 
(
    [PO #] NVARCHAR(10), 
    [CUSTOMER] NVARCHAR(200), 
    [CUST PN] INT, 
    [PN] INT, 
    [DESC] NVARCHAR(500), 
    [QTY] INT, 
    [SHIP DATE] DATE 
); 

--DECLARE @workorders_W_GroupingID AS TABLE 
--(
-- groupingID INT, 
-- [PO #] NVARCHAR(10), 
-- [CUSTOMER] NVARCHAR(200), 
-- [CUST PN] INT, 
-- [PN] INT, 
-- [DESC] NVARCHAR(500), 
-- [QTY] INT, 
-- [SHIP DATE] DATE 
--); 



INSERT @workorders 
(
    [PO #], 
    CUSTOMER, 
    [CUST PN], 
    PN, 
    [DESC], 
    QTY, 
    [SHIP DATE] 
) 
VALUES 
('001', N'xxx', 111, 100200, N'description1', 24, '2017-06-27'), 
('002', N'xxx', 112, 100200, N'description2', 24, '2017-06-27'), 
('003', N'xxx', 113, 100200, N'description3', 24, '2017-06-28'), 
('004', N'xxx', 114, 100200, N'description4', 24, '2017-06-25'), 
('005', N'aaa', 115, 100200, N'description5', 24, '2017-06-27'), 
('006', N'aaa', 116, 100200, N'description6', 24, '2017-06-28'), 
('007', N'aaa', 117, 100200, N'description7', 24, '2017-06-28'), 
('008', N'ccc', 118, 100200, N'description8', 24, '2017-06-27'), 
('009', N'xxx', 119, 100200, N'description9', 24, '2017-06-27'); 

--INSERT INTO @workorders_W_GroupingID 
SELECT ROW_NUMBER() OVER (PARTITION BY S.CUSTOMER, S.[SHIP DATE] ORDER BY S.[PO #]) AS groupingID, 
     S.* 
INTO #workorders_W_GroupingID 
FROM @workorders S; 

SELECT * FROM #workorders_W_GroupingID;--DISPLAY 

--COUNTER 
DECLARE @CUNTER INT; 
DECLARE @MAX_CUNTER INT; 
SET @MAX_CUNTER = 
(
    SELECT MAX(groupingID) FROM #workorders_W_GroupingID 
); 

DECLARE @query_Select NVARCHAR(MAX), 
     @query_From NVARCHAR(MAX), 
     @query_Into NVARCHAR(MAX), 
     @query_OrderBy NVARCHAR(MAX), 
     @query NVARCHAR(MAX); 

SET @query_Select = N'SELECT '; 
----PO---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT PO FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'PO' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----CUSTOMER---- 
SET @query_Select = @query_Select + (' CASE WHEN temp.groupingID = 1 THEN temp.CUSTOMER END AS' + N' customer, '); 

----CUST PN---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT [CUST PN] FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'[CUST PN' + CONVERT(NVARCHAR(100), @CUNTER) + '],'); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----PN---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT PN FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'PN' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----DESC---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT [DESC] FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'DESC' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----QTY---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT QTY FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'QTY' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----SHIP DATE---- 
SET @query_Select = @query_Select + (' CASE WHEN temp.groupingID = 1 THEN temp.[SHIP DATE] END AS' + N'[ship date] '); 

SET @query_From = N'FROM #workorders_W_GroupingID AS temp '; 
SET @query_Into = N' INTO ##RESULTS_2017_06_28'; 
SET @query_OrderBy = N'Order by BY CUSTOMER, [SHIP DATE] '; 

SET @query = @query_Select + ' ' + @query_Into + ' ' + @query_From; --+ -- ' ' + @query_GroupBy 


execute (@query); 

SELECT * 
FROM ##RESULTS_2017_06_28 
WHERE CUSTOMER IS NOT NULL 
/*ORDER OF COLUMNS IS WRONG SEE https://stackoverflow.com/questions/982286/tsql-string-concat-with-select-and-order-by-does-not-work-with-function-in-order 
    Currently do not see a way to fix. Data appears to be correct. 
*/ 

/* 
SELECT * 
FROM ##RESULTS_2017_06_28 
WHERE CUSTOMER IS NOT NULL 
ORDER BY 
    CASE 
*/ 
DROP TABLE #workorders_W_GroupingID, ##RESULTS_2017_06_28; 

更新されたコードを試してみて、後にクリーンアップされます。さらにいくつかのバグを発見し、(私のバグに起因する)注文を修正しました。上のコードをそのまま残しておきますが、下は魔法です)

--VARIABLES 
DECLARE @workorders AS TABLE 
(
    [PO] NVARCHAR(10), 
    [CUSTOMER] NVARCHAR(200), 
    [CUST PN] INT, 
    [PN] INT, 
    [DESC] NVARCHAR(500), 
    [QTY] INT, 
    [SHIP DATE] DATE 
); 

--COUNTER 
DECLARE @CUNTER INT; 
DECLARE @MAX_CUNTER INT; 


--CREATE TEST DATA 
INSERT @workorders 
(
    [PO], 
    CUSTOMER, 
    [CUST PN], 
    PN, 
    [DESC], 
    QTY, 
    [SHIP DATE] 
) 
VALUES 
('001', N'xxx', 111, 100200, N'description1', 24, '2017-06-27'), 
('002', N'xxx', 112, 100200, N'description2', 24, '2017-06-27'), 
('003', N'xxx', 113, 100200, N'description3', 24, '2017-06-28'), 
('004', N'xxx', 114, 100200, N'description4', 24, '2017-06-25'), 
('005', N'aaa', 115, 100200, N'description5', 24, '2017-06-27'), 
('006', N'aaa', 116, 100200, N'description6', 24, '2017-06-28'), 
('007', N'aaa', 117, 100200, N'description7', 24, '2017-06-28'), 
('008', N'ccc', 118, 100200, N'description8', 24, '2017-06-27'), 
('009', N'xxx', 119, 100200, N'description9', 24, '2017-06-27'); 

--INSERT INTO @workorders_W_GroupingID 
SELECT ROW_NUMBER() OVER (PARTITION BY S.CUSTOMER, S.[SHIP DATE] ORDER BY S.[PO]) AS groupingID, 
     S.* 
INTO #workorders_W_GroupingID 
FROM @workorders S; 

SELECT * FROM #workorders_W_GroupingID;--DISPLAY 

--INITIALIZE COUNTER VARIABLES 
SET @MAX_CUNTER = 
(
    SELECT MAX(groupingID) + 1 FROM #workorders_W_GroupingID 
); 
SET @CUNTER = 1; 

DECLARE @query_Select NVARCHAR(MAX), 
     @query_From NVARCHAR(MAX), 
     @query_Into NVARCHAR(MAX), 
     @query_OrderBy NVARCHAR(MAX), 
     @query NVARCHAR(MAX); 

SET @query_Select = N'SELECT '; 
----PO---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT PO FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'PO' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----CUSTOMER---- 
SET @query_Select = @query_Select + (' CASE WHEN temp.groupingID = 1 THEN temp.CUSTOMER END AS' + N' customer, '); 

----CUST PN---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT [CUST PN] FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'[CUST PN' + CONVERT(NVARCHAR(100), @CUNTER) + '],'); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----PN---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT PN FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'PN' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----DESC---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT [DESC] FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'DESC' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----QTY---- 
WHILE (@CUNTER < @MAX_CUNTER) 
BEGIN 
    --ADD COLUMN 
    SET @query_Select 
     = @query_Select 
      + (' CASE WHEN temp.groupingID = 1 THEN (SELECT QTY FROM #workorders_W_GroupingID WHERE customer = temp.customer AND [ship date] = temp.[ship date] AND GroupingID = ' 
      + CONVERT(NVARCHAR(100), @CUNTER) + ') END AS ' + N'QTY' + CONVERT(NVARCHAR(100), @CUNTER) + ','); 

    --INCREASE CUNTER 
    SET @CUNTER = @CUNTER + 1; 
END; 
--RESET CUNTER 
SET @CUNTER = 1; 

----SHIP DATE---- 
SET @query_Select = @query_Select + (' CASE WHEN temp.groupingID = 1 THEN temp.[SHIP DATE] END AS' + N'[ship date] '); 

SET @query_From = N'FROM #workorders_W_GroupingID AS temp '; 
SET @query_Into = N' INTO ##RESULTS_2017_06_28'; 
SET @query_OrderBy = N'Order by BY CUSTOMER, [SHIP DATE] '; 

SET @query = @query_Select + ' ' + @query_Into + ' ' + @query_From; --+ -- ' ' + @query_GroupBy 


execute (@query); 

SELECT * 
FROM ##RESULTS_2017_06_28 
WHERE CUSTOMER IS NOT NULL 
/*ORDER OF COLUMNS IS WRONG SEE https://stackoverflow.com/questions/982286/tsql-string-concat-with-select-and-order-by-does-not-work-with-function-in-order 
    Currently do not see a way to fix. Data appears to be correct. 
*/ 

/* 
SELECT * 
FROM ##RESULTS_2017_06_28 
WHERE CUSTOMER IS NOT NULL 
ORDER BY 
    CASE 
*/ 
DROP TABLE #workorders_W_GroupingID, ##RESULTS_2017_06_28; 

/*Results 
PO1 PO2 PO3 customer CUST PN1 CUST PN2 CUST PN3 PN1 PN2 PN3 DESC1 DESC2 DESC3 QTY1 QTY2 QTY3 ship date 
005 NULL NULL aaa 115 NULL NULL 100200 NULL NULL description5 NULL NULL 24 NULL NULL 2017-06-27 
006 007 NULL aaa 116 117 NULL 100200 100200 NULL description6 description7 NULL 24 24 NULL 2017-06-28 
008 NULL NULL ccc 118 NULL NULL 100200 NULL NULL description8 NULL NULL 24 NULL NULL 2017-06-27 
004 NULL NULL xxx 114 NULL NULL 100200 NULL NULL description4 NULL NULL 24 NULL NULL 2017-06-25 
001 002 009 xxx 111 112 119 100200 100200 100200 description1 description2 description9 24 24 24 2017-06-27 
003 NULL NULL xxx 113 NULL NULL 100200 NULL NULL description3 NULL NULL 24 NULL NULL 2017-06-28 
*/ 
+0

ありがとうございましたChris !!値を別のテーブルに挿入する方法 – user8066749

+0

レコードは## Results_2017_06_28テーブルにあります。 'insert into'を使うことはできますが、実行時に列が何であるか分からないので、 '*'を使う必要があります。私は、少なくとも正しい順序でそれらを得る方法を見つけ出そうとしていましたが、今は不可能であることが証明されています。これはアプリケーションによって使用される予定ですか? – chris

+0

ありがとう、クリスワーキンググレート。 'ship date' = getdate()?' – user8066749

関連する問題