[英]Two Rows into one row in SQL
I had an output for the following like this in sQL server 2008. 我在sQL Server 2008中有以下输出。
select ItemCode,
case when trntype = 'Issued' then sum(qty) end as issuedqty,
case when trntype = 'Received' then sum(qty) end as receievedqty
from [View_New]
group by ItemCode,trntype
Order by itemcode
Code Recd Qty Issued Qty
--------------------------------------
10CMSQURSET NULL 2.0000
10CMSQURSET 56.0000 NULL
How will i display these rows into one Row: 如何将这些行显示为一行:
Code Recd Qty Issued Qty
--------------------------------------
10CMSQURSET 56.0000 2.0000
Please help 请帮忙
another way is to use PIVOT()
function 另一种方法是使用
PIVOT()
函数
SELECT itemCode,
Received AS RECEIEVEDQTY,
Issued AS ISSUEDQTY
FROM
(
SELECT itemCode, trntype, qty
FROM View_new
) dta
PIVOT
(
SUM(qty)
FOR trntype IN ([Issued], [Received])
) pvt
Group only by item code and put the CASE inside the SUM: 仅按商品代码分组,然后将CASE放在SUM中:
select ItemCode,
sum(case when trntype = 'Issued' then qty else 0 end) as issuedqty,
sum(case when trntype = 'Received' then qty else 0 end) as receievedqty
from [View_New]
group by ItemCode
Order by itemcode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.