使用 Sql 填补缺失的宿舍
Fill Missing Quarters using Sql
我在 table 中有这样的数据。
Customer_Key
Customer_Name
Component
Region
Quarter
Sales
109
General Motors
Piston
EAST
2019-Q3
7,85,063.37
109
General Motors
Piston
EAST
2020-Q2
24,36,808.92
109
General Motors
Piston
EAST
2020-Q4
19,03,820.10
109
General Motors
Piston
EAST
2021-Q2
62,24,228.01
109
General Motors
Piston
EAST
2022-Q3
44,35,135.25
109
General Motors
Piston
NORTH
2020-Q2
0
109
General Motors
Piston
NORTH
2020-Q3
-1,93,84,747.20
109
General Motors
Piston
NORTH
2021-Q2
-4,78,786.77
109
General Motors
Piston
NORTH
2021-Q3
3,84,93,986.44
109
General Motors
Piston
WEST
2019-Q1
10,29,581.28
109
General Motors
Piston
WEST
2020-Q3
5,51,29,226.88
我想 fill/insert 从 2019-Q1 到 2022-Q4 缺少季度的行,缺少季度的销售额为 0,其余维度保持原样。
例如:West 只有两行 quarter 2019-Q1 AND 2020-Q3。我还需要 2019-Q1 和 2022-Q4 之间的其他 14 行剩余季度。
样本fiddle数据集:Data Set
能否请一些人帮助 sql 代码来实现相同的目标?
试试这个,
CREATE TABLE #Fill_Gaps
(Customer_Key int, Customer_Name varchar(14), Component varchar(6), Region varchar(5),Quarter varchar(7), Sales varchar(15))
;
INSERT INTO #Fill_Gaps
(Customer_Key, Customer_Name, Component, Region, Quarter, Sales)
VALUES
(109, 'General Motors', 'Piston', 'EAST', '2019-Q3', '7,85,063.37'),
(109, 'General Motors', 'Piston', 'EAST', '2020-Q2', '24,36,808.92'),
(109, 'General Motors', 'Piston', 'EAST', '2020-Q4', '19,03,820.10'),
(109, 'General Motors', 'Piston', 'EAST', '2021-Q2', '62,24,228.01'),
(109, 'General Motors', 'Piston', 'EAST', '2022-Q3', '44,35,135.25'),
(109, 'General Motors', 'Piston', 'NORTH', '2020-Q2', '0'),
(109, 'General Motors', 'Piston', 'NORTH', '2020-Q3', '-1,93,84,747.20'),
(109, 'General Motors', 'Piston', 'NORTH', '2021-Q2', '-4,78,786.77'),
(109, 'General Motors', 'Piston', 'NORTH', '2021-Q3', '3,84,93,986.44'),
(109, 'General Motors', 'Piston', 'WEST', '2019-Q1', '10,29,581.28'),
(109, 'General Motors', 'Piston', 'WEST', '2020-Q3', '5,51,29,226.88')
;
CREATE TABLE #Quarters
(Quarters varchar(7))
;
INSERT INTO #Quarters
(Quarters)
VALUES
('2019-Q1'),
('2019-Q2'),
('2019-Q3'),
('2019-Q4'),
('2020-Q1'),
('2020-Q2'),
('2020-Q3'),
('2020-Q4'),
('2021-Q1'),
('2021-Q2'),
('2021-Q3'),
('2021-Q4'),
('2022-Q1'),
('2022-Q2'),
('2022-Q3'),
('2022-Q4')
;
create table #temp (Customer_Key int, Customer_Name varchar(14), Component varchar(6), Region varchar(5),Quarter varchar(7))
--insert into #temp(Customer_Key,Customer_Name, Component, Region,Quarter)
--Select distinct Customer_Key,Customer_Name, Component, Region,Quarters
--from #Fill_Gaps A, #Quarters B
---OR
insert into #temp(Customer_Key,Region,Quarter)
Select distinct Customer_Key,Region,Quarters
from #Fill_Gaps A, #Quarters B
update t
set Customer_Name=fg.Customer_Name,Component=fg.Component
from #temp t
inner join #Fill_Gaps fg on fg.Customer_Key=t.Customer_Key
select t.Customer_Key , t.Customer_Name, t.Component , t.Region ,t.Quarter,isnull(fg.Sales,0)Sales
from #temp t
left join #Fill_Gaps fg on fg.Customer_Key=t.Customer_Key and t.Quarter=fg.Quarter and t.Region=fg.Region
drop table #Fill_Gaps,#Quarters,#temp
我在 table 中有这样的数据。
Customer_Key | Customer_Name | Component | Region | Quarter | Sales |
---|---|---|---|---|---|
109 | General Motors | Piston | EAST | 2019-Q3 | 7,85,063.37 |
109 | General Motors | Piston | EAST | 2020-Q2 | 24,36,808.92 |
109 | General Motors | Piston | EAST | 2020-Q4 | 19,03,820.10 |
109 | General Motors | Piston | EAST | 2021-Q2 | 62,24,228.01 |
109 | General Motors | Piston | EAST | 2022-Q3 | 44,35,135.25 |
109 | General Motors | Piston | NORTH | 2020-Q2 | 0 |
109 | General Motors | Piston | NORTH | 2020-Q3 | -1,93,84,747.20 |
109 | General Motors | Piston | NORTH | 2021-Q2 | -4,78,786.77 |
109 | General Motors | Piston | NORTH | 2021-Q3 | 3,84,93,986.44 |
109 | General Motors | Piston | WEST | 2019-Q1 | 10,29,581.28 |
109 | General Motors | Piston | WEST | 2020-Q3 | 5,51,29,226.88 |
我想 fill/insert 从 2019-Q1 到 2022-Q4 缺少季度的行,缺少季度的销售额为 0,其余维度保持原样。
例如:West 只有两行 quarter 2019-Q1 AND 2020-Q3。我还需要 2019-Q1 和 2022-Q4 之间的其他 14 行剩余季度。
样本fiddle数据集:Data Set
能否请一些人帮助 sql 代码来实现相同的目标?
试试这个,
CREATE TABLE #Fill_Gaps
(Customer_Key int, Customer_Name varchar(14), Component varchar(6), Region varchar(5),Quarter varchar(7), Sales varchar(15))
;
INSERT INTO #Fill_Gaps
(Customer_Key, Customer_Name, Component, Region, Quarter, Sales)
VALUES
(109, 'General Motors', 'Piston', 'EAST', '2019-Q3', '7,85,063.37'),
(109, 'General Motors', 'Piston', 'EAST', '2020-Q2', '24,36,808.92'),
(109, 'General Motors', 'Piston', 'EAST', '2020-Q4', '19,03,820.10'),
(109, 'General Motors', 'Piston', 'EAST', '2021-Q2', '62,24,228.01'),
(109, 'General Motors', 'Piston', 'EAST', '2022-Q3', '44,35,135.25'),
(109, 'General Motors', 'Piston', 'NORTH', '2020-Q2', '0'),
(109, 'General Motors', 'Piston', 'NORTH', '2020-Q3', '-1,93,84,747.20'),
(109, 'General Motors', 'Piston', 'NORTH', '2021-Q2', '-4,78,786.77'),
(109, 'General Motors', 'Piston', 'NORTH', '2021-Q3', '3,84,93,986.44'),
(109, 'General Motors', 'Piston', 'WEST', '2019-Q1', '10,29,581.28'),
(109, 'General Motors', 'Piston', 'WEST', '2020-Q3', '5,51,29,226.88')
;
CREATE TABLE #Quarters
(Quarters varchar(7))
;
INSERT INTO #Quarters
(Quarters)
VALUES
('2019-Q1'),
('2019-Q2'),
('2019-Q3'),
('2019-Q4'),
('2020-Q1'),
('2020-Q2'),
('2020-Q3'),
('2020-Q4'),
('2021-Q1'),
('2021-Q2'),
('2021-Q3'),
('2021-Q4'),
('2022-Q1'),
('2022-Q2'),
('2022-Q3'),
('2022-Q4')
;
create table #temp (Customer_Key int, Customer_Name varchar(14), Component varchar(6), Region varchar(5),Quarter varchar(7))
--insert into #temp(Customer_Key,Customer_Name, Component, Region,Quarter)
--Select distinct Customer_Key,Customer_Name, Component, Region,Quarters
--from #Fill_Gaps A, #Quarters B
---OR
insert into #temp(Customer_Key,Region,Quarter)
Select distinct Customer_Key,Region,Quarters
from #Fill_Gaps A, #Quarters B
update t
set Customer_Name=fg.Customer_Name,Component=fg.Component
from #temp t
inner join #Fill_Gaps fg on fg.Customer_Key=t.Customer_Key
select t.Customer_Key , t.Customer_Name, t.Component , t.Region ,t.Quarter,isnull(fg.Sales,0)Sales
from #temp t
left join #Fill_Gaps fg on fg.Customer_Key=t.Customer_Key and t.Quarter=fg.Quarter and t.Region=fg.Region
drop table #Fill_Gaps,#Quarters,#temp