1

在过去的 4 天里,我一直在努力处理 MSSQL Server 查询。无论我尝试什么,我都失败了。如果可能的话,我可以在这里得到帮助。

这是我想从查询中获取的报告:

          Playstation    Laptop    Phone    Macbook    Total 
-------------------------------------------------------------
England    1x 257$       0x 0$     0x 0$     0x 0$    1x 257$
Sweden     0x 0$         1x 433$   1x 424$   0x 0$    2x 857$ 
Russia     2x 472$       0x 0$     0x 0$     0x 0$    2x 472$
Italy      0x 0$         0x 0$     0x 0$     0x 0$    0x 0$
Total      3x 729$       1x 433$   1x 424$   0x 0$    5x 1586$

这些是我的表:

Country桌子:

Id   Name   
----------------------------------------
1     England
2     Sweden
3     Russia
4     Italy

Items桌子:

Id   Name   
----------------------------------------
1     Laptop
2     Phone
3     Playstation
4     Macbook

Pre-Request桌子:

Id   countryId   ItemId      blablabla
----------------------------------------
1        1         3          blablabla 
2        2         1          blablabla 
3        2         2          blablabla 
4        3         3          blablabla 
5        3         3          blablabla 
6        2         3          blablabla 

Offers桌子:

Id   Price         Blablabla
----------------------------------------
18     257$          Blablabla
19     151$          Blablabla
20     424$          Blablabla
21     433$          Blablabla
22     321$          Blablabla

Request桌子:

Id     preReqId     requestStatus     winOfferId
---------------------------------------------
44        1          3                   18
11        2          4                   21
53        3          4                   20
87        4          3                   22
43        5          3                   19
45        6          2                   Null

Blablabla 的列和行无关紧要。只有报告表查询是我必须requestStatus > 2用来获取合法报告,即使没有来自“意大利”的订单或没有订单的“Macbook”,它们无论如何都会在报告中。谢谢

4

1 回答 1

2

我正在插入一个临时表以使查询更具可读性。我在里面写了逻辑。

;WITH CTE AS
(
    SELECT DISTINCT I.NAME ITEMNAME,C.NAME COUNTRYNAME
    ,CAST(REPLACE(TAB.PRICE,'$','')AS INT)PRICE
    ,COUNT(CASE WHEN TAB.PRICE IS NOT NULL THEN I.NAME END) OVER(PARTITION BY C.NAME,I.NAME) CNTITEM    
    FROM [#Pre-Request] PR
    LEFT JOIN #Items I ON PR.ITEMID=I.ID
    LEFT JOIN #COUNTRY C ON PR.COUNTRYID = C.ID
    OUTER APPLY
    (
        SELECT R.preReqId,R.winOfferId,O.PRICE
        FROM #Request R 
        JOIN #Offers O ON R.winOfferId=O.Id
        WHERE PR.ID=R.preReqId
    )TAB
    
    UNION 
    -- Used to select Item name and country that are not in Pre-request table and other tables
    SELECT I.NAME ,C.NAME ,NULL,0
    FROM #Items I
    CROSS JOIN #COUNTRY C
)
,CTE2 AS
(
    -- Find the sum for number of items
    SELECT DISTINCT ISNULL(ITEMNAME,'TOTAL')ITEMNAME,ISNULL(COUNTRYNAME,'TOTAL')COUNTRYNAME,
    SUM(PRICE)PRICE 
    FROM CTE
    GROUP BY ITEMNAME,COUNTRYNAME
    WITH CUBE
)
,CTE3 AS
(
    -- Find the sum of PRICE
    SELECT DISTINCT ISNULL(ITEMNAME,'TOTAL')ITEMNAME,ISNULL(COUNTRYNAME,'TOTAL')COUNTRYNAME--,CNTITEM
    ,SUM(CNTITEM)CNTITEM    
    FROM 
    (
        SELECT DISTINCT ITEMNAME,COUNTRYNAME,CNTITEM
        FROM CTE
    )TAB
    GROUP BY ITEMNAME,COUNTRYNAME
    WITH CUBE
)
SELECT C2.*,C3.CNTITEM,
CAST(C3.CNTITEM AS VARCHAR(20))+'x'+' ' + CAST(C2.PRICE AS VARCHAR(20))+'$' NEWCOL
INTO #NEWTABLE
FROM CTE2 C2
JOIN CTE3 C3 ON C2.COUNTRYNAME=C3.COUNTRYNAME AND C2.ITEMNAME=C3.ITEMNAME

获取动态透视的列

DECLARE @cols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + ITEMNAME + ']', '[' + ITEMNAME + ']')
               FROM (SELECT DISTINCT ITEMNAME FROM #NEWTABLE WHERE ITEMNAME<>'TOTAL') PV 
               ORDER BY ITEMNAME 
-- Since we need Total in last column, we append it at last
SELECT @cols += ',[Total]'

现在旋转查询

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT COUNTRYNAME,' + @cols + ' FROM 
             (
                 SELECT DISTINCT ITEMNAME,COUNTRYNAME,ISNULL(NEWCOL,''0x 0$'')NEWCOL
                 FROM #NEWTABLE
             ) x
             PIVOT 
             (
                 MIN(NEWCOL)
                 FOR ITEMNAME IN (' + @cols + ')
            ) p
            ORDER BY CASE WHEN (COUNTRYNAME=''Total'') THEN 1 ELSE 0 END,COUNTRYNAME' 

EXEC SP_EXECUTESQL @query
于 2015-02-28T18:41:52.777 回答