SQL - 分区或循环从主表创建子表[关闭]

Posted

技术标签:

【中文标题】SQL - 分区或循环从主表创建子表[关闭]【英文标题】:SQL - Partition By or Loop to create sub table from master table [closed] 【发布时间】:2021-03-05 13:06:27 【问题描述】:

我创建了一个运行此查询的主表:

主表

WITH cfr_pr_audit AS

(WITH cfr_pr AS
(SELECT
ROW_NUMBER() OVER (ORDER BY id ASC) AS id_seq,
id,
status AS pr_status
FROM infrabi_stg.o_infr_dly_cfr_purchase_request
WHERE snapshot_day = (SELECT max(snapshot_day) FROM infrabi_stg.o_infr_dly_cfr_purchase_request)), 

cfr_pr_audit_pre AS
(SELECT 
purchase_request,
status AS audit_status,
created_by,
created_at
FROM infrabi_stg.o_infr_dly_cfr_purchase_request_audit
WHERE snapshot_day = (SELECT max(snapshot_day) FROM infrabi_stg.o_infr_dly_cfr_purchase_request_audit)
ORDER BY created_at DESC)

SELECT *,
ROW_NUMBER() OVER (PARTITION BY cfr_pr.id,cfr_pr_audit_pre.audit_status ORDER BY created_at DESC) AS row_seq
FROM cfr_pr LEFT JOIN cfr_pr_audit_pre ON cfr_pr.id = cfr_pr_audit_pre.purchase_request
ORDER BY cfr_pr_audit_pre.created_at DESC)

此查询为每个 id 返回多条记录 - 每个 id 大约有 9-11 行,具有不同的审计状态和日期。 (下图是每个 id 的行示例)

我正在尝试创建一个查询,该查询将创建一个新表,每个 id 将有 1 行,并将结合主表中每个 id 的不同详细信息(换句话说,从 9-11 行中选择特定信息)主表中的每个 id 并为每个 id 创建一行)。

我试图在新表中按 id 提取的信息是这样的:

按 id 提取的信息

SELECT DISTINCT
(SELECT id FROM cfr_pr_audit WHERE created_at = (SELECT MIN(created_at) FROM cfr_pr_audit WHERE id = '306637') AND id = '306637') AS purchase_request,
(SELECT MIN(created_at) FROM cfr_pr_audit WHERE id = '306637') AS created_at,
(SELECT created_by FROM cfr_pr_audit WHERE created_at = (SELECT MIN(created_at) FROM cfr_pr_audit WHERE id = '306637') AND id = '306637') AS created_by,
(SELECT created_at FROM cfr_pr_audit WHERE created_at = (SELECT created_at FROM cfr_pr_audit WHERE audit_status = 'Status 8' AND row_seq = 1 AND id = '306637') AND NOT pr_status = 'NO PLACED' AND id = '306637') AS submitted_at,
(SELECT created_by FROM cfr_pr_audit WHERE created_at = (SELECT created_at FROM cfr_pr_audit WHERE audit_status = 'Status 8' AND row_seq = 1 AND id = '306637') AND NOT pr_status = 'NO PLACED' AND id = '306637') AS submitted_by,
ISNULL((SELECT created_at FROM cfr_pr_audit WHERE audit_status = 'Status 9' AND row_seq = 1 AND id = '306637'),(SELECT created_at FROM cfr_pr_audit WHERE audit_status = 'Status 10' AND row_seq = 1 AND id = '306637')) AS approved_at,
(SELECT created_at FROM cfr_pr_audit WHERE audit_status = 'Status 11' AND row_seq = 1 AND id = '306637') AS po_created_at
FROM cfr_pr_audit

上述查询适用于一个特定的 ID(我选择的那个 - 363114)。

我怎样才能使它适用于所有 ID?类似于遍历不同 id 的循环。

我尝试了以下选项:

选项 1:

SELECT DISTINCT
id AS purchase_request,
MIN(created_at) OVER (PARTITION BY id) AS created_at
FROM cfr_pr_audit

partition by 是最合乎逻辑的方法(我通常使用的方法),但是我被困在我试图获得的第三个值中

(SELECT created_by FROM cfr_pr_audit WHERE created_at = (SELECT MIN(created_at) FROM cfr_pr_audit WHERE id = '363114') AND id = '363114') AS created_by

因为我在这里找不到 PARTITION BY 的方法。其余行也是如此 - MIN(created_at) 是我可以按(第二个值)划分的唯一部分

选项 2:

我有做类似循环的事情

DECLARE @i INT = 0
SELECT @count = MAX(id_seq) FROM cfr_pr_audit
WHILE @i <= @count
BEGIN 
  SET @i = @i + 1;

  MIN(created_at) OVER (PARTITION BY purchase_request) AS created_at
END 

但不确定这是否可以工作并创建一个统一的表,以及这是否有效。

非常感谢任何提示。

【问题讨论】:

【参考方案1】:

您可以将条件聚合与窗口函数一起使用:

SELECT DISTINCT id AS purchase_request,
       MIN(created_at) OVER (PARTITION BY id) AS created_at,
       FIRST_VALUE(created_by) OVER (PARTITION BY id ORDER BY created_at) AS created_by,
       MAX(CASE WHEN audit_status = 'Status 8' AND row_seq = 1 AND pr_status <> 'NO PLACED' THEN created_at END) OVER (PARTITION BY id) AS submitted_at,
       MAX(CASE WHEN audit_status = 'Status 8' AND row_seq = 1 AND pr_status <> 'NO PLACED' THEN created_by END) OVER (PARTITION BY id) AS submitted_by,
       ISNULL(MAX(CASE WHEN audit_status = 'Status 9' AND row_seq = 1 THEN created_at END) OVER (PARTITION BY id),
              MAX(CASE WHEN audit_status = 'Status 10' AND row_seq = 1 THEN created_at END) OVER (PARTITION BY id)) AS approved_at,
       MAX(CASE WHEN audit_status = 'Status 11' AND row_seq = 1 THEN created_at END) OVER (PARTITION BY id) AS po_created_at
FROM cfr_pr_audit

【讨论】:

以上是关于SQL - 分区或循环从主表创建子表[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

sql语句从主表删除一条数据,并且把先关联的多个子表数据也删除

为啥在主表上查询很慢?

VBA根据数据值将数据从主表复制并粘贴到另一个表中

我无法将数据从主表复制到临时表

数据库(比如MYSQL) ,表连结查询与子查询哪个效率高些? 为啥

输入详细信息表时,我无法从主表中获取 ID(Laravel 8)