SQL使用group by合并重复列

时间:2017-11-09 16:46:56

标签: sql-server tsql

我有一个包含这样的值的表格;

ACC     SALES LY   SALES TY      YEAR
-------------------------------------
B0022      0           15        2017
B0022      22          0         2016
AA000      0           12        2017
AA000      6           0         2016

我希望创建一个将行与同一个ACC合并的视图。所以我正在寻找的是这个;

ACC     SALES LY   SALES TY      YEAR
--------------------------------------
B0022      22          15        EMPTY
AA000       6          12        EMPTY

我查看了GROUP BY条款over(Partition by),我无法让它发挥作用。这只显示了我当前表/视图的一部分。

完整的视图代码在这里:

SELECT DISTINCT TOP (100) PERCENT 
    dbo.[Sales By SKU Units].[Customer Account Number], 
    CASE 
       WHEN [Sales By SKU Units].[Customer Account Number] = 'S0040' THEN 'H Samuel'
       WHEN [Sales By SKU Units].[Customer Account Number] = 'Z0004' THEN 'Ernest Jones'
       WHEN [Sales By SKU Units].[Customer Account Number] = 'N0014' THEN 'Goldsmiths' 
       WHEN [Sales By SKU Units].[Customer Account Number] = 'B0022' THEN 'Beaverbrooks'
       WHEN [Sales By SKU Units].[Customer Account Number] = 'A0097' THEN 'Fraser Hart' 
       WHEN [Sales By SKU Units].[Customer Account Number] = 'H0085' THEN 'F Hinds'
       WHEN [Sales By SKU Units].[Customer Account Number] = 'A0044' THEN 'Amazon' 
       WHEN [Sales By SKU Units].[Customer Account Number] = 'S0482' THEN 'Watch Shop'
    END AS CustomerName, 
    dbo.[Sales By SKU Units].SKU, 
    dbo.RangeLists.Column1 AS Status, 
    CASE 
       WHEN [Sales By SKU Value].[W/H Stock] IS NULL THEN '0' 
       WHEN [Sales By SKU Value].[W/H Stock] = '' THEN '0' 
       WHEN [Sales By SKU Value].[W/H Stock] IS NOT NULL THEN [Sales By SKU Value].[W/H Stock] 
    END AS [Warehouse Stock], 
    CASE 
       WHEN [Sales By SKU Value].[Store Stock] IS NULL THEN '0' 
       WHEN [Sales By SKU Value].[Store Stock] = '' THEN '0' 
       WHEN [Sales By SKU Value].[Store Stock] IS NOT NULL THEN [Sales By SKU Value].[Store Stock] 
    END AS [Store Stock], 
    CASE 
        WHEN [Sales By SKU Value].[On Order] IS NULL THEN '0' 
        WHEN [Sales By SKU Value].[On Order] = '' THEN '0' 
        WHEN [Sales By SKU Value].[On Order] IS NOT NULL THEN [Sales By SKU Value].[On Order] 
    END AS [On Order], 
    CASE 
        WHEN [Sales By SKU Value].[Total Stock] IS NULL THEN '0' 
        WHEN [Sales By SKU Value].[Total Stock] = '' THEN '0' 
        WHEN [Sales By SKU Value].[Total Stock] IS NOT NULL THEN [Sales By SKU Value].[Total Stock] 
    END AS [Total Stock], 
    CASE 
       WHEN [Sales By SKU Value].[No Of Stores] IS NULL THEN '0' 
       WHEN [Sales By SKU Value].[No Of Stores] = '' THEN '0' 
       WHEN [Sales By SKU Value].[No Of Stores] IS NOT NULL THEN [Sales By SKU Value].[No Of Stores] 
    END AS [Number of Stores], 
    CASE 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'jan' THEN [Sales By SKU Units].[Jan] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'feb' THEN [Sales By SKU Units].[Feb] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'mar' THEN [Sales By SKU Units].[March] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'apr' THEN [Sales By SKU Units].[April] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'may' THEN [Sales By SKU Units].[May] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'jun' THEN [Sales By SKU Units].[June] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'jul' THEN [Sales By SKU Units].[July] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'aug' THEN [Sales By SKU Units].[August] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'sep' THEN [Sales By SKU Units].[September] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'oct' THEN [Sales By SKU Units].[October] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'nov' THEN [Sales By SKU Units].[November] 
       WHEN LEFT(datename(month, DATEADD(MM, - 1, getdate())), 3) = 'dec' THEN [Sales By SKU Units].[December] 
       ELSE '0' 
    END AS [Sales Last Month], 
    dbo.RangeLists.pnum AS Model, 
    CASE 
        WHEN [Sales By SKU Units].[Year] = '2016' 
           THEN ISNULL([Sales By SKU Units].[Jan], 0) + ISNULL([Sales By SKU Units].[Feb], 0) + 
                ISNULL([Sales By SKU Units].[March], 0) + ISNULL([Sales By SKU Units].[April], 0) + 
                ISNULL([Sales By SKU Units].[May], 0) + ISNULL([Sales By SKU Units].[June], 0) + 
                ISNULL([Sales By SKU Units].[July], 0) + ISNULL([Sales By SKU Units].[August], 0) + 
                ISNULL([Sales By SKU Units].[September], 0) + ISNULL([Sales By SKU Units].[October], 0) + 
                ISNULL([Sales By SKU Units].[November], 0) + ISNULL([Sales By SKU Units].[December], 0) 
           ELSE '0' 
    END AS [SALES LY], 
    CASE 
       WHEN [Sales By SKU Units].[Year] = '2017' 
          THEN ISNULL([Sales By SKU Units].[Jan], 0) + ISNULL([Sales By SKU Units].[Feb], 0) + 
               ISNULL([Sales By SKU Units].[March], 0) + ISNULL([Sales By SKU Units].[April], 0) + 
               ISNULL([Sales By SKU Units].[May], 0) + ISNULL([Sales By SKU Units].[June], 0) + 
               ISNULL([Sales By SKU Units].[July], 0) + ISNULL([Sales By SKU Units].[August], 0) + 
               ISNULL([Sales By SKU Units].[September], 0) + ISNULL([Sales By SKU Units].[October], 0) + 
               ISNULL([Sales By SKU Units].[November], 0) + ISNULL([Sales By SKU Units].[December], 0) 
          ELSE '0' 
    END AS [SALES TY], 
    dbo.[Sales By SKU Units].Year
FROM
    dbo.[Sales By SKU Units] 
INNER JOIN
    dbo.[Sales By SKU Value] ON dbo.[Sales By SKU Units].Year = dbo.[Sales By SKU Value].Year 
                             AND dbo.[Sales By SKU Units].SKU = dbo.[Sales By SKU Value].SKU 
                             AND dbo.[Sales By SKU Units].[Customer Account Number] = dbo.[Sales By SKU Value].[Customer Account Number] 
INNER JOIN
    dbo.RangeLists ON dbo.[Sales By SKU Value].SKU = dbo.RangeLists.CustProdRef

我知道我需要GROUP BY或OVER(分区依据),但我无法弄清楚如何将它们应用到我当前的查询中。

1 个答案:

答案 0 :(得分:1)

我假设您正在寻找SALES字段的总和,在这种情况下,您需要SUM()进行分组,例如:

select 
    ACC
    , sum(SALES_LY) as SALES_LY
    , sum(SALES_TY) as SALES_TY
    , null as [YEAR]
from 
    ([insert your current query here])
    group by ACC

如果您想要最大值,则只需使用MAX()代替SUM()