每月数据比例

时间:2018-07-12 13:56:41

标签: sql google-bigquery

我正在尝试制作每月数据比例表

数据:

  

消息|日期|

     

bla bla | 2018-04-10 |

     

blabla | 2018-04-11 |

     

blabla | 2018-04-12 |

     

blabla | 2018-04-13 |

     

blabla | 2018-05-13 |

     

blabla | 2018-05-14 |

     

blabla | 2018-05-15 |

     

blabla | 2018-05-16 |

     

blabla | 2018-05-17 |

我尝试过

SELECT Date, count(*) as Message, EXTRACT(YEAR FROM Date) as year, EXTRACT(MONTH FROM Date) as month, MIN(Date) as date
FROM `my_table` 
GROUP BY Date, EXTRACT(YEAR FROM Date) as an, EXTRACT(MONTH FROM Date)

我想要:

  

消息|日期|

     

4 |四月|

     

5 |可能|

很抱歉,我不知道如何在这里摆桌子。

2 个答案:

答案 0 :(得分:2)

如果数据仅限于今年,则

 SELECT EXTRACT(MONTH FROM Date) date , count(*) message from your_table 
 GROUP BY EXTRACT(MONTH FROM Date)

您可以尝试多年

 SELECT EXTRACT(MONTH FROM Date) as Mnth,EXTRACT(YEAR FROM Date) as yr ,
 count(*) message from your_table 
 GROUP BY EXTRACT(MONTH FROM Date), EXTRACT(YEAR FROM Date) 

答案 1 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
SELECT 
  COUNT(*) AS Message, 
  EXTRACT(YEAR FROM Date) AS year, 
  FORMAT_DATE('%B', Date) AS month
FROM `project.dataset.my_table` 
GROUP BY 
  EXTRACT(YEAR FROM Date), 
  FORMAT_DATE('%B', Date)  

您可以使用以下问题的虚拟数据来测试,玩游戏

#standardSQL
WITH `project.dataset.my_table` AS (
  SELECT 'bla bla' Message, DATE '2018-04-10' Date UNION ALL
  SELECT 'blabla', '2018-04-11' UNION ALL
  SELECT 'blabla', '2018-04-12' UNION ALL
  SELECT 'blabla', '2018-04-13' UNION ALL
  SELECT 'blabla', '2018-05-13' UNION ALL
  SELECT 'blabla', '2018-05-14' UNION ALL
  SELECT 'blabla', '2018-05-15' UNION ALL
  SELECT 'blabla', '2018-05-16' UNION ALL
  SELECT 'blabla', '2018-05-17' 
)
SELECT 
  COUNT(*) AS Message, 
  EXTRACT(YEAR FROM Date) AS year, 
  FORMAT_DATE('%B', Date) AS month
FROM `project.dataset.my_table` 
GROUP BY 
  EXTRACT(YEAR FROM Date), 
  FORMAT_DATE('%B', Date)  

结果为

Row Message year    month    
1   5       2018    May  
2   4       2018    April