按列分组,其值超过0

时间:2019-05-02 07:05:09

标签: mysql

我将如何编写一个查询,将表行按值大于0的列分组?

在我的表格中,我有4列:

  
      
  1. 名称(Varchar(45))
  2.   
  3. 现金(DECIMAL(15,2))
  4.   
  5. 卡(DECIMAL(15,2))
  6.   
  7. Wtransfer(DECIMAL(15,2))
  8.   

我可以这样显示列总和:

SELECT sum(Cash) as gotovina, sum(Card) as kartice, sum(Wtransfer) as ziralno FROM tpos.dok_zag_d;

但是我想为每种付款方式显示一个新行,并另显示一个名为“ sum”的列,其中包含使用的付款方式的总和。

2 个答案:

答案 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