2015-05-19 55 views
-3

我试图检索销售我正在寻找的特定产品的所有订单。例如,有些订单可能只有一个项目,有些订单可能有多个项目。我想将订单上的所有商品合并为一行。这里是我的脚本,它返回所有我在寻找的产品:如何通过mssql中的订单号将多个订单合并为一个订单

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 

回答

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]