如何在PostgreSQL中制定条件和?

时间:2013-12-23 08:58:53

标签: sql postgresql

我的表格包含idcategorynoofquestionscompany。我想要一个查询,当noofquestions在两列或更多列中相同时,noofquestions的值为category的总和。我正在尝试此查询,但它只添加类别相同且noofquestions相等且错误的列。它不应该检查noofquestions

 SELECT id ,  category,   SUM(NULLIF(noofquestions, '')::int), company 
 FROM tableName   
 WHERE id=1 
 GROUP BY id, category, noofquestions, company;

1 个答案:

答案 0 :(得分:2)

您不应按noofquestions分组:

SELECT   id, category, SUM(NULLIF(noofquestions, '')::int), company 
FROM     tableName 
WHERE    id = 1 
GROUP BY id, category, company;