简体   繁体   English

按月查找客户和订单数量以及分组 (Bigquery)

[英]Find number of customers and orders and group by month (Bigquery)

I need to get monthly sales numbers in each Country & region.我需要获得每个国家和地区的月度销售数字。

Also - a number of orders, customers and sales persons in each month with a total amount.此外 - 每个月的订单、客户和销售人员数量以及总金额。

I got stuck as I cannot understand how to make all counts/sum and also group by month as I only have daily data.我被卡住了,因为我无法理解如何进行所有计数/总和以及按月分组,因为我只有每日数据。

I have tried something like this:我试过这样的事情:

SELECT
orderdate,
  TerritoryID,
  (
  SELECT
    COUNT(SalesOrderID)
  FROM
    adwentureworks_db.salesorderheader),
  COUNT(DISTINCT CustomerID) SalesPersonID,
  SUM(totaldue)
FROM
  adwentureworks_db.salesorderheader
GROUP BY
  OrderDate,
  TerritoryID,
  TotalDue`

It should look like this:它应该是这样的:例子

Data:数据:数据

The 1st day of a month can be obtained by date_trunc(orderdate,month)一个月的第一天可以通过date_trunc(orderdate,month)获得

For the inner SELECT , I do not know what you want to archive.对于内部的SELECT ,我不知道你要归档什么。 The total number can be obtained by a window function count(...) over() .总数可以通过窗口函数count(...) over()获得。

Select 
 date_trunc(orderdate,month),
 TerritoryID,
 COUNT(DISTINCT CustomerID) as SalesPersonID,
 SUM(totaldue) as totaldue,
 COUNT(SalesOrderID) over () as totalsalesorder_of_whole_table
FROM adwentureworks_db.salesorderheader
GROUP BY 1,2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM