我有以下的SQL查询
select sta_name Station,count(factory_name) as factories from gps1
group by sta_name
order by sta_name
通过这个查询我只得到一个工作站的总工厂。问题是我想得到的结果形状,工厂编号也应该显示在Station For Exampel的工厂总数中如果一个工作站有5个因素然后五个工厂应该有这个输出
1/5
2/5
3/5
4/5
5/5
任何人请帮助我得到这种结果 的更新 我想要这个输出 例如我在车站1有5家工厂
factoryName Count
factroty 1 1/5
factroty 2 2/5
factroty 3 3/5
factroty 4 4/5
factroty 5 5/5
答案 0 :(得分:5)
将计数转换为分析函数并删除分组:
select sta_name Station,count(factory_name) over(partition by sta_name) as factories from gps1
order by sta_name
当然,如果您想要1/5
,请将其更改为:
select sta_name||'/'||count(factory_name) over(partition by sta_name) as factories from gps1
order by sta_name
编辑:将其标记为“1个满分5”,“2个满分5”等,然后您可以添加row_number
:
select FACTORY_NAME
, row_number() over (partition by STA_NAME order by STA_NAME)
|| '/'
|| count(STA_NAME) over (partition by STA_NAME)
as FACTORIES
from GPS1
order by FACTORY_NAME