【问题标题】:In sql server, how do i group rows that have same values in a select query?在sql server中,如何对选择查询中具有相同值的行进行分组?
【发布时间】:2018-04-26 14:20:06
【问题描述】:

我有以下 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

生成以下输出:

+---------------+------------+----------------+---- ------+-------+--------+--------+------+ |包裹 |订购 |项目 |别名 |颜色 |尺寸 |价格 |已发送 | +---------------+------------+----------------+---- ------+-------+--------+--------+------+ | DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 |红色 |中 | 2.00 | 1 | | DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 |红色 |中 | 1 | 1 | | DPV0010260188 | DHRU124_1 | 00021331918437 | 9782189D | | | 4 | 1 | +---------------+------------+----------------+---- ------+-------+--------+--------+------+

如您所见,除了发送字段外,其余数据相同。它不必一直相同。别名基本上是包内的项目。所以我想根据别名值将行分组在一起,并总结它们的价格和发送值。这样就可以将其读取为订单以及相同商品的数量及其总价。 随附的屏幕截图有表格值供参考

预期输出:

+---------------+------------+----------------+---- ------+-------+--------+--------+------+ |包裹 |订购 |项目 |别名 |颜色 |尺寸 |价格 |已发送 | +---------------+------------+----------------+---- ------+-------+--------+--------+------+ | DPV0010260188 | DHRU124_1 | 00717851968853 | 9E9D2256 |红色 |中 | 3.00 | 2 | | DPV0010260188 | DHRU124_1 | 00021331918437 | 9782189D | | | 4 | 1 | +---------------+------------+----------------+---- ------+-------+--------+--------+------+

【问题讨论】:

标签: sql sql-server join


【解决方案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

【讨论】:

  • @AswinFrancis 有任何问题吗?这有帮助吗?
  • 非常感谢。我完全忘记了这种方法。我能够使用您的代码并根据需要让它工作,现在它工作正常。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 2023-01-06
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
相关资源
最近更新 更多