Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否仅在PostgreSQL中按月分组查询?_Sql_Postgresql_Group By_Count_Sum - Fatal编程技术网

是否仅在PostgreSQL中按月分组查询?

是否仅在PostgreSQL中按月分组查询?,sql,postgresql,group-by,count,sum,Sql,Postgresql,Group By,Count,Sum,我有一份三个月的售出物品清单和它们各自的日期。我想创建一个表格,显示每月9月、10月、11月售出的商品总数,以及每个月最畅销的商品 这就是我的数据输出的样子: ~dateshipped~ ~item~ 9/3/19 A 9/3/19 A 9/5/19 B 10/10/19 C 10/12/19 D 10/12/19 D 11/13/19

我有一份三个月的售出物品清单和它们各自的日期。我想创建一个表格,显示每月9月、10月、11月售出的商品总数,以及每个月最畅销的商品

这就是我的数据输出的样子:

  ~dateshipped~  ~item~  

    9/3/19         A
    9/3/19         A
    9/5/19         B
    10/10/19       C
    10/12/19       D
    10/12/19       D
    11/13/19       D
    11/15/19       B     
    11/15/19       B
    11/18/19       A 
期望结果:三列:月份//商品总数//该特定月份销售的最佳商品

  ~month~  ~total_month~  ~best_sold~ 
   Sep           3             A             
   Oct           3             D
   Nov           4             B

对我来说,获得解决方案的最简单方法是什么

使用cte尝试下面的方法

 with cte as 
 (
 select
 to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon')
 as month, count(*) as total_month
 from table_name group by 
 to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon')

  ) , cte2 as
   (
     select  to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon') as month, count(*) as total_month,item
    from table_name group by
   to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon'),item
   )
  ,cte3 as
  ( select t1.* from cte2 t1 where
    t1.total_month=(select max(total_month) from cte2 t2 where t1.month=t2.month)
 ) select cte.*,cte3.item as best_sold from cte join cte3 on cte.month=cte3.month
 order by cte.month 
输出

month   total_month     best_sold
nov     4                B
oct     3                D
sep     3                A

使用cte尝试下面的方法

 with cte as 
 (
 select
 to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon')
 as month, count(*) as total_month
 from table_name group by 
 to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon')

  ) , cte2 as
   (
     select  to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon') as month, count(*) as total_month,item
    from table_name group by
   to_char(to_timestamp (EXTRACT(MONTH FROM dateshipped)::text, 'MM'), 'TMmon'),item
   )
  ,cte3 as
  ( select t1.* from cte2 t1 where
    t1.total_month=(select max(total_month) from cte2 t2 where t1.month=t2.month)
 ) select cte.*,cte3.item as best_sold from cte join cte3 on cte.month=cte3.month
 order by cte.month 
输出

month   total_month     best_sold
nov     4                B
oct     3                D
sep     3                A

一个简单的group by后跟rank窗口函数应该可以做到这一点:

以cte1为例 选择EXTRACTMONTH FROM dateshipped AS month_num 项目 ,SUMCOUNT*从发货日期算起,按提取月份在分区上作为本月的总计 ,将*计为本月总计项目 ,从发货日期起按提取月份按计数*描述为项目排名 从t 从发货日期开始按月分组 项目 挑选* 来自cte1 其中项目排名=1
一个简单的group by后跟rank窗口函数应该可以做到这一点:

以cte1为例 选择EXTRACTMONTH FROM dateshipped AS month_num 项目 ,SUMCOUNT*从发货日期算起,按提取月份在分区上作为本月的总计 ,将*计为本月总计项目 ,从发货日期起按提取月份按计数*描述为项目排名 从t 从发货日期开始按月分组 项目 挑选* 来自cte1 其中项目排名=1 @墨西哥检查link@mexica2检查链接