【问题标题】:Create a count of Column A to distribute by Column B创建 A 列的计数以按 B 列分配
【发布时间】:2021-05-05 03:07:33
【问题描述】:

我想按州统计在美国进行的审判次数。我需要的信息来自 nct_id、state 和 country 列。我在 Macbook Pro 上的 pgAdmin 中。下面是一个小样本:

nct_id        state       country
NCT04832854   Illinois    United States
NCT04832851   Michigan    United States
NCT04832802   California  United States
NCT04832813   Naples      Italy
NCT04832813   Illinois    United States

我想按州对 nct_id 进行分组,但前提是该国家/地区是“美国”。到目前为止我有这个。我收到一条错误消息:ERROR: syntax error at or near "AS" LINE 3: COUNT(nct_id) AS Count_Trials。我不确定这里的语法错误是什么。

SELECT nct_id, state
FROM facilities
COUNT(nct_id) AS Count_Trials
GROUP BY state
HAVING Country = 'United States'

我希望输出如下所示:

state      Count_Trials
Illinois   2
Michigan   1
California 1

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    你的答案如下

    SELECT  state, count(nct_id) count_trials
    FROM facilities
    where country  = 'United States'  group by state, country order by count(nct_id) desc
    

    测试

    select   state , count(nct_id) count_trials from (            
    select 'NCT04832854' nct_id  ,  'Illinois' state,    'United States'country union all
    select 'NCT04832851' ,  'Michigan',    'United States' union all
    select 'NCT04832802' ,  'California',  'United States' union all
    select 'NCT04832813' ,  'Naples',      'Italy' union all
    select 'NCT04832813' ,  'Illinois',    'United States'
    ) a where country  = 'United States'  group by state, country order by count(nct_id) desc
    

    【讨论】:

    • 成功了!!太棒了,非常感谢!
    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多