相同的代码随着子查询中的子句的更改提供了不同的结果SQL

发布于 2025-02-11 05:34:42 字数 1485 浏览 0 评论 0原文

我试图找到两个相似的代码段之间的区别,因为它给出了不同的结果。

在下面的代码中,计数为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文