如何通过mssql中的订单号将多个订单组合成一个订单

时间:2015-05-19 20:28:16

标签: sql-server tsql

我试图检索已售出我正在寻找的特定产品的所有订单。例如,某些订单可能只有一个商品,而某些订单可能包含多个商品。我想将订单中的所有项目合并为一行。这是我的脚本,它返回了我正在寻找的所有产品:

select 
distinct a.SOPNUMBE as 'Order No.', 
(case when ITMCLSCD like '%CLK' then COUNT(*) else 0 End) as 'Clicks Counts',
(case when ITMCLSCD like '%CLK' then sum(XTNDPRCE) else 0 End) as 'Clicks Total',
(case when ITMCLSCD like '%105' then COUNT(*) else 0 End) as 'Smart Sensor Counts',
(case when ITMCLSCD like '%105' then sum(XTNDPRCE) else 0 End) as 'Smart Sensors Total',
(case when ITMCLSCD like '%125' then COUNT(*) else 0 End) as 'HD Counts',
(case when ITMCLSCD like '%125' then sum(XTNDPRCE) else 0 End) as 'HD Total',
(case when ITMCLSCD like '%sen' then COUNT(*) else 0 End)  as 'Sensor Counts',
(case when ITMCLSCD like '%sen' then sum(XTNDPRCE) else 0 End) as 'Sensors Total',
(case when ITMCLSCD like '%Mat' then COUNT(*) else 0 End) as 'Matrix Counts',
(case when ITMCLSCD like '%Mat' then sum(XTNDPRCE) else 0 End) as 'Matrix Total',
(case when ITMCLSCD like '%200' then COUNT(*) else 0 End) as 'Advance Counts',
(case when ITMCLSCD like '%200' then sum(XTNDPRCE) else 0 End) as 'Advance Total'

from SOP30300 a join IV00101 b

on a.ITEMNMBR = b.ITEMNMBR

join sop30200 c

on a.SOPNUMBE = c.SOPNUMBE

where (a.SOPNUMBE like 'INV%')


group by a.SOPNUMBE, ITMCLSCD

order by SOPNUMBE

结果如下所示:

Order No.   Clicks Counts   Clicks Total    Smart Sensor Counts Smart Sensors Total HD Counts   HD Total    Sensor Counts   Sensors Total   Matrix Counts   Matrix Total    Advance Counts  Advance Total
001 2   537 0   0   0   0   0   0   0   0   0   0
001 0   0   0   0   0   0   0   0   0   0   0   0
002 1   277.46  0   0   0   0   0   0   0   0   0   0
002 0   0   0   0   0   0   1   80  0   0   0   0
003 0   0   0   0   0   0   1   450 0   0   0   0
003 0   0   0   0   0   0   0   0   0   0   1   5200
003 0   0   0   0   0   0   0   0   0   0   0   0
003 6   1483.09 0   0   0   0   0   0   0   0   0   0
004 1   1400    0   0   0   0   0   0   0   0   0   0
004 0   0   0   0   0   0   1   875 0   0   0   0

1 个答案:

答案 0 :(得分:0)

请使用子查询尝试下面的代码。查询正确解析。希望这个帮助

你可能有错误。

Select 
[Order No],
Sum([Clicks Total]),
Sum([Smart Sensor Counts]),
Sum([Smart Sensors Total]),
Sum([HD Counts]),
Sum([HD Total]),
Sum([Sensor Counts]),
Sum([Sensors Total]),
Sum([Matrix Counts]),
Sum([Matrix Total]),
Sum([Advance Counts]),
Sum([Advance Total ]),
(
select 
distinct a.SOPNUMBE as 'Order No.', 
(case when ITMCLSCD like '%CLK' then COUNT(*) else 0 End) as 'Clicks Counts',
(case when ITMCLSCD like '%CLK' then sum(XTNDPRCE) else 0 End) as 'Clicks Total',
(case when ITMCLSCD like '%105' then COUNT(*) else 0 End) as 'Smart Sensor Counts',
(case when ITMCLSCD like '%105' then sum(XTNDPRCE) else 0 End) as 'Smart Sensors Total',
(case when ITMCLSCD like '%125' then COUNT(*) else 0 End) as 'HD Counts',
(case when ITMCLSCD like '%125' then sum(XTNDPRCE) else 0 End) as 'HD Total',
(case when ITMCLSCD like '%sen' then COUNT(*) else 0 End)  as 'Sensor Counts',
(case when ITMCLSCD like '%sen' then sum(XTNDPRCE) else 0 End) as 'Sensors Total',
(case when ITMCLSCD like '%Mat' then COUNT(*) else 0 End) as 'Matrix Counts',
(case when ITMCLSCD like '%Mat' then sum(XTNDPRCE) else 0 End) as 'Matrix Total',
(case when ITMCLSCD like '%200' then COUNT(*) else 0 End) as 'Advance Counts',
(case when ITMCLSCD like '%200' then sum(XTNDPRCE) else 0 End) as 'Advance Total'

from SOP30300 a join IV00101 b

on a.ITEMNMBR = b.ITEMNMBR

join sop30200 c

on a.SOPNUMBE = c.SOPNUMBE

where (a.SOPNUMBE like 'INV%')


group by a.SOPNUMBE, ITMCLSCD

--order by SOPNUMBE
) as t 
Group  by [Order No]
order by [Order No]