提交 6dfa31b8 编写于 作者: M Mars Liu

union

上级 fd30a1a2
{
"type": "code_options",
"author": "ccat",
"source": "pivot.md",
"source": "case.md",
"notebook_enable": false,
"exercise_id": "d61198987832488ab53e8a18f7337946"
}
\ No newline at end of file
# 透视表
Goods 表结构如下
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
category varchar(64),
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
Joe 想要做一个报表,只需要显示商品名和价格分级,其中不足10元的是 cheap, 超过1000的是expensive,其它的是
normal,这个查询应该怎么写?
## 答案
```mysql
select name,
case
when price < 10 then 'cheap'
when price > 1000 then 'expensive'
else 'normal'
end as level
from goods;
```
## 选项
### A
```mysql
select name,
case price
when < 10 then 'cheap'
when > 1000 then 'expensive'
else 'normal'
end as level
from goods;
```
### B
```mysql
select name,
case
when price < 10 'cheap'
when price > 1000 'expensive'
else 'normal'
end as level
from goods;
```
### C
```mysql
select name,
case
when price < 10 then 'cheap'
when price > 1000 then 'expensive'
case _ 'normal'
end as level
from goods;
```
### C
```mysql
select name,
case
when price < 10 then 'cheap'
when price > 1000 then 'expensive'
case _ 'normal'
end as level
from goods;
```
\ No newline at end of file
{
"node_id": "mysql-8e6cd4d5f4b446a2bc3f5402de9bd49c",
"keywords": ["case", "pivot", "透视表"],
"keywords": [
"case",
"pivot",
"透视表"
],
"children": [],
"export": [
"pivot.json"
"case.json"
],
"keywords_must": [],
"keywords_forbid": [],
......
# 透视表
现有销售记录表
```mysql
create table sales(
id serial primary key ,
sku_id integer not null ,
amount decimal(12, 4),
created_at timestamp default now()
);
create index idx_created_at on sales(created_at);
```
现在我们希望对这个表做一个月度的透视汇总,得到2021年每个月每种商品(sku_id)的的销售总额,每个月一列,哪一项可以实现?
## 答案
```mysql
select sku_id,
sum(case extract(month from created_at) when 1 then amount else 0 end) as Jan,
sum(case extract(month from created_at) when 2 then amount else 0 end) as Feb,
sum(case extract(month from created_at) when 3 then amount else 0 end) as Mar,
sum(case extract(month from created_at) when 4 then amount else 0 end) as Apr,
sum(case extract(month from created_at) when 5 then amount else 0 end) as May,
sum(case extract(month from created_at) when 6 then amount else 0 end) as June,
sum(case extract(month from created_at) when 7 then amount else 0 end) as July,
sum(case extract(month from created_at) when 8 then amount else 0 end) as Aug,
sum(case extract(month from created_at) when 9 then amount else 0 end) as Sept,
sum(case extract(month from created_at) when 10 then amount else 0 end) as Oct,
sum(case extract(month from created_at) when 11 then amount else 0 end) as Nov,
sum(case extract(month from created_at) when 12 then amount else 0 end) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id;
```
## 选项
### 格式不相符
```mysql
select sku_id, extract(month from created_at) as month, sum(amount)
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by 1, 2;
```
### 计算逻辑错误
```mysql
select sku_id,
sum(amount) as Jan,
sum(amount) as Feb,
sum(amount) as Mar,
sum(amount) as Apr,
sum(amount) as May,
sum(amount) as June,
sum(amount) as July,
sum(amount) as Aug,
sum(amount) as Sept,
sum(amount) as Oct,
sum(amount) as Nov,
sum(amount) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id, extract(month from created_at);
```
### 计算格式错误
```mysql
select sku_id,
sum(amount having extract(month from created_at) = 1) as Jan,
sum(amount having extract(month from created_at) = 2) as Feb,
sum(amount having extract(month from created_at) = 3) as Mar,
sum(amount having extract(month from created_at) = 4) as Apr,
sum(amount having extract(month from created_at) = 5) as May,
sum(amount having extract(month from created_at) = 6) as June,
sum(amount having extract(month from created_at) = 7) as July,
sum(amount having extract(month from created_at) = 8) as Aug,
sum(amount having extract(month from created_at) = 9) as Sept,
sum(amount having extract(month from created_at) = 10) as Oct,
sum(amount having extract(month from created_at) = 11) as Nov,
sum(amount having extract(month from created_at) = 12) as Dec
from sales
where created_at between '2020-01-01'::timestamp and '2021-01-01'::timestamp
group by sku_id, extract(month from created_at);
```
\ No newline at end of file
{
"node_id": "mysql-b57b6c08f5f240c6a997284e4448f088",
"keywords": [],
"keywords": ["union"],
"children": [],
"export": [],
"export": [
"union.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 1
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "union.md",
"notebook_enable": false,
"exercise_id": "331f369b150340f9989ccea8abfcd42f"
}
\ No newline at end of file
# Union
现有员工信息表和顾客信息表如下
```mysql
create table employee(
id int primary key auto_increment,
name varchar(256),
address varchar(1024),
dept varchar(64)
-- ignore more
);
create table customer(
id int primary key auto_increment,
name varchar(256),
address varchar(1024),
level int
-- ignore more
)
```
Joe 需要员工和顾客的联系方式(姓名+地址)清单,用于邮寄礼品。这个查询如何写?
## 答案
```mysql
select name, address
from customer
union
select name, address
from employee
```
## 选项
### A
```mysql
select *
from customer
union
select *
from employee
```
### B
```mysql
select *
from customer
join employee
```
### C
```mysql
select *
from customer
join employee on customer.id = employee.id
```
### D
```mysql
select *
from customer, employee
```
### E
```mysql
select name, address
from customer, employee
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册