Текущий запрос:
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 в ссылку выше и выбрав «средство просмотра дерева», чтобы следовать моим ссылкам для навигации
Проблема:
Snowflake / ASNI SQL:
Для каждого индекса в массиве мне нужно оценить логическую функцию, которая проверяет ключ "title", и если значение ключа "title" равно X (для этого упражнения давайте выберем "Revenue"), вернуть " имя »и« значения »для каждого индекса ключа« строк ». Под строками: значения у вас есть 2 значения. Их нужно переназначить на MTD и YTD. Они последовательно сохраняются как [0]: MTD и [1]: YTD. Я хочу, чтобы эти новые ключи появились в столбце, который я назвал «периодичность». Я знаю, что это одна сложная проблема сглаживания снежинки, но я боролся с ней все выходные, используя множество разных итераций, и хотел бы посмотреть, сможет ли кто-нибудь дать ей шанс. Спасибо за помощь!
Желаемый результат: CID, TYPE, REPORT_DATE, «Заголовок» Ключ, строки: Имя как «Имя», периодичность, строки: значения: [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