将子查询更改为连接的一种类型

huangapple go评论65阅读模式
英文:

Change Subquery to a type of JOIN

问题

过去几天,我一直在尝试将子查询转换为内连接,因为我需要将SQL代码迁移到BigQuery,而后者不支持子查询。

这是原始查询:

SELECT order_id_int, order_number_vc, 
(SELECT TOP 1 create_dt FROM lifecycle_tbl life WHERE life.recipient_package_id_fk_int = pak.recipient_package_id_int AND status_vc = 'Deleted' ORDER BY lifecycle_id_int DESC) AS package_archive_dt,
FROM recipient_package_tbl pak

我已将其更改如下:

SELECT order_id_int, order_number_vc
FROM recipient_package_tbl pak
INNER JOIN lifecycle_tbl life
ON life.recipient_package_id_fk_int = pak.recipient_package_id_int
WHERE life.status_vc = 'Deleted'

显然,使用 TOP 1 和 ORDER BY DESC,我只需要获取第一个值并将其添加到查询中(通过子查询)。然而,如果我直接在INNER JOIN中这样做,它会过滤整个查询。

如果有人知道如何正确进行更改,我将非常感激。

英文:

For the past few days, I have been trying to convert a subquery to an Inner Join since I need to migrate an SQL code to BigQuery, and the latter does not support subqueries.

This is the original query:

SELECT order_id_int, order_number_vc, 
(SELECT TOP 1 create_dt FROM lifecycle_tbl life WHERE life.recipient_package_id_fk_int = pak.recipient_package_id_int AND status_vc = 'Deleted' ORDER BY lifecycle_id_int DESC) AS package_archive_dt,
FROM recipient_package_tbl pak

I have changed it like this:

SELECT order_id_int, order_number_vc
FROM recipient_package_tbl pak
INNER JOIN lifecycle_tbl life
ON life.recipient_package_id_fk_int = pak.recipient_package_id_int
WHERE life.status_vc = 'Deleted'

Clearly, with a TOP 1 and an ORDER BY DESC, I need to only take the first value to add it to the query (through a subquery). However, if I do that directly in the INNER JOIN, it filters the entire query.

If somebody knows how to make the change correctly I will thank you so much.

答案1

得分: 0

以下是翻译好的部分:

"Could something like this work for you? I used ROW_NUMBER, and broke it out into two steps for clarity.

with subquery as (
SELECT life.recipient_package_id_fk_int, create_dt, row_number() over(partition by life.recipient_package_id_fk_int ORDER BY lifecycle_id_int DESC) as rn
FROM lifecycle_tbl life
WHERE status_vc = 'Deleted'
), subquery_fitlered as (
SELECT recipient_package_id_fk_int, create_dt as package_archive_dt
FROM subquery
WHERE rn = 1
)
SELECT order_id_int, order_number_vc,
package_archive_dt
FROM recipient_package_tbl pak
LEFT JOIN subquery_fitlered
ON subquery_fitlered.recipient_package_id_fk_int = pak.recipient_package_id_int

英文:

Could something like this work for you? I used ROW_NUMBER, and broke it out into two steps for clarity.

with subquery as (
    SELECT life.recipient_package_id_fk_int, create_dt, row_number() over(partition by life.recipient_package_id_fk_int ORDER BY lifecycle_id_int DESC) as rn
    FROM lifecycle_tbl life 
    WHERE status_vc = 'Deleted' 
), subquery_fitlered as (
    SELECT recipient_package_id_fk_int, create_dt as package_archive_dt
    FROM subquery
    WHERE rn = 1
)
SELECT order_id_int, order_number_vc, 
 package_archive_dt
FROM recipient_package_tbl pak
LEFT JOIN subquery_fitlered 
ON subquery_fitlered.recipient_package_id_fk_int = pak.recipient_package_id_int

huangapple
  • 本文由 发表于 2023年6月21日 23:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定