如何在postgresql中获取频率

thtygnil  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(232)

我有一个这样的表(表1)(我简化了它)

code   |    description   |    season    
----------+------------------+--------------
   500    |      info 1      |    fall    
   500    |      info 4      |    fall    
   500    |      info 8      |    fall    
   500    |      info 1      |    winter
   300    |      info 1      |    spring
   400    |      info 1      |    fall

我想要一个如下的表格,在那里我有每个季节的代码频率

season  |  Number of Unique Codes  
----------+------------------------
   fall   |           2      
  winter  |           1           
  spring  |           1

到目前为止,我有:

SELECT 
    season, 
    count(DISTINCT code) AS "Number of Unique Codes"
FROM table_1
WHERE code IS NOT NULL
GROUP BY season
ORDER BY code desc;

然而,我遇到了一些问题。

wn9m85ua

wn9m85ua1#

你的错误在 ORDER BY ,更改您的 ORDER BY 按创建的别名排序。

SELECT 
    season, 
    count(distinct code) AS "Number of Unique Codes"
FROM table_1
WHERE code IS NOT NULL
GROUP BY season
ORDER BY "Number of Unique Codes" DESC;

相关问题