Home > Blockchain >  Return no row if non meets criteria using SUM()
Return no row if non meets criteria using SUM()

Time:10-09

According to documentation Sqlite SUM() function returns NULL if no row in table meets criteria. I don't want any lines to return if the id does not exist, like in the query:

SELECT SUM(tMoney.money),tCustomer.name FROM tMoney JOIN tCustomer ON tMoney.id = tCustomer.id WHERE tCustomer.id = 3

tMoney

id   money
--- ------
1      210
2      400
1      150

tCustomer

name    id
--- ------
bob      1
dan      2

CodePudding user response:

Just filter out lines with null using "having" clause

SELECT SUM(tMoney.money), tCustomer.name 
  FROM tMoney 
  JOIN tCustomer 
    ON tMoney.id = tCustomer.id 
 WHERE tCustomer.id = 3
 group by tCustomer.name 
 having SUM(tMoney.money) is not null;
  • Related