我将如何编写一个查询,将表行按值大于0的列分组?
在我的表格中,我有4列:
- 名称(Varchar(45))
- 现金(DECIMAL(15,2))
- 卡(DECIMAL(15,2))
- Wtransfer(DECIMAL(15,2))
我可以这样显示列总和:
SELECT sum(Cash) as gotovina, sum(Card) as kartice, sum(Wtransfer) as ziralno FROM tpos.dok_zag_d;
但是我想为每种付款方式显示一个新行,并另显示一个名为“ sum”的列,其中包含使用的付款方式的总和。
答案 0 :(得分:0)
您可以添加3列
SELECT sum(Cash) as gotovina, sum(Card) as kartice, sum(Wtransfer) as ziralno
, sum(Cash) + sum(Card) + sum(Wtransfer) as summ
FROM tpos.dok_zag_d;
答案 1 :(得分:0)
我认为您可能想要UNION
:
SELECT 'Cash' AS payment_method, SUM(Cash) AS summ FROM tpos.dok_zag_d
UNION
SELECT 'Card', SUM(Card) FROM tpos.dok_zag_d
UNION
SELECT 'Wtransfer', SUM(Wtransfer) FROM tpos.dok_zag_d