【问题标题】:Year and Month columns issue SQL年月列发出SQL
【发布时间】:2021-10-28 00:38:01
【问题描述】:

我收到了一组类似的数据,如下所示。如您所见,月份在自己的列中,年份在自己的列中,然后存储产品的月度销售数据。我使用 Oracle 来获取数据。

我现在的问题是,由于销售值列在月份列下,因此很难进一步处理数据,例如在 PowerBi 中。我的目标是改变底部表格中的结构。目前我不知道它会如何工作。欢迎任何提示。

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

【问题讨论】:

    标签: sql oracle date powerbi-desktop


    【解决方案1】:

    您可以使用横向连接:

    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 是一个 dbfiddle。

    【讨论】:

    • 感谢您的提示。所有列最初都来自同一个表。 (产品、销售、年份、所有月份名称)。这样我就不能使用这个横向连接子句了。
    • @Meettu 是的,你可以 :) Gordon 的解决方案只使用一张表 T(替换您自己的表并尝试)。
    • @Meettu 。 . .当然可以。你试过了吗?
    • 您好!是的,我确实尝试过,但它首先没有奏效。我会再看一遍。现在我也更好地理解了你的代码。
    【解决方案2】:

    您可以使用 buldin 函数来取消透视列。

    转到转换,选择要取消透视的列,然后使用“取消透视列”

    输出:

    此处为 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")
    

    【讨论】:

      【解决方案3】:

      这称为反透视。

      测试数据:

      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')。您可以摆弄to_datesubstr,或者使用case 表达式为每个人分配一个数字。

      另外,它应该如何处理具有空值的月份?

      【讨论】:

        猜你喜欢
        • 2020-12-21
        • 2010-12-18
        • 2012-02-08
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多