SQL Server:Union all&分组条款不起作用

时间:2016-09-05 16:00:11

标签: sql sql-server sql-server-2005

我有一张表格如下: -

CREATE TABLE ITEM_DIM
(
     DOCKET_NO VARCHAR(9), 
     DOC_DT DATETIME,
     GL_CODE VARCHAR(10),
     ITEM_CD VARCHAR(15),
     ITEM_DIMENSION VARCHAR(100),
     LOT_NOS VARCHAR(15),
     QTY1 DECIMAL(14, 6), QTY2 DECIMAL(14, 6), QTY3 DECIMAL(14, 6),
     QTY4 DECIMAL(14, 6), QTY5 DECIMAL(14, 6), QTY6 DECIMAL(14, 6),
     QTY7 DECIMAL(14, 6), QTY8 DECIMAL(14, 6), QTY9 DECIMAL(14, 6),
     QTY10 DECIMAL(14, 6), QTY11 DECIMAL(14, 6), QTY12 DECIMAL(14, 6),
     QTY14 DECIMAL(14, 6), QTY15 DECIMAL(14, 6), QTY16 DECIMAL(14, 6)
)

插入语句以将数据插入表中: -

INSERT INTO ITEM_DIM (DOCKET_NO, DOC_DT, GL_CODE, ITEM_CD, ITEM_DIMENSION, 
                      LOT_NOS, QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8) 
VALUES ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'INNER DIA', 
        'AA8/ZZ16', 40.1, 40, 39.91, 39.92, 39.93, 39.94, 39.95, 39.96),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'INNER DIA 1', 
        'AA8/ZZ16', 37.8, 37.71, 37.72, 37.73, 37.74, 37.75, 37.76, 37.77),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'THICKNESS', 
        'AA8/ZZ16', 0.88, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.77),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'THICKNESS 1', 
        'AA8/ZZ16', 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78)

选择要显示数据的语句: -

SELECT 
    DOCKET_NO, DOC_DT, GL_CODE, PARTY_CD, ITEM_CD, ITEM_DIMENSION, LOT_NOS,
    QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8 
FROM 
    ITEM_DIM

我试图按照附加图像获得结果"期望的结果":

enter image description here

所以我写下面的查询来获得所需的结果:

SELECT 
    DOC_DT, PARTY_CD, ITEM_CD, LOT_NOS, INNER_DIA, THK 
FROM
    (SELECT 
         DOC_DT, GL_CODE, PARTY_CD, ITEM_CD, LOT_NOS, QTY1, INNER_DIA, 0 THK 
     FROM ITEM_DIM
     WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
     UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY2 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY3 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY4 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY5 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY6 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY7 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,QTY8 INNER_DIA,0 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('INNER DIA','INNER DIA 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY1 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY2 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY3 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY4 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY5 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY6 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY7 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')
UNION ALL
SELECT DOC_DT,GL_CODE PARTY_CD,ITEM_CD,LOT_NOS,0 INNER_DIA, QTY8 THK FROM ITEM_DIM
WHERE ITEM_DIMENSION IN ('THICKNESS','THICKNESS 1')) Z 

但是我的查询根据附件图像显示结果" My_Qry_Result"而不是期望的结果。

enter image description here

我也试过加入但没有获得所需的数据。所以请帮助我获得理想的结果 - 谢谢

1 个答案:

答案 0 :(得分:0)

以下是使用UNPIVOT的方法。请注意,我将表格更改为TEMP TABLES进行测试。只需删除表名前面的#。

CREATE TABLE #ITEM_DIM
(
     DOCKET_NO VARCHAR(9), 
     DOC_DT DATETIME,
     GL_CODE VARCHAR(10),
     ITEM_CD VARCHAR(15),
     ITEM_DIMENSION VARCHAR(100),
     LOT_NOS VARCHAR(15),
     QTY1 DECIMAL(14, 6), QTY2 DECIMAL(14, 6), QTY3 DECIMAL(14, 6),
     QTY4 DECIMAL(14, 6), QTY5 DECIMAL(14, 6), QTY6 DECIMAL(14, 6),
     QTY7 DECIMAL(14, 6), QTY8 DECIMAL(14, 6), QTY9 DECIMAL(14, 6),
     QTY10 DECIMAL(14, 6), QTY11 DECIMAL(14, 6), QTY12 DECIMAL(14, 6),
     QTY14 DECIMAL(14, 6), QTY15 DECIMAL(14, 6), QTY16 DECIMAL(14, 6)
)


INSERT INTO #ITEM_DIM (DOCKET_NO, DOC_DT, GL_CODE, ITEM_CD, ITEM_DIMENSION, 
                      LOT_NOS, QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8) 
VALUES ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'INNER DIA', 
        'AA8/ZZ16', 40.1, 40, 39.91, 39.92, 39.93, 39.94, 39.95, 39.96),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'INNER DIA 1', 
        'AA8/ZZ16', 37.8, 37.71, 37.72, 37.73, 37.74, 37.75, 37.76, 37.77),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'THICKNESS', 
        'AA8/ZZ16', 0.88, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.77),
       ('ZKR000816', '2016-08-20', 'PARTY0002', '1285BRF21OP', 'THICKNESS 1', 
        'AA8/ZZ16', 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78)


--First we unpivot the Diameter into a TempTable
IF OBJECT_ID('tempdb..#Diam') IS NOT NULL DROP TABLE #Diam

select DOC_DT, GL_CODE, ITEM_CD, LOT_NOS, DIA, ROW_NUMBER() OVER (ORDER BY DOC_DT, GL_CODE, ITEM_CD) RN
INTO #Diam
FROM
(SELECT DOC_DT, GL_CODE, ITEM_CD, LOT_NOS, QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8, QTY9, QTY10, QTY11, QTY12, QTY14, QTY15, QTY16
FROM #ITEM_DIM
WHERE ITEM_DIMENSION LIKE 'INNER DIA%') as CP
UNPIVOT
(DIA for Diams in (QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8, QTY9, QTY10, QTY11, QTY12, QTY14, QTY15, QTY16)) AS UP

--Next we unpivot the thickness into a TempTable
IF OBJECT_ID('tempdb..#Thick') IS NOT NULL DROP TABLE #Thick

select DOC_DT, GL_CODE, ITEM_CD, LOT_NOS, THK, ROW_NUMBER() OVER (ORDER BY DOC_DT, GL_CODE, ITEM_CD) RN
INTO #Thick
FROM
(SELECT DOC_DT, GL_CODE, ITEM_CD, LOT_NOS, QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8, QTY9, QTY10, QTY11, QTY12, QTY14, QTY15, QTY16
FROM #ITEM_DIM
WHERE ITEM_DIMENSION LIKE 'THICKNESS%') as CP
UNPIVOT
(THK for Thickness in (QTY1, QTY2, QTY3, QTY4, QTY5, QTY6, QTY7, QTY8, QTY9, QTY10, QTY11, QTY12, QTY14, QTY15, QTY16)) AS UP



--Now bring them together, based on the Row Number
SELECT
    d.DOC_DT,
    d.GL_CODE as PARTY_CD,
    d.ITEM_CD,
    d.LOT_NOS,
    d.DIA as INNER_DIA,
    t.THK
FROM #Diam d
INNER JOIN #Thick t on 
    t.RN = d.RN

结果如下。订购他们你喜欢什么

enter image description here