年和月列问题 SQL
Year and Month columns issue SQL
我收到了一组类似的数据,如下所示。如您所见,月份在自己的列中,年份在自己的列中,然后存储产品的每月销售数据。要获取数据,我正在使用 Oracle。
我现在的问题是,由于销售额列在月份列下,因此很难进一步处理数据,例如在 PowerBi 中。我的目标是改变底部的结构 table。目前我不知道它会如何运作。欢迎任何提示。
Product
Year
Jan
Feb
March
April
May
June
July
Aug
Sep
Oct
Nov
Dec
Bike
2020
12
42
42
42
20
20
12
10
15
3
16
27
Bike
2021
10
11
15
53
30
20
10
10
目标:
Product
Sale
Year
Month
Bike
12
2020
Jan
Bike
42
2020
Feb
Bike
42
2021
Jan
Bike
42
2021
Feb
您可以使用横向连接:
select t.product, t.year, x.month, x.sale
from t cross join lateral
(select 'Jan' as month, t.jan as sale from dual union all
select 'Feb' as month, t.feb as sale from dual union all
select 'Mar' as month, t.Mar as sale from dual union all
select 'Apr' as month, t.Apr as sale from dual union all
select 'May' as month, t.May as sale from dual union all
select 'Jun' as month, t.Jun as sale from dual union all
select 'Jul' as month, t.Jul as sale from dual union all
select 'Aug' as month, t.Aug as sale from dual union all
select 'Sep' as month, t.Sep as sale from dual union all
select 'Oct' as month, t.Oct as sale from dual union all
select 'Nov' as month, t.Nov as sale from dual union all
select 'Dec' as month, t.dec as sale from dual
) x;
Here 是一个 db<>fiddle.
您可以使用 buldin 函数对列进行逆透视。
转到“转换”,select 要逆透视的列,然后使用“逆透视列”
输出:
这里是 excel 的高级代码(包含您的样本数据)
let
Source = Excel.Workbook(File.Contents("C:\Users\Mers\Downloads\pbi_tst.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Product", type text}, {"Year", Int64.Type}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"March", Int64.Type}, {"April", Int64.Type}, {"May", Int64.Type}, {"June", Int64.Type}, {"July", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "Year"}, "Attribute", "Value")
in
#"Unpivoted Columns"
这部分正在做的工作:
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "Year"}, "Attribute", "Value")
这称为逆轴。
测试数据:
create table sales (product, year, jan, feb, march, april, may, june, july, aug, sep, oct, nov, dec)
as
select 'Bike', 2020, 12, 42, 42, 42, 20, 20, 12, 10, 15, 3, 16, 27 from dual union all
select 'Bike', 2021, 10, 11, 15, 53, 30, 20, 10, 10, null, null, null, null from dual;
查询:
select *
from sales s
unpivot (month
for total in
(jan, feb, march, april, may, june, july, aug, sep, oct, nov, dec)
)
order by product, year;
PRODUCT YEAR TOTAL MONTH
------- ---------- ----- ----------
Bike 2020 JAN 12
Bike 2020 FEB 42
Bike 2020 DEC 27
Bike 2020 NOV 16
Bike 2020 OCT 3
Bike 2020 SEP 15
Bike 2020 AUG 10
Bike 2020 JULY 12
Bike 2020 JUNE 20
Bike 2020 MARCH 42
Bike 2020 APRIL 42
Bike 2020 MAY 20
Bike 2021 APRIL 53
Bike 2021 MARCH 15
Bike 2021 FEB 11
Bike 2021 JAN 10
Bike 2021 JULY 10
Bike 2021 JUNE 20
Bike 2021 MAY 30
Bike 2021 AUG 10
棘手的部分是按日历顺序排列月份,因为它们的命名不一致('Jan'、'Feb',但随后是 'March'、'April')。您可以 fiddle 与 to_date
和 substr
,或者使用 case
表达式为每个分配一个数字。
此外,它应该如何处理具有空值的月份?
我收到了一组类似的数据,如下所示。如您所见,月份在自己的列中,年份在自己的列中,然后存储产品的每月销售数据。要获取数据,我正在使用 Oracle。
我现在的问题是,由于销售额列在月份列下,因此很难进一步处理数据,例如在 PowerBi 中。我的目标是改变底部的结构 table。目前我不知道它会如何运作。欢迎任何提示。
Product | Year | Jan | Feb | March | April | May | June | July | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Bike | 2020 | 12 | 42 | 42 | 42 | 20 | 20 | 12 | 10 | 15 | 3 | 16 | 27 |
Bike | 2021 | 10 | 11 | 15 | 53 | 30 | 20 | 10 | 10 |
目标:
Product | Sale | Year | Month |
---|---|---|---|
Bike | 12 | 2020 | Jan |
Bike | 42 | 2020 | Feb |
Bike | 42 | 2021 | Jan |
Bike | 42 | 2021 | Feb |
您可以使用横向连接:
select t.product, t.year, x.month, x.sale
from t cross join lateral
(select 'Jan' as month, t.jan as sale from dual union all
select 'Feb' as month, t.feb as sale from dual union all
select 'Mar' as month, t.Mar as sale from dual union all
select 'Apr' as month, t.Apr as sale from dual union all
select 'May' as month, t.May as sale from dual union all
select 'Jun' as month, t.Jun as sale from dual union all
select 'Jul' as month, t.Jul as sale from dual union all
select 'Aug' as month, t.Aug as sale from dual union all
select 'Sep' as month, t.Sep as sale from dual union all
select 'Oct' as month, t.Oct as sale from dual union all
select 'Nov' as month, t.Nov as sale from dual union all
select 'Dec' as month, t.dec as sale from dual
) x;
Here 是一个 db<>fiddle.
您可以使用 buldin 函数对列进行逆透视。
转到“转换”,select 要逆透视的列,然后使用“逆透视列”
输出:
这里是 excel 的高级代码(包含您的样本数据)
let
Source = Excel.Workbook(File.Contents("C:\Users\Mers\Downloads\pbi_tst.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Product", type text}, {"Year", Int64.Type}, {"Jan", Int64.Type}, {"Feb", Int64.Type}, {"March", Int64.Type}, {"April", Int64.Type}, {"May", Int64.Type}, {"June", Int64.Type}, {"July", Int64.Type}, {"Aug", Int64.Type}, {"Sep", Int64.Type}, {"Oct", Int64.Type}, {"Nov", Int64.Type}, {"Dec", Int64.Type}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "Year"}, "Attribute", "Value")
in
#"Unpivoted Columns"
这部分正在做的工作:
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Product", "Year"}, "Attribute", "Value")
这称为逆轴。
测试数据:
create table sales (product, year, jan, feb, march, april, may, june, july, aug, sep, oct, nov, dec)
as
select 'Bike', 2020, 12, 42, 42, 42, 20, 20, 12, 10, 15, 3, 16, 27 from dual union all
select 'Bike', 2021, 10, 11, 15, 53, 30, 20, 10, 10, null, null, null, null from dual;
查询:
select *
from sales s
unpivot (month
for total in
(jan, feb, march, april, may, june, july, aug, sep, oct, nov, dec)
)
order by product, year;
PRODUCT YEAR TOTAL MONTH
------- ---------- ----- ----------
Bike 2020 JAN 12
Bike 2020 FEB 42
Bike 2020 DEC 27
Bike 2020 NOV 16
Bike 2020 OCT 3
Bike 2020 SEP 15
Bike 2020 AUG 10
Bike 2020 JULY 12
Bike 2020 JUNE 20
Bike 2020 MARCH 42
Bike 2020 APRIL 42
Bike 2020 MAY 20
Bike 2021 APRIL 53
Bike 2021 MARCH 15
Bike 2021 FEB 11
Bike 2021 JAN 10
Bike 2021 JULY 10
Bike 2021 JUNE 20
Bike 2021 MAY 30
Bike 2021 AUG 10
棘手的部分是按日历顺序排列月份,因为它们的命名不一致('Jan'、'Feb',但随后是 'March'、'April')。您可以 fiddle 与 to_date
和 substr
,或者使用 case
表达式为每个分配一个数字。
此外,它应该如何处理具有空值的月份?