-1

I have a table

DocumentNumber, ItemNumber, Qty,
-----------------------------------
600000001,  1006YL01,   1
100001976,  1006YL01,   -1
100008439,  1006YL08,   -1
600000001,  1006YL08,   1
100005095,  1006YL08,   -1
100003814,  1006YL08,   -1
100002955,  1006YL08,   -1
500000087,  1006YL08,   2
500000212,  1006YL08,   1
100001152,  1006YL09,   -1
100004799,  1006YL09,   -1
500000282,  1006YL09,   2
100002859,  1006YL09,   -1
600000001,  1006YL09,   2
100003625,  1006YL09,   -1
600000001,  1006YL10,   2
100003274,  1006YL10,   -1
100002451,  1006YL10,   -1

I want to make a query which returns

Document Number, ItemNumber, ReceiveQty (from qty >1), Issue Qty (from qty<1)

Can any body help?

4

2 回答 2

1
SELECT  DocumentNumber, 
        ItemNumber,
        SUM(CASE WHEN qty > 1 THEN 1 ELSE 0 END) Total_LessThanOne,
        SUM(CASE WHEN qty < 1 THEN 1 ELSE 0 END) Total_GreaterThanOne
FROM    TableName
GROUP   BY DocumentNumber, ItemNumber
于 2013-05-03T09:24:27.297 回答
0

尝试这个:

SELECT  DocumentNumber
        ,ItemNumber
        ,CASE WHEN Qty > 1 THEN Qty ELSE 0 END ReceiveQty 
        ,CASE WHEN Qty < 1 THEN Qty ELSE 0 END IssueQty
FROM    Table1

看到这个 SQLFiddle

你也可以在 MySQL 中使用这个:

SELECT  DocumentNumber
        ,ItemNumber
        ,GROUP_CONCAT(CASE WHEN Qty > 1 THEN Qty ELSE 0 END) ReceiveQty 
        ,GROUP_CONCAT(CASE WHEN Qty < 1 THEN Qty ELSE 0 END) IssueQty
FROM    Table1
GROUP   BY DocumentNumber, ItemNumber

看到这个 SQLFiddle

于 2013-05-03T09:28:46.577 回答