2

我在 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

我将如何将这些行显示为一行:

Code            Recd Qty   Issued Qty
--------------------------------------
10CMSQURSET 56.0000     2.0000

请帮忙

4

2 回答 2

1

另一种方法是使用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
于 2013-05-28T05:35:21.567 回答
0

仅按项目代码分组并将 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
于 2013-05-28T05:39:18.183 回答