我在表格中有這樣的資料。
| Customer_Key | 顧客姓名 | 零件 | 地區 | 四分之一 | 銷售量 |
|---|---|---|---|---|---|
| 109 | 通用汽車 | 活塞 | 東 | 2019-Q3 | 7,85,063.37 |
| 109 | 通用汽車 | 活塞 | 東 | 2020-Q2 | 24,36,808.92 |
| 109 | 通用汽車 | 活塞 | 東 | 2020-第四季度 | 19,03,820.10 |
| 109 | 通用汽車 | 活塞 | 東 | 2021-Q2 | 62,24,228.01 |
| 109 | 通用汽車 | 活塞 | 東 | 2022-Q3 | 44,35,135.25 |
| 109 | 通用汽車 | 活塞 | 北 | 2020-Q2 | 0 |
| 109 | 通用汽車 | 活塞 | 北 | 2020-Q3 | -1,93,84,747.20 |
| 109 | 通用汽車 | 活塞 | 北 | 2021-Q2 | -4,78,786.77 |
| 109 | 通用汽車 | 活塞 | 北 | 2021-Q3 | 3,84,93,986.44 |
| 109 | 通用汽車 | 活塞 | 西方 | 2019-Q1 | 10,29,581.28 |
| 109 | 通用汽車 | 活塞 | 西方 | 2020-Q3 | 5,51,29,226.88 |
我想填充/插入從 2019-Q1 到 2022-Q4 缺少季度的行,其中缺少季度的銷售額為 0,其余維度保持不變。
例如:West 只有兩行 Quarter 2019-Q1 和 2020-Q3。我還需要 2019-Q1 和 2022-Q4 之間的其他 14 行剩余季度。
小提琴資料集示例:資料集
有人可以幫助使用 sql 代碼來實作相同的目標嗎?
uj5u.com熱心網友回復:
嘗試這個,
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483421.html
上一篇:將行轉換為列并分組
