GROUP BY在一个记录中

时间:2016-10-26 18:10:08

标签: sql sql-server group-by union

我正在使用SQL Server,我需要查询帮助。

这就是情景。

我有两张表ForecastOrders

有些字段在表格和其他字段上都不匹配。

我可以在SELECT上拥有所有这些内容并不重要。

但是,只有2个字段应始终匹配。 IDSKU

起初我尝试使用LEFT JOIN,但这不起作用,因为我需要一列IDSKU

然后我尝试UNION两个表并按他们分组。

以下是CREATEINSERT的代码。

CREATE TABLE [dbo].[Tbl_Forecast_Test](
[ID] [varchar](100) NULL,
[CUST_ID] [varchar](40) NULL,
[MFG_PART_NUMBER] [varchar](40) NULL,
[SEGMENT] [varchar](40) NULL,
[WH_NUMBER] [varchar](40) NULL,
[RTM] [varchar](40) NULL,
[FORECAST] [int] NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[Tbl_Orders_Test](
[ID] [varchar](100) NULL,
[END_CUST_ID] [varchar](40) NULL,
[PROD_ID] [varchar](40) NULL,
[PIPELINE] [varchar](40) NULL,
[WAREHOUSE] [varchar](40) NULL,
[RTM_GROUP] [varchar](40) NULL,
[ORDERS] [int] NULL
) ON [PRIMARY]

INSERT INTO [BOSSTest].[dbo].[Tbl_Forecast_Test]
([ID],[CUST_ID],[MFG_PART_NUMBER],[SEGMENT],[WH_NUMBER],[RTM],[FORECAST])
VALUES
('US8098629','','W0P88EP','Comm','471','Direct','10')

INSERT INTO [BOSSTest].[dbo].[Tbl_Orders_Test]
([ID],[END_CUST_ID],[PROD_ID],[PIPELINE],[WAREHOUSE],[RTM_GROUP],[ORDERS])
SELECT 'US8098629','W17445','W0P88EP','Ent Group','1','Direct','5'
UNION ALL
SELECT 'US8098629','W17445','V9G85EP','Ent Group','460','Direct','12'
UNION ALL
SELECT 'US8098629','W17445','V9G85EP','Ent Group','460','Direct','29'

这是我得到的结果。

 SELECT  

 [ID]
,[CUST_ID]
,[END_CUST_ID]
,[SKU]
,[SEGMENT]
,[PIPELINE]
,[WH_NUMBER]
,[WAREHOUSE]
,[RTM] 
,[RTM_GROUP]
,SUM ([FORECAST]) AS [FORECAST]
,SUM([ORDERS]) AS [ORDER_LOAD]

FROM ( 

     SELECT  

     [ID] AS [ID]
    ,[CUST_ID]
    ,'' AS [END_CUST_ID]
    ,[MFG_PART_NUMBER] AS [SKU]
    ,[SEGMENT]
    ,'' AS [PIPELINE]
    ,[WH_NUMBER]
    ,'' AS [WAREHOUSE]
    ,[RTM] 
    ,'' AS [RTM_GROUP]
    ,SUM ([FORECAST]) AS [FORECAST]
    ,0 AS [ORDERS]

    FROM [BOSSTest].[dbo].[Tbl_Forecast_Test] 

    GROUP BY
     [ID]
    ,[CUST_ID]
    ,[MFG_PART_NUMBER]
    ,[SEGMENT]
    ,[WH_NUMBER]
    ,[RTM] 

    UNION ALL

    SELECT 

    [ID] AS [ID]
    ,'' AS [CUST_ID]
    ,[END_CUST_ID]
    ,[PROD_ID] AS [SKU]
    ,'' AS [SEGMENT]
    ,[PIPELINE]
    ,'' AS [WH_NUMBER]
    ,[WAREHOUSE]
    ,'' AS [RTM]
    ,[RTM_GROUP]
    ,0 AS [FORECAST]
    ,SUM([ORDERS]) AS [ORDERS]

    FROM [BOSSTest].[dbo].[Tbl_Orders_Test] 

    GROUP BY
     [ID]
    ,[END_CUST_ID]
    ,[PROD_ID] 
    ,[PIPELINE]
    ,[WAREHOUSE]
    ,[RTM_GROUP]


) AS TEST

GROUP BY
 [ID]
,[CUST_ID]
,[END_CUST_ID]
,[SKU]
,[SEGMENT]
,[PIPELINE]
,[WH_NUMBER]
,[WAREHOUSE]
,[RTM] 
,[RTM_GROUP]

这是执行查询时的结果。

+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+
|    ID   |CUST_ID|END_CUST_ID|SKU    |SEGMENT|PIPELINE |WH_NUMBER|WAREHOUSE| RTM  |RTM_GROUP|FORECAST|ORDER_LOAD|
+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+
|US8098629|       |           |W0P88EP| Comm  |         |   471   |         |Direct|         |      10|         0|
|US8098629|       | W17445    |V9G85EP|       |Ent Group|         |      460|      |   Direct|       0|        41|
|US8098629|       | W17445    |W0P88EP|       |Ent Group|         |        1|      |   Direct|       0|         5|
+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+

但这就是我需要的。我想在表格中保留不同的SKU。

+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+
|    ID   |CUST_ID|END_CUST_ID|SKU    |SEGMENT|PIPELINE |WH_NUMBER|WAREHOUSE| RTM  |RTM_GROUP|FORECAST|ORDER_LOAD|
+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+
|US8098629|       | W17445    |W0P88EP| Comm  |Ent Group|   471   |        1|Direct|   Direct|      10|         5|
|US8098629|       | W17445    |V9G85EP|       |Ent Group|         |      460|      |   Direct|       0|        41|
+---------+-------+-----------+-------+-------+---------+---------+---------+------+---------+--------+----------+

有关如何将ID = US8098629SKU = W0P88EP分组到一个记录中的任何建议。

提前致谢, 路易斯

4 个答案:

答案 0 :(得分:2)

您声明无法使用左连接,因为ID和SKU都需要一个列。

考虑到这一点,您仍然可以使用左连接。执行连接后,您不必从两个表中选择每个列。根据您的要求包含或省略列。

SELECT ot.ID, ft.CUST_ID, ot.END_CUST_ID, ot.PROD_ID AS SKU, ft.SEGMENT,
       ot.PIPELINE, ft.WH_NUMBER, ot.WAREHOUSE, ft.RTM, ot.RTM_GROUP,
       SUM(ft.FORECAST) AS FORECAST, SUM(ot.ORDERS) AS ORDERS
FROM Tbl_Orders_Test ot
LEFT JOIN Tbl_Forecast_Test ft ON ft.ID = ot.ID AND ft.MFG_PART_NUMBER = ot.PROD_ID
GROUP BY ot.ID, ft.CUST_ID, ot.END_CUST_ID, ot.PROD_ID, ft.SEGMENT,
         ot.PIPELINE, ft.WH_NUMBER, ot.WAREHOUSE, ft.RTM, ot.RTM_GROUP

答案 1 :(得分:0)

您可以使用以下解决方案,

       SELECT   [ID],[CUST_ID],max([END_CUST_ID]),[SKU],max([SEGMENT]),max([PIPELINE]),max([WH_NUMBER]),max([WAREHOUSE]),max([RTM]) ,max([RTM_GROUP]),SUM ([FORECAST]) AS [FORECAST],SUM([ORDERS]) AS [ORDER_LOAD]

 FROM ( 
 SELECT  

 [ID] AS [ID]    ,[CUST_ID]    ,'' AS [END_CUST_ID]    ,[MFG_PART_NUMBER] AS [SKU]    ,[SEGMENT]    ,'' AS [PIPELINE]    ,[WH_NUMBER]    ,'' AS [WAREHOUSE]
,[RTM]     ,'' AS [RTM_GROUP]    ,SUM ([FORECAST]) AS [FORECAST]    ,0 AS [ORDERS]
FROM [##Tbl_Forecast_Test] 

GROUP BY      [ID]   ,[CUST_ID]    ,[MFG_PART_NUMBER]    ,[SEGMENT]    ,[WH_NUMBER]    ,[RTM] 
UNION ALL

SELECT      [ID] AS [ID]    ,'' AS [CUST_ID]    ,[END_CUST_ID]    ,[PROD_ID] AS [SKU]    ,'' AS [SEGMENT]    ,[PIPELINE]    ,'' AS [WH_NUMBER]    ,[WAREHOUSE]
,'' AS [RTM]    ,[RTM_GROUP]    ,0 AS [FORECAST]    ,SUM([ORDERS]) AS [ORDERS]
    FROM [##Tbl_Orders_Test] 

   GROUP BY
 [ID]    ,[END_CUST_ID]    ,[PROD_ID]     ,[PIPELINE]    ,[WAREHOUSE]    ,[RTM_GROUP]) AS TEST
  GROUP BY
  [ID],[CUST_ID],[SKU]    

答案 2 :(得分:0)

SELECT #Tbl_Orders_Test。[ID],ISNULL([CUST_ID],'')[CUST_ID],[END_CUST_ID],PROD_ID] [SKU],ISNULL([SEGMENT],'')[SEGMENT],[PIPELINE] ,ISNULL([WH_NUMBER],'')[WH_NUMBER],[WAREHOUSE],ISNULL([RTM],'')[RTM],[RTM_GROUP],ISNULL(SUM([FORECAST]),0)AS [预测] ,SUM([ORDERS])AS [ORDER_LOAD] FROM #Tbl_Orders_Test LEFT OUTER JOIN #Tbl_Forecast_Test ON #Tbl_Forecast_Test。[ID] = #Tbl_Orders_Test。[ID] AND #Tbl_Forecast_Test [MFG_PART_NUMBER] = #Tbl_Orders_Test。[PROD_ID] GROUP BY #Tbl_Orders_Test 。[ID],[CUST_ID],[END_CUST_ID],[PROD_ID],[SEGMENT],[PIPELINE],[WH_NUMBER],[WAREHOUSE],[RTM],[RTM_GROUP] ORDER BY [FORECAST] DESC

答案 3 :(得分:-1)

乍一看,您似乎可以停止按" END_CUST_ID"进行分组,而是拉出第一个。

SELECT  
     [ID]
    ,[CUST_ID]
    ,Max([END_CUST_ID]) as [END_CUSTID]
    ,[SKU]
    ,[SEGMENT]
    ,[PIPELINE]
    ,[WH_NUMBER]
    ,[WAREHOUSE]
    ,[RTM] 
    ,[RTM_GROUP]
    ,SUM ([FORECAST]) AS [FORECAST]
    ,SUM([ORDERS]) AS [ORDER_LOAD]

    FROM ( 

         SELECT  

         [ID] AS [ID]
        ,[CUST_ID]
        ,'' AS [END_CUST_ID]
        ,[MFG_PART_NUMBER] AS [SKU]
        ,[SEGMENT]
        ,'' AS [PIPELINE]
        ,[WH_NUMBER]
        ,'' AS [WAREHOUSE]
        ,[RTM] 
        ,'' AS [RTM_GROUP]
        ,SUM ([FORECAST]) AS [FORECAST]
        ,0 AS [ORDERS]

        FROM [BOSSTest].[dbo].[Tbl_Forecast_Test] 

        GROUP BY
         [ID]
        ,[CUST_ID]
        ,[MFG_PART_NUMBER]
        ,[SEGMENT]
        ,[WH_NUMBER]
        ,[RTM] 

        UNION ALL

        SELECT 

        [ID] AS [ID]
        ,'' AS [CUST_ID]
        ,[END_CUST_ID]
        ,[PROD_ID] AS [SKU]
        ,'' AS [SEGMENT]
        ,[PIPELINE]
        ,'' AS [WH_NUMBER]
        ,[WAREHOUSE]
        ,'' AS [RTM]
        ,[RTM_GROUP]
        ,0 AS [FORECAST]
        ,SUM([ORDERS]) AS [ORDERS]

        FROM [BOSSTest].[dbo].[Tbl_Orders_Test] 

        GROUP BY
         [ID]
        ,[END_CUST_ID]
        ,[PROD_ID] 
        ,[PIPELINE]
        ,[WAREHOUSE]
        ,[RTM_GROUP]

    ) AS TEST

    GROUP BY
     [ID]
    ,[CUST_ID]
    ,[SKU]
    ,[SEGMENT]
    ,[PIPELINE]
    ,[WH_NUMBER]
    ,[WAREHOUSE]
    ,[RTM] 
    ,[RTM_GROUP]