在 IF 语句中嵌套展平;重新键入多层展平

Posted

技术标签:

【中文标题】在 IF 语句中嵌套展平;重新键入多层展平【英文标题】:Nested flatten in IF statement; re-keying in multi layer flatten 【发布时间】:2020-09-13 14:56:02 【问题描述】:

当前查询:

with
report as (

  select * from Report_Data
  where type = 'pnl'
  and (created_at > '2020-01-01' or updated_at > '2020-01-01')

    ),
flattened as (

    select 
        report.company_ID as "CID",
        report.type,
        report.from_date as "Report_Date",
        f.*
        --f.value:"title"




    from report,
      lateral flatten(input =>prepared:profitAndLoss:report:sections) f
      --,lateral flatten(input =>f.value)f1

)
select * from flattened where CID = 908 limit 100;

f 的电流输出:

[
  
    "rows": [
      
        "emphasize": false,
        "name": "Product Sales",
        "values": [
          
            "rawValue": 19889.16,
            "value": "19,889"
          ,
          
            "rawValue": 56181.94,
            "value": "56,182"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Credits from Vendors",
        "values": [
          
            "rawValue": 38,
            "value": "38"
          ,
          
            "rawValue": 47.16,
            "value": "47"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Sales - PayPal",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 275,
            "value": "275"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Sales Returns and Allowances",
        "values": [
          
            "rawValue": -379.28,
            "value": "(379)"
          ,
          
            "rawValue": -941.78,
            "value": "(942)"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Revenue",
      "values": [
        
          "rawValue": 19547.88,
          "value": "19,548"
        ,
        
          "rawValue": 55562.32,
          "value": "55,562"
        
      ]
    ,
    "title": "Revenue"
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Product Costs",
        "values": [
          
            "rawValue": 9857.22,
            "value": "9,857"
          ,
          
            "rawValue": 31907.32,
            "value": "31,907"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Supplies",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 109.92,
            "value": "110"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Cost of Sales",
      "values": [
        
          "rawValue": 9857.22,
          "value": "9,857"
        ,
        
          "rawValue": 32017.24,
          "value": "32,017"
        
      ]
    ,
    "title": "Less Cost of Sales"
  ,
  
    "rows": [
      
        "emphasize": true,
        "name": "Gross Profit",
        "values": [
          
            "rawValue": 9690.66,
            "value": "9,691"
          ,
          
            "rawValue": 23545.08,
            "value": "23,545"
          
        ]
      
    ],
    "summary": 
      "name": "",
      "values": []
    ,
    "title": ""
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Advertising & Marketing",
        "values": [
          
            "rawValue": 100,
            "value": "100"
          ,
          
            "rawValue": 100,
            "value": "100"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Advertising Expenses",
        "values": [
          
            "rawValue": 22,
            "value": "22"
          ,
          
            "rawValue": 689,
            "value": "689"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Bank Fees",
        "values": [
          
            "rawValue": 53,
            "value": "53"
          ,
          
            "rawValue": 53,
            "value": "53"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Business Telephone and Internet",
        "values": [
          
            "rawValue": 10.08,
            "value": "10"
          ,
          
            "rawValue": 30.24,
            "value": "30"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Charitable Contributions",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": -81.89,
            "value": "(82)"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Computer and Software Expense",
        "values": [
          
            "rawValue": 20,
            "value": "20"
          ,
          
            "rawValue": 20,
            "value": "20"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Dues and Subscriptions",
        "values": [
          
            "rawValue": 0.99,
            "value": "1"
          ,
          
            "rawValue": 135.99,
            "value": "136"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Fees and Licenses",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 50,
            "value": "50"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Insurance Expense - General Liability",
        "values": [
          
            "rawValue": 101.6,
            "value": "102"
          ,
          
            "rawValue": 304.8,
            "value": "305"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Legal & Professional",
        "values": [
          
            "rawValue": 200,
            "value": "200"
          ,
          
            "rawValue": 600,
            "value": "600"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Merchant Account Fees",
        "values": [
          
            "rawValue": 444.91,
            "value": "445"
          ,
          
            "rawValue": 1221.71,
            "value": "1,222"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Office Expenses",
        "values": [
          
            "rawValue": 169.66,
            "value": "170"
          ,
          
            "rawValue": 988.42,
            "value": "988"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Payroll Processing Fee",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 0.45,
            "value": "0"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Rent",
        "values": [
          
            "rawValue": 1500,
            "value": "1,500"
          ,
          
            "rawValue": 6000,
            "value": "6,000"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Taxes",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 3000,
            "value": "3,000"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Travel",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 194.35,
            "value": "194"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Utilities",
        "values": [
          
            "rawValue": 559.59,
            "value": "560"
          ,
          
            "rawValue": 1769.63,
            "value": "1,770"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Operating Expenses",
      "values": [
        
          "rawValue": 3181.83,
          "value": "3,182"
        ,
        
          "rawValue": 15075.7,
          "value": "15,076"
        
      ]
    ,
    "title": "Operating Expenses"
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Operating Income / (Loss)",
        "values": [
          
            "rawValue": 6508.83,
            "value": "6,509"
          ,
          
            "rawValue": 8469.38,
            "value": "8,469"
          
        ]
      
    ],
    "summary": 
      "name": "",
      "values": []
    ,
    "title": ""
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Interest",
        "values": [
          
            "rawValue": -85.44,
            "value": "(85)"
          ,
          
            "rawValue": -434.74,
            "value": "(435)"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Interest Income",
        "values": [
          
            "rawValue": 0.57,
            "value": "1"
          ,
          
            "rawValue": 1.24,
            "value": "1"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Other Income",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 100,
            "value": "100"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Other Income and Expense",
      "values": [
        
          "rawValue": -84.87,
          "value": "(85)"
        ,
        
          "rawValue": -333.5,
          "value": "(334)"
        
      ]
    ,
    "title": "Other Income and Expense"
  ,
  
    "rows": [
      
        "emphasize": true,
        "name": "Net Income  / (Loss) before Tax",
        "values": [
          
            "rawValue": 6423.96,
            "value": "6,424"
          ,
          
            "rawValue": 8135.88,
            "value": "8,136"
          
        ]
      
    ],
    "summary": 
      "name": "",
      "values": []
    ,
    "title": ""
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Net Income",
        "values": [
          
            "rawValue": 6423.96,
            "value": "6,424"
          ,
          
            "rawValue": 8135.88,
            "value": "8,136"
          
        ]
      
    ],
    "summary": 
      "name": "",
      "values": []
    ,
    "title": ""
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Total Comprehensive Income",
        "values": [
          
            "rawValue": 6423.96,
            "value": "6,424"
          ,
          
            "rawValue": 8135.88,
            "value": "8,136"
          
        ]
      
    ],
    "summary": 
      "name": "",
      "values": []
    ,
    "title": ""
  
]

https://codebeautify.org/jsonviewer

将 c/p JSON 推荐到上面的链接中并选择“树查看器”以跟随我的导航参考

问题:

雪花/ASNI SQL:

对于数组中的每个索引,我需要评估一个检查“title”键的逻辑函数,如果“title”键的值为 X(为了本练习,我们选择“Revenue”),返回“行”键的每个索引的“名称”和“值”。在 rows:values 下,您有 2 个值。它们需要重新键入 MTD 和 YTD。它们始终存储为 [0]:MTD 和 [1]:YTD。我希望这些新键出现在我命名为“周期性”的列中。我知道这只是雪花中一个棘手的扁平化问题,但我整个周末都在多次不同的迭代中苦苦挣扎,想看看是否有人可以试一试。感谢您的帮助!

期望的输出: CID, TYPE, REPORT_DATE, "Title" Key, rows:Name as "Name", 周期性, rows:values:[0]:rawValue

编辑:带有值的示例输出列:

      CID    TYPE    REPORT_DATE    "Title" Key     rows:Name as "Name" 
       47    PNL      2020-01-01         REVENUE      "Sales - PayPal"

   periodicity       rows:values:[0]:rawValue
      "MTD"                        0

【问题讨论】:

如果您的示例 json 只有“一个”块来关心“收入”,我可以建议您需要另一个不是“收入”的块来展示数据的混合性质。此外,虽然您已经在输出中描述了所需列的名称,但您还没有将您希望在示例中看到的值放入示例中。这将允许人们弥合您的描述和您想要的结果之间的差距。 您是否建议 prepared:profitAndLoss:report:sections 是附加的 JSON,或者实际上 f.* 的结果是 JSON,因为 f.* 的输出看起来不像您的 JSON,这让我想知道什么你认为你的 JSON 试图代表。 已编辑以显示示例输出,您成功了,谢谢。其他块在其他“title”键下,例如“title”:“Operating Expenses”。至于prepared:profitAndLoss:report:sections,这不是我能够将JSON从原始范围缩小到的。示例 JSON 是需要展平的。 所以如果 json 已经“正确”展平,还有什么问题吗? 不!你明白了! 【参考方案1】:

所以压扁不会太难。如果你一步一步地走:

with random_json as (
select '2020-05-27'::date as report_date, parse_json('[
  
    "rows": [
      
        "emphasize": false,
        "name": "Product Sales",
        "values": [
          
            "rawValue": 19889.16,
            "value": "19,889"
          ,
          
            "rawValue": 56181.94,
            "value": "56,182"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Credits from Vendors",
        "values": [
          
            "rawValue": 38,
            "value": "38"
          ,
          
            "rawValue": 47.16,
            "value": "47"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Sales - PayPal",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 275,
            "value": "275"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Sales Returns and Allowances",
        "values": [
          
            "rawValue": -379.28,
            "value": "(379)"
          ,
          
            "rawValue": -941.78,
            "value": "(942)"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Revenue",
      "values": [
        
          "rawValue": 19547.88,
          "value": "19,548"
        ,
        
          "rawValue": 55562.32,
          "value": "55,562"
        
      ]
    ,
    "title": "Revenue"
  ,
  
    "rows": [
      
        "emphasize": false,
        "name": "Product Costs",
        "values": [
          
            "rawValue": 9857.22,
            "value": "9,857"
          ,
          
            "rawValue": 31907.32,
            "value": "31,907"
          
        ]
      ,
      
        "emphasize": false,
        "name": "Supplies",
        "values": [
          
            "rawValue": 0,
            "value": "0"
          ,
          
            "rawValue": 109.92,
            "value": "110"
          
        ]
      
    ],
    "summary": 
      "emphasize": false,
      "name": "Total Cost of Sales",
      "values": [
        
          "rawValue": 9857.22,
          "value": "9,857"
        ,
        
          "rawValue": 32017.24,
          "value": "32,017"
        
      ]
    ,
    "title": "Less Cost of Sales"
  
]') as json
), flatten_a AS (
select
        r.report_date
        ,f.value:rows as r_rows
        ,f.value:summary as r_summary
        ,f.value:title as r_title
    from random_json r
    ,lateral flatten(input=>r.json) f
), flatten_b AS (
select a.*
   , f.value:name as rr_name
   , f.value:values as rr_values
   from flatten_a a
   ,lateral flatten(input=>a.r_rows) f
)
select b.report_date
    ,b.r_title as title_key
    ,b.rr_name as name
    ,case when f.index =0 then 'MTD' when f.index=1 then 'YTD' else 'WTF' end as periodicity
    , f.value:rawValue raw_value
   from flatten_b b
   ,lateral flatten(input=>b.rr_values) f;

给予:

REPORT_DATE    TITLE_KEY              NAME                            PERIODICITY    RAW_VALUE
2020-05-27    "Revenue"              "Product Sales"                  MTD            19889.16
2020-05-27    "Revenue"              "Product Sales"                  YTD            56181.94
2020-05-27    "Revenue"              "Credits from Vendors"           MTD            38
2020-05-27    "Revenue"              "Credits from Vendors"           YTD            47.16
2020-05-27    "Revenue"              "Sales - PayPal"                 MTD            0
2020-05-27    "Revenue"              "Sales - PayPal"                 YTD            275
2020-05-27    "Revenue"              "Sales Returns and Allowances"   MTD            -379.28
2020-05-27    "Revenue"              "Sales Returns and Allowances"   YTD            -941.78
2020-05-27    "Less Cost of Sales"   "Product Costs"                  MTD            9857.22
2020-05-27    "Less Cost of Sales"   "Product Costs"                  YTD            31907.32
2020-05-27    "Less Cost of Sales"   "Supplies"                       MTD            0
2020-05-27    "Less Cost of Sales"   "Supplies"                       YTD            109.92

如果您在第一句中提到的仅希望上述“收入”行使结果集也成为 CTE,并左加入您的“报告”和 ON 过滤器以获取收入。

【讨论】:

以上是关于在 IF 语句中嵌套展平;重新键入多层展平的主要内容,如果未能解决你的问题,请参考以下文章

一日一技:如何把多层嵌套的列表展平

转载 | Python中多层List展平为一层

7.2.5 多层嵌套的if语句

r语言repeat语句内嵌套多层if语句的代码?

转载 | Python中多层List展平为一层

转载 | Python中多层List展平为一层