Exercise 18:1479. Sales by Day of the Week

1.Description

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| customer_id   | int     |
| order_date    | date    | 
| item_id       | varchar |
| quantity      | int     |
+---------------+---------+
(ordered_id, item_id) is the primary key for this table.
This table contains information of the orders placed.
order_date is the date when item_id was ordered by the customer with id customer_id.

Table: Items

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| item_id             | varchar |
| item_name           | varchar |
| item_category       | varchar |
+---------------------+---------+
item_id is the primary key for this table.
item_name is the name of the item.
item_category is the category of the item.

You are the business owner and would like to obtain a sales report for category items and day of the week.

Write an SQL query to report how many units in each category have been ordered on each day of the week.

Return the result table ordered by category.

The query result format is in the following example:

2.Create Table and insert into values

There is no function for the day of the week in Hive (SQL is dayofweek()), but it can be implemented with datediff() and pmod()

2012-01-01 is Sunday, the above function returns "0-6" ("0-6" means "Sunday-Saturday" respectively)

Hive中沒有內建星期幾的函數(SQL是dayofweek()),可使用日期相减函数(datediff())及取余函数pmod()配合實現

2012-01-01為星期日,上述函數返回值为“0-6”(“0-6”分别表示“星期日-星期六)

Last updated