相同的代码随着子查询中的子句的更改提供了不同的结果SQL
我试图找到两个相似的代码段之间的区别,因为它给出了不同的结果。
在下面的代码中,计数为15506,
select
application_ID,
userid,
product,
sub_product,
ts_est,
event_type from
(
Select
fa.ApplicationId as application_ID,
ro.userid,
ro.product,
ro.sub_product,
ro.ts_est,
ro.event_type,
from_utc_timestamp(fa.bornondate, 'America/New_York') as app_bornondate,
row_number() over(
partition by ro.userid, ro.product, ro.event_type
order by
fa.bornondate desc
) as rowid
from
T1 ro
left join T2 fa on ro.userid = fa.userid
where event_type = "OS"
and cast(ts_est as date) >= '2021-10-05' and cast(ts_est as date) <= '2022-03-07' and product = "pt"
)
where rowid = 1
这是我使用了子查询以外的条件的第二代码段。 计数为15096
select
application_ID,
userid,
product,
sub_product,
ts_est,
event_type from
(
Select
fa.ApplicationId as application_ID,
ro.userid,
ro.product,
ro.sub_product,
ro.ts_est,
ro.event_type,
from_utc_timestamp(fa.bornondate, 'America/New_York') as app_bornondate,
row_number() over(
partition by ro.userid, ro.product, ro.event_type
order by
fa.bornondate desc
) as rowid
from
T1 ro
left join T2 fa on ro.userid = fa.userid
)
where rowid = 1 and cast(ts_est as date) >= '2021-10-05'
and cast(ts_est as date) <= '2022-03-07' and product = "pt" and event_type = "OS"
当我为这两个代码使用负操作时, 。它显示出410个差异,但在两个表中都存在userID,这很奇怪。
我是SQL的新手,但是我很难做到这一点。
谢谢!
I am trying to find the difference between 2 code snippets that are similar since it is giving different results.
In the code below, The count is 15506
select
application_ID,
userid,
product,
sub_product,
ts_est,
event_type from
(
Select
fa.ApplicationId as application_ID,
ro.userid,
ro.product,
ro.sub_product,
ro.ts_est,
ro.event_type,
from_utc_timestamp(fa.bornondate, 'America/New_York') as app_bornondate,
row_number() over(
partition by ro.userid, ro.product, ro.event_type
order by
fa.bornondate desc
) as rowid
from
T1 ro
left join T2 fa on ro.userid = fa.userid
where event_type = "OS"
and cast(ts_est as date) >= '2021-10-05' and cast(ts_est as date) <= '2022-03-07' and product = "pt"
)
where rowid = 1
Here is the 2nd code snippet where I have used the conditions outside the subquery. The count is 15096
select
application_ID,
userid,
product,
sub_product,
ts_est,
event_type from
(
Select
fa.ApplicationId as application_ID,
ro.userid,
ro.product,
ro.sub_product,
ro.ts_est,
ro.event_type,
from_utc_timestamp(fa.bornondate, 'America/New_York') as app_bornondate,
row_number() over(
partition by ro.userid, ro.product, ro.event_type
order by
fa.bornondate desc
) as rowid
from
T1 ro
left join T2 fa on ro.userid = fa.userid
)
where rowid = 1 and cast(ts_est as date) >= '2021-10-05'
and cast(ts_est as date) <= '2022-03-07' and product = "pt" and event_type = "OS"
When I am using a minus operation for the two codes. It is showing 410 differences but the userid is present in both the tables which is strange.
I am new to sql and I'm having trouble taking this further though.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论