Postgres 9.3分组依据和计数功能

时间:2015-07-29 14:52:52

标签: database postgresql count group-by

我对数据库稍微有点新意,但我有一些关于MySQL的经验,但现在我正在制作一个使用Postgres的应用程序,我很混淆查询如何与postgres一起工作,或者结构与MySQL略有不同。我真的不知道从哪里开始。所以我有一张桌子

ren OldTimesheets\%_pdfname% %str%%_pdfname%

qa_forms ->id ->agent_id ->verified_status 的值verified_status我想要做的是计算每Passed, Passed-Approved, Passed-With Changes, Passed-Unverified, Pending, Reject A, Reject B, Reject CPassed, Passed-Approved, Passed-With Changes, Passed-Unverified, Pending, Reject A, Reject B, Reject C的总数,使其为agent_id } 我猜。我有Postgres 9.3。这是报告应该是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

我试过这个并且似乎工作得很好。任何更好的答案表示赞赏。

`SELECT agent_id, 
COUNT(verified_status = 'Passed' OR NULL) AS passed, 
COUNT(verified_status = 'Passed-Approved' OR NULL) AS passed_approved,
COUNT(verified_status = 'Passed-With Changes' OR NULL) AS passed_changes,
COUNT(verified_status = 'Passed-Unverified' OR NULL) AS passed_unverified,
COUNT(verified_status = 'Pending' OR NULL) AS pending,
COUNT(verified_status = 'Reject A' OR NULL) AS reject_a,
COUNT(verified_status = 'Reject B' OR NULL) AS reject_b,
COUNT(verified_status = 'Reject C' OR NULL) AS reject_c
FROM qa_forms 
GROUP BY agent_id;`