SQL查询,是否需要GROUP BY?如何避免不必要的复杂语法
SQL query, is GROUP BY needed? how to avoid unnecessarily complicated syntax
我正在构建一个查询,该查询将基于 4 tables 构建一个非常大的数据集。到目前为止,这是我的查询:
SELECT
CLIENT_CA."Requisition_ID",
CLIENT_REQS."Requisition Title",
CLIENT_REQS."Country",
CLIENT_CD."Application_Status",
CLIENT_CA."Candidate",
CLIENT_CD."Gender",
max(case when
CLIENT_CA."Process_of_Activity" = 'Application Entry' then
CLIENT_CA."Completed_on" end) as "ENTRY",
max(case when
CLIENT_CA."Process_of_Activity" = 'Candidate Selection' then
CLIENT_CA."Completed_on" end) as "Selection",
max(case when
CLIENT_CA."Process_of_Activity" = 'Job Offer' then
CLIENT_CA."Completed_on" end) as "Offer",
CLIENT_RR."Status_Reason(E-Rec)"
FROM CLIENT_CA
LEFT JOIN CLIENT_CD ON (CLIENT_CA."Candidate_ID"=CLIENT_CD."Candidate_ID"
and CLIENT_CA."Requisition_ID"=CLIENT_CD."Requisition_ID" )
LEFT JOIN CLIENT_REQS ON CLIENT_REQS."Requisition_ID"=CLIENT_CA."Requisition_ID"
LEFT JOIN CLIENT_RR
ON (CLIENT_CA."Candidate_ID"=CLIENT_RR."Candidate_ID" and CLIENT_CA."Requisition_ID"=CLIENT_RR."Requisition_ID" )
GROUP BY CLIENT_CA."Candidate"
,CLIENT_CA."Requisition_ID"
,CLIENT_CD."Gender"
,CLIENT_REQS."Requisition Title"
,CLIENT_REQS."Country"
,CLIENT_RR."Status_Reason(E-Rec)"
,CLIENT_CD."Application_Status";
从技术上讲,这个查询工作得很好,准确无误地提供了我需要的信息。问题是我需要在 SELECT 子句中添加大约 80 列,而系统或更确切地说是逻辑迫使我将我添加的每一列都放在 GROUP BY 子句中......我猜这是因为 "max" 函数。基本上 CLIENT_CA 是我的 "base" table,我正在尝试实现在 MS Excel 中由其他 3 table 上的 VLOOKUP 函数完成的功能。我只是有点担心我让这件事变得有点不必要的复杂。请毫不犹豫地告诉我,我只是个愚蠢的人 :) 但前提是您有建议如何做得更好 :) 谢谢。哦....我在 dashDB 上 运行 这个。
使用子查询从主查询中提取 table,连接列和计算列(条目、选择、报价),这样您就可以将分组列限制为固定数量
SELECT
CA."Requisition_ID",
REQS."Requisition Title",
REQS."Country",
CD."Application_Status",
CA."Candidate",
CD."Gender",
"ENTRY",
"Selection",
"Offer",
RR."Status_Reason(E-Rec)"
FROM (
SELECT
"Requisition_ID", "Candidate_ID", "Candidate",
max(case when "Process_of_Activity" = 'Application Entry' then "Completed_on" end) as "ENTRY",
max(case when "Process_of_Activity" = 'Candidate Selection' then "Completed_on" end) as "Selection",
max(case when "Process_of_Activity" = 'Job Offer' then "Completed_on" end) as "Offer"
FROM CLIENT_CA
GROUP BY "Candidate", "Requisition_ID", "Candidate_ID"
) AS CA
LEFT JOIN CLIENT_CD AS CD ON (CA."Candidate_ID"=CD."Candidate_ID" and CA."Requisition_ID"=CD."Requisition_ID" )
LEFT JOIN CLIENT_REQS AS REQS ON REQS."Requisition_ID"=CA."Requisition_ID"
LEFT JOIN CLIENT_RR AS RR ON (CA."Candidate_ID"=RR."Candidate_ID" and CA."Requisition_ID"=RR."Requisition_ID" )
我正在构建一个查询,该查询将基于 4 tables 构建一个非常大的数据集。到目前为止,这是我的查询:
SELECT
CLIENT_CA."Requisition_ID",
CLIENT_REQS."Requisition Title",
CLIENT_REQS."Country",
CLIENT_CD."Application_Status",
CLIENT_CA."Candidate",
CLIENT_CD."Gender",
max(case when
CLIENT_CA."Process_of_Activity" = 'Application Entry' then
CLIENT_CA."Completed_on" end) as "ENTRY",
max(case when
CLIENT_CA."Process_of_Activity" = 'Candidate Selection' then
CLIENT_CA."Completed_on" end) as "Selection",
max(case when
CLIENT_CA."Process_of_Activity" = 'Job Offer' then
CLIENT_CA."Completed_on" end) as "Offer",
CLIENT_RR."Status_Reason(E-Rec)"
FROM CLIENT_CA
LEFT JOIN CLIENT_CD ON (CLIENT_CA."Candidate_ID"=CLIENT_CD."Candidate_ID"
and CLIENT_CA."Requisition_ID"=CLIENT_CD."Requisition_ID" )
LEFT JOIN CLIENT_REQS ON CLIENT_REQS."Requisition_ID"=CLIENT_CA."Requisition_ID"
LEFT JOIN CLIENT_RR
ON (CLIENT_CA."Candidate_ID"=CLIENT_RR."Candidate_ID" and CLIENT_CA."Requisition_ID"=CLIENT_RR."Requisition_ID" )
GROUP BY CLIENT_CA."Candidate"
,CLIENT_CA."Requisition_ID"
,CLIENT_CD."Gender"
,CLIENT_REQS."Requisition Title"
,CLIENT_REQS."Country"
,CLIENT_RR."Status_Reason(E-Rec)"
,CLIENT_CD."Application_Status";
从技术上讲,这个查询工作得很好,准确无误地提供了我需要的信息。问题是我需要在 SELECT 子句中添加大约 80 列,而系统或更确切地说是逻辑迫使我将我添加的每一列都放在 GROUP BY 子句中......我猜这是因为 "max" 函数。基本上 CLIENT_CA 是我的 "base" table,我正在尝试实现在 MS Excel 中由其他 3 table 上的 VLOOKUP 函数完成的功能。我只是有点担心我让这件事变得有点不必要的复杂。请毫不犹豫地告诉我,我只是个愚蠢的人 :) 但前提是您有建议如何做得更好 :) 谢谢。哦....我在 dashDB 上 运行 这个。
使用子查询从主查询中提取 table,连接列和计算列(条目、选择、报价),这样您就可以将分组列限制为固定数量
SELECT
CA."Requisition_ID",
REQS."Requisition Title",
REQS."Country",
CD."Application_Status",
CA."Candidate",
CD."Gender",
"ENTRY",
"Selection",
"Offer",
RR."Status_Reason(E-Rec)"
FROM (
SELECT
"Requisition_ID", "Candidate_ID", "Candidate",
max(case when "Process_of_Activity" = 'Application Entry' then "Completed_on" end) as "ENTRY",
max(case when "Process_of_Activity" = 'Candidate Selection' then "Completed_on" end) as "Selection",
max(case when "Process_of_Activity" = 'Job Offer' then "Completed_on" end) as "Offer"
FROM CLIENT_CA
GROUP BY "Candidate", "Requisition_ID", "Candidate_ID"
) AS CA
LEFT JOIN CLIENT_CD AS CD ON (CA."Candidate_ID"=CD."Candidate_ID" and CA."Requisition_ID"=CD."Requisition_ID" )
LEFT JOIN CLIENT_REQS AS REQS ON REQS."Requisition_ID"=CA."Requisition_ID"
LEFT JOIN CLIENT_RR AS RR ON (CA."Candidate_ID"=RR."Candidate_ID" and CA."Requisition_ID"=RR."Requisition_ID" )