英文:
Snowflake - Calculating days from multiple tables based on specific condition
问题
有人能帮忙编写雪花查询以获得正确结果。我正在处理雪花并有3个表:ZBK、ZLF和ZT05
TAG(整数)是月份的日期。它只用于与日期(inv_dt)进行比较,可以为00
MONA(整数)是月份数,即01表示1个月...
FAEL是月份的日期,即20表示月份的第20天。
TAG1是具有附加天数的列,需要在最终求和计算中添加,不带任何条件。
如果inv_dt小于TAG,则计算天数时应考虑供应商的特定条款,并根据该特定标签行应用计算规则。
例如-对于第一行,Tag是16,而day(inv_dt)是7,小于Tag,则在计算天数时,应取Fael的值,即20,mona为00,并从2月7日到2月20日计算天数,即13天。
对于第二行,Tag是25,而day(inv_dt)是17,小于25但大于同一供应商和条款组的16,则计算应该是Fael即20,mona为01,并从3月17日到4月20日计算天数,即34天(3月14日+4月20日的14天)。
Inv_Dt Vendor | Vendor Term | TAG FAEL MONA Term TAG1
7/02/23 100428 | 100428 AA5 | 16 20 00 AA5 00
17/3/23 100428 | 100429 CA2 | 25 20 01 AA5 07
26/3/23 100428 | | 31 10 02 AA5 15
22/4/23 100429 | | 00 07 02 CA2 5
期望输出(sum_days列)- 仅作参考的列表列。
inv_dt term tag tag1 fael mona sum_days list
2023-02-07 AA5 16 00 20 00 13 13
2023-03-17 AA5 25 07 20 01 41 14+20 + 07(tag1)
2023-03-26 AA5 31 15 10 02 60 5+30+10 + 15(tag1)
2023-04-22 CA2 00 05 7 02 51 8+31+7 + 05(tag1)
我有一个SQL的样本查询如下
与连接一样(
选择ZBK.Inv_Dt,ZLF.TERM
从ZBK
联接ZLF on ZBK.Vendor = ZLF.Vendor)
选择Inv_Dt,TERM,TAG,FAEL,MONA,sum(FAEL) sum_days,
string_agg(FAEL,'+') 在n的排序下(组内)列表
从(
选择Inv_Dt,a.TERM,TAG,FAEL,MONA,n,
案例
当MONA = 0并且FAEL > day(Inv_Dt)时,FAEL - day(Inv_Dt)
当n = 0时,datediff(day,Inv_Dt,last_day(Inv_Dt))
当n = MONA时,FAEL
其他时候,day(last_day(dateadd(month,n,Inv_Dt)))
结束天数
从连接a
十字应用(选择top 1 * from ZT05 b
其中b.TERM = a.TERM and day(Inv_Dt) < TAG顺序为TAG) b,
十字应用(选择n from (values (0),(1),(2),(3),(4),(5)) v(n) 其中n <= MONA) v) t
组按Inv_Dt,TERM,TAG,FAEL,MONA
我正在寻找一种雪花查询。在对此查询进行修改后,运行该查询时出现错误,错误消息为“SQL编译错误:不支持的子查询类型无法评估”。
谢谢
英文:
Can anyone help in snowflake query to get the correct result. I am working on snowflake and have 3 tables as ZBK, ZLF and ZT05
TAG(int) is the day of the month. It is only to compare with day(inv_dt) It can be 00
MONA(int) is the No. of Months i.e. 01 means 1 Month...
FAEL is the day of the month i.e 20 means 20th day of the month.
TAG1 is the column which has additional days which needs to be added in the final sum calculation without any condition.
If the inv_dt is less then TAG then the no. of days calculation should consider specific Term for a vendor and apply the rule of calculation as per that specific tag row.
example - for 1st row, Tag is 16 and day(inv_dt) is 7 which is less then Tag then while calculating no. of days it should take value of Fael i.e. 20 and mona as 00 and count no. of days from 7th Feb to 20th Feb which is 13.
For 2nd row, Tag is 25 and day(inv_dt) is 17 which is less then 25 but greater then 16 of the same vendor and term group. then calculation should be Fael i.e. 20 and mona as 01 and count no. of days from 17th March to 20th April which is 34 (14 day of March + 20 Days of April)
Table ZBK Table ZLF ZT05
Inv_Dt Vendor | Vendor Term | TAG FAEL MONA Term TAG1
7/02/23 100428 | 100428 AA5 | 16 20 00 AA5 00
17/3/23 100428 | 100429 CA2 | 25 20 01 AA5 07
26/3/23 100428 | | 31 10 02 AA5 15
22/4/23 100429 | | 00 07 02 CA2 5
Desired Output (sum_days column) - List column just for reference.
inv_dt term tag tag1 fael mona sum_days list
2023-02-07 AA5 16 00 20 00 13 13
2023-03-17 AA5 25 07 20 01 41 14+20 + 07(tag1)
2023-03-26 AA5 31 15 10 02 60 5+30+10 + 15(tag1)
2023-04-22 CA2 00 05 7 02 51 8+31+7 + 05(tag1)
I have a sample query on SQL as below
with joined as(
select ZBK.Inv_Dt, ZLF.TERM
from ZBK
join ZLF on ZBK.Vendor = ZLF.Vendor)
select Inv_Dt, TERM, TAG, FAEL, MONA, sum(FAEL) sum_days,
string_agg(FAEL, '+') within group (order by n) list
from(
select Inv_Dt, a.TERM, TAG, FAEL, MONA,n,
case
when MONA = 0 and FAEL > day(Inv_Dt) then FAEL - day(Inv_Dt)
when n = 0 then datediff(day, Inv_Dt, last_day(Inv_Dt))
when n = MONA then FAEL
else day(last_day(dateadd(month, n, Inv_Dt)))
end days
from joined a
cross apply(select top 1 * from ZT05 b
where b.TERM = a.TERM and day(Inv_Dt) < TAG order by TAG) b,
cross apply(select n from (values (0),(1),(2),(3),(4),(5)) v(n) where n <= MONA) v) t
group by Inv_Dt, TERM, TAG, FAEL, MONA
I am looking for a query on snowflake.
When I am running this query after modification of string_agg to listagg()
and cross apply to lateral
in snowflake. I am getting error of SQL compilation error: Unsupported subquery type cannot be evaluated
Thank you
答案1
得分: 1
以下是已翻译的代码部分:
这将给您所需的信息:
选择 ZBK.INV_DT
,ZLF.TERM
,T1.TAG, T1.FAEL, T1.MONA
,日期差('天',ZBK.INV_DT,
添加月份(
日期_从_部分(年(ZBK.INV_DT), 月(ZBK.INV_DT), T1.FAEL)
,T1.MONA
)
) "总天数"
从 ZBK
内部连接 ZLF ON ZBK.VENDOR = ZLF.VENDOR
内部连接 ZT05 T1 ON ZLF.TERM = T1.TERM AND T1.TAG =
(选择 MIN(T2.TAG) 从 ZT05 T2 WHERE ZLF.TERM = T2.TERM AND T2.TAG > 天(ZBK.INV_DT) )
按 ZBK.INV_DT 排序
;
英文:
This should give you what you want:
SELECT ZBK.INV_DT
,ZLF.TERM
,T1.TAG, T1.FAEL, T1.MONA
,DATEDIFF('DAYS',ZBK.INV_DT,
ADD_MONTHS(
DATE_FROM_PARTS(YEAR(ZBK.INV_DT), MONTH(ZBK.INV_DT), T1.FAEL)
,T1.MONA
)
) "SUM_DAYS"
FROM ZBK
INNER JOIN ZLF ON ZBK.VENDOR = ZLF.VENDOR
INNER JOIN ZT05 T1 ON ZLF.TERM = T1.TERM AND T1.TAG =
(SELECT MIN(T2.TAG) FROM ZT05 T2 WHERE ZLF.TERM = T2.TERM AND T2.TAG > DAY(ZBK.INV_DT) )
ORDER BY ZBK.INV_DT
;
I've assumed that the tag value has to be greater than the invoice day but if the rule is <= then you'll need to adjust the "T2.TAG > DAY(ZBK.INV_DT)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论