我有一个问题要在MS SQL 2005 Management studio上运行查询,我有一个表格如下结构,
|收据编号|货号|金额|
| 1 | 1 | $10 |
| 1 | X | $0 |
| 2 | 2 | $10 |
| 2 | X | $0 |
如何使用任何相同的收据号和金额总和查询任何第X项?等
2号收据和第2号收据的记录以及X总额为10美元(10美元+0)的结果,(实际上应该得到上述4条记录。)
非常感谢!
帕特里克
答案 0 :(得分:0)
SELECT [Receipt No],
STUFF(( SELECT ',' + SUB.[Item No]
FROM table_name SUB
WHERE
SUB.[Receipt No] = MAIN.[Receipt No]
FOR XML PATH('')
), 1, 1, '' ) as [Item No]
,sum(Amount) as [Amount]
FROM table_name MAIN
GROUP by [Receipt No]
答案 1 :(得分:0)
我有人建议这个查询...
SELECT ReceiptNo,ItemNo,Amount
FROM MyTable
WHERE RECiptNo IN
(SELECT DISTINCT ReceiptNo FROM MyTable
WHERE ItemNo =' XXX')