簡體   English   中英

在SQL Server中,如何在選擇查詢中對具有相同值的行進行分組?

[英]In sql server, how do i group rows that have same values in a select query?

我有以下SQL查詢:

SELECT pd.trackingno                                                AS [Parcel],
         Trim(o.orderno) + '_'
         + CONVERT(NVARCHAR, pd.groupnum )                            AS [Order],
         Isnull(vi.[gtin], '''')                                      AS [Item],
         Isnull(i.skuno, '')                                          AS [Article],
         Isnull(ix_clr.skuno, '')                                     AS [Color],
         Isnull(ix_sze.skuno, '')                                     AS [Size],
         Isnull(i.[description], '')                                  AS [Description],
         CONVERT(DECIMAL(8, 2), oi.unitprice * oi.quantity)           AS [Price],
         CONVERT(INT, oi.quantity)                                    AS [Sent]
  FROM   (SELECT orderno,
                 'S' Type,
                 groupnum
          FROM   shipments
          WHERE  shipbatch BETWEEN '2018-04-26 00:00:00' AND '2018-04-27 00:00:00') ords
         INNER JOIN orders AS o
                 ON o.orderno = ords.orderno
         INNER JOIN packageTable AS pd
                 ON ords.orderno = pd.orderno
                    AND ords.groupnum = pd.groupnum
         INNER JOIN oiTable oi
                 ON oi.orderno = ords.orderno
                    AND Isnull(ords.groupnum, 0) = Isnull(oi.groupnum, 0)
         INNER JOIN detailsTable AS od
                 ON ords.orderno = od.orderno
                    AND oi.linenum = od.linenum
         INNER JOIN itemsTable i
                 ON od.itemid = i.itemid
         left JOIN itemXref ix_clr
                 ON i.itemid = ix_clr.itemid
                    AND ix_clr.skutype = 'CLR'
         left JOIN itemXref ix_sze
                 ON i.itemid = ix_sze.itemid
                    AND ix_sze.skutype = 'SZE'
         INNER JOIN vItemTables vi
                 ON vi.itemid = od.itemid
  GROUP  BY pd.trackingno,
            o.orderno,
            pd.groupnum,
            vi.gtin,
            i.skuno,
            ix_clr.skuno,
            ix_sze.skuno,
            i.description,
            o.tax,
            oi.unitprice,
            oi.quantity

生成以下輸出:

+---------------+-----------+----------------+----------+-------+--------+-------+------+
|    Parcel     |   Order   |      Item      |  Alias   | Color |  Size  | Price | Sent |
+---------------+-----------+----------------+----------+-------+--------+-------+------+
| DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 | Red   | MEDIUM |  2.00 |    1 |
| DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 | Red   | MEDIUM |     1 |    1 |
| DPV0010260188 | DHRU124_1 | 00021331918437 | 9782189D |       |        |     4 |    1 |
+---------------+-----------+----------------+----------+-------+--------+-------+------+

如您所見,除了send字段外,其余數據相同。 不需要一直都一樣。別名基本上是包裝內的物品。 因此,我想根據別名值將行分組在一起,並對它們的價格和發送的值進行匯總。 這樣就可以將其解讀為訂單,相同商品的數量及其總價。 隨附的屏幕截圖具有表值供參考 表值

預期產量:

+---------------+-----------+----------------+----------+-------+--------+-------+------+
|    Parcel     |   Order   |      Item      |  Alias   | Color |  Size  | Price | Sent |
+---------------+-----------+----------------+----------+-------+--------+-------+------+
| DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 | Red   | MEDIUM |  3.00 |    2 |
| DPV0010260188 | DHRU124_1 | 00021331918437 | 9782189D |       |        |     4 |    1 |
+---------------+-----------+----------------+----------+-------+--------+-------+------+

您可以將其應用於當前查詢,我很可能會使用變量表來存儲查詢的結果,然后像這樣查詢變量表...

declare @example table (
    ExampleID int identity(1, 1) not null primary key clustered
,   Parcel  nvarchar(255) not null
,   [Order] nvarchar(255) not null
,   Item    nvarchar(255) not null
,   Alias   nvarchar(255) not null
,   Color   nvarchar(255) null
,   Size    nvarchar(255) null
,   Price   money not null
,   [Sent]  int not null
);

insert into @example (Parcel, [Order], Item, Alias, Color, Size, Price, [Sent])

select 'DPV0010260188' , 'DHRU124_1' , '00717851968853' , '9E9D2256' , 'Red'   , 'MEDIUM' ,  2.00 ,    1 union all
select 'DPV0010260188' , 'DHRU124_1' , '00717851968853' , '9E9D2256' , 'Red'   , 'MEDIUM' ,     1 ,    1 union all
select 'DPV0010260188' , 'DHRU124_1' , '00021331918437' , '9782189D' , null    , null     ,     4 ,    1;

select distinct Parcel
     , [Order]
     , Item
     , Alias
     , Color
     , Size
     , sum(Price) Price
     , sum([Sent]) [Sent]
  from @example
  group by Parcel, [Order], Item, Alias, Color, Size

輸出:

Parcel          Order       Item            Alias       Color   Size    Price   Sent
DPV0010260188   DHRU124_1   00021331918437  9782189D    NULL    NULL    4.00    1
DPV0010260188   DHRU124_1   00717851968853  9E9D2256    Red     MEDIUM  3.00    2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM