我有這個sql語法,這很好。它顯示了用於獲取我們的源的數量。我希望數字以百分比顯示(100%以外),而不是數字顯示。我如何得到我的陳述以百分比顯示計數,請提供幫助Databasers。顯示周和年份的百分比
聲明:
select coalesce(t2.Source, 'Total') as "Source",
coalesce(count(case when Week = date_trunc('week', now())::date - 1
then t1."Source" end),0) "This Week",
coalesce(count(t1.Source),0) "YTD"
from infosys t2
left join infocentertracker t1
ON t1.Source = t2.Source
and Week >= DATE '01/01/2017'
and Week < DATE '01/01/2018'
and Project_ID = 'PCR'
group by rollup(t2.Source)
結果:
Source This Week YTD This Week YTD
Billboard 0 0 0% 0%
Builder 2 8 20% 12%
Community Website 0 2 0% 3%
Drive-by 6 35 60% 52%
Non-Disclosed 0 1 Requested --> 0% 2%
Other Website 0 4 0% 6%
Radio 0 0 0% 0%
Realtor 1 13 10% 19%
Resident Referral 1 3 10% 4%
Return Visitor 0 0 0% 0%
RPG Website 0 0 0% 0%
Social Media 0 2 0% 3%
Total 10 68 100% 100%
你爲什麼使用'by rollup'? – McNets
在查詢底部顯示總數。 –
也許這裏需要一個窗口函數:http://stackoverflow.com/a/6207658/3430807 – Andreas