我有两个表q1data和q1lookuppostgres 数据库。q1data包含 3 列 ( postid, reasonid, other) 并q1lookup包含 2 列 ( reasonid, reason)。
我正在尝试创建一个包含 4 列(、、、、reasonid)reason的count视图percentage。count是每个的计数,reason应该percentage每个count除以总数count(*) from q1data(即总行数,如果reasonid)。
但它给出了一个错误,并在 .附近显示语法错误count(*)。以下是我正在使用的代码。请帮忙。
select
cwfis_web.q1data.reasonid AS reasonid,
cwfis_web.q1lookup.reason AS reason,
count(cwfis_web.q1data.reasonid) AS count,
round(
(
(
count(cwfis_web.q1data.reasonid)
/
(select count(0) AS count(*) from cwfis_web.q1data)
) * 100
)
,0) AS percentage
from
cwfis_web.q1data
join
cwfis_web.q1lookup
ON cwfis_web.q1data.reasonid = cwfis_web.q1lookup.reasonid
group by
cwfis_web.q1data.reasonid;