我有數據庫表重要的列是這樣的:如何修改此查詢?
name | violation
file1| major
file1| major
file1| minor
file2| minor
file2| minor
我想結果是這樣的:
name | minor | major etc.
file1| 1 | 2
file2| 2 | 0
我已經完成,與此查詢:
select distinct component as cmp ,project_id,
(select count(severity) from issue_lines where severity = 'MINOR' AND component = cmp) as minor,
(select count(severity) from issue_lines where severity = 'MAJOR' AND component = cmp) as major,
(select count(severity) from issue_lines where severity = 'CRITICAL' AND component = cmp) as critical,
(select count(severity) from issue_lines where severity = 'INFO' AND component = cmp) as info,
(select count(severity) from issue_lines where severity = 'BLOCKER' AND component = cmp) as blocker
from issue_lines
現在我想讓它看到,因爲它會被重用很多。 但厚顏無恥的ActiveRecord execute()命令將cmp從子查詢解析到組件中,最後我得到組件==組件,並且它在所有文件中返回完全違規,而不是單個文件。
我可以創建這個視圖沒有問題直接通過工作臺,但這不是一個選項。
是否有可能重寫我的查詢以避免與execute()命令衝突?
你可以使用別名..或只是重寫查詢情況下,通過報表和組..或轉動 – JamieD77
嗯,這就是問題所在,ActiveRecord的解決別名,打破查詢。你能提供案例使用的例子嗎?它可以在任何數據庫系統上工作嗎? – Tintor
我的意思是表別名..不列... ..選擇不同的il1.component作爲cmp,il1.project_id,(select count(il2.severity)from issue_lines as il2 where il2.severity ='MINOR'AND il2.component = il1 .component)from issue_lines as il1' – JamieD77