在尝试学习BigQuery SQL时,我正在使用公共数据集来运行一些建模。我有一个日期列,但我想按年份中的非完整日期分组。输入的日期为2018-2-12,但我想输入2-12或02-12。我有从日期中提取日期和月份的代码,但是找不到将两者串联以进行分组的方法。
SELECT
EXTRACT (MONTH FROM sales.date) AS month,
EXTRACT(DAY FROM sales.date) AS day ,
ROUND(AVG(sales.bottles_sold/sales.pack), 2) as pack_qty, -- average case or pack
ROUND(AVG(sales.bottles_sold), 2) AS qty_bottles, -- average total number of bottles
ROUND(AVG(sales.sale_dollars), 2) as sales_rev, -- average sales rev
ROUND(AVG((sales.state_bottle_retail - sales.state_bottle_cost) * sales.bottles_sold), 2) AS profit, -- avg profit on that day
ROUND (AVG(sales.volume_sold_liters), 2) as volumeLit, -- average volume in liters
ROUND (AVG(sales.volume_sold_gallons), 2) as volumeGal -- average volume in gal
FROM `bigquery-public-data.iowa_liquor_sales.sales` AS sales
GROUP BY
month,
day
ORDER BY
volumeGal DESC;
答案 0 :(得分:0)
SELECT
EXTRACT (MONTH FROM sales.date) AS month,
EXTRACT(DAY FROM sales.date) AS day ,
ROUND(AVG(sales.bottles_sold/sales.pack), 2) as pack_qty, -- average case or pack
ROUND(AVG(sales.bottles_sold), 2) AS qty_bottles, -- average total number of bottles
ROUND(AVG(sales.sale_dollars), 2) as sales_rev, -- average sales rev
ROUND(AVG((sales.state_bottle_retail - sales.state_bottle_cost) * sales.bottles_sold), 2) AS profit, -- avg profit on that day
ROUND (AVG(sales.volume_sold_liters), 2) as volumeLit, -- average volume in liters
ROUND (AVG(sales.volume_sold_gallons), 2) as volumeGal -- average volume in gal
FROM `bigquery-public-data.iowa_liquor_sales.sales` AS sales
GROUP BY
EXTRACT (MONTH FROM sales.date),
EXTRACT(DAY FROM sales.date)
ORDER BY
volumeGal DESC;
答案 1 :(得分:0)
您应该在SELECT语句中使用CONCAT函数。通过以下查询,您将在结果中有一个单日列,其格式为“日-月”。
SELECT
CONCAT(EXTRACT (DAY FROM sales.date) ,'-', EXTRACT(MONTH FROM sales.date)) AS day ,
ROUND(AVG(sales.bottles_sold/sales.pack), 2) as pack_qty, -- average case or pack
ROUND(AVG(sales.bottles_sold), 2) AS qty_bottles, -- average total number of bottles
ROUND(AVG(sales.sale_dollars), 2) as sales_rev, -- average sales rev
ROUND(AVG((sales.state_bottle_retail - sales.state_bottle_cost) * sales.bottles_sold), 2) AS profit, -- avg profit on that day
ROUND (AVG(sales.volume_sold_liters), 2) as volumeLit, -- average volume in liters
ROUND (AVG(sales.volume_sold_gallons), 2) as volumeGal -- average volume in gal
FROM `bigquery-public-data.iowa_liquor_sales.sales` AS sales
GROUP BY
day
ORDER BY
volumeGal DESC;
答案 2 :(得分:0)
您可以使用+运算符来连接两个字符串。
代码:
选择 convert(nvarchar,month(sales.date))+'-'+ convert(nvarchar,day(sales.date))作为date_month, ROUND(AVG(sales.bottles_sold / sales.pack),2)作为pack_qty,-平均大小或包装 ROUND(AVG(sales.bottles_sold),2)AS qty_bottles,-平均总瓶数 ROUND(AVG(sales.sale_dollars),2)作为sales_rev,-平均销售收入 ROUND(AVG((sales.state_bottle_retail-sales.state_bottle_cost)* sales.bottles_sold),2)AS利润,-当天的平均利润 ROUND(AVG(sales.volume_sold_liters),2)作为volumeLit,-以升为单位的平均体积 ROUND(AVG(sales.volume_sold_gallons),2)作为volumeGal-gal的平均体积
从bigquery-public-data.iowa_liquor_sales.sales
的AS销售
组别 date_month 订购 volumeGal DESC;