gpt4 book ai didi

mysql - 未使用带有 ID 的Where子句时没有结果集

转载 作者:行者123 更新时间:2023-11-29 18:20:56 25 4
gpt4 key购买 nike

我试图找出为什么当我取出 Account_ID 时我的代码没有返回任何数据。我希望它运行并找到满足连接条件的每个 Account_ID 以及我想要返回的字段。这是在 MySQL 中,我环顾四周并用 Google 搜索,但没有找到类似的东西,或者只是没有正确地放置它。因此,下面是代码,如果有人可以帮助我,我将不胜感激。该代码应为每个 Account_ID 返回 1 行我不习惯使用MySQL。版本 5.6.34 使用 Toad Data-Point 运行 sql 脚本。谢谢丹

    SELECT DISTINCT
pd.ACCOUNT_ID
, do.MEMBER_NAME
, do.GENDER
, MaxDate.EarlyDATE AS ACTIVITY_DATE
, MaxDate.LateDATE AS LAST_ACTIVITY_DATE
, pd.NUMBER_WKS
... more fields after this
Joins below
FROM ZZ_Program_Details pd
LEFT JOIN ZZ_Demographics do
ON do.ACCOUNT_ID = pd.ACCOUNT_ID
INNER JOIN
(SELECT DISTINCT cl.ACCOUNT_ID
, min(cl.activity_date) AS EarlyDATE
, cl1.activity_date AS LateDATE
, cl1.scheduling_selection
, cl.ATND_STATUS_THIS_INTERVAL
, cl.WEIGHT_STATUS_FOR_INTERVAL
, TRUNCATE(cl.WEIGHT_WHEN_ENROLLED,2) AS WEIGHT_WHEN_ENROLLED
, TRUNCATE(cl.WEIGHT_AT_INTERVAL_END,2) AS WEIGHT_AT_INTERVAL_END
FROM ZZ_Class_Attendance cl
INNER JOIN ZZ_Class_Attendance cl1
ON cl1.account_id = cl.ACCOUNT_ID
WHERE cl1.ACTIVITY_DATE > cl.ACTIVITY_DATE
/*and cl.account_id = '47F74C65BA8BB02DE053BC010B0AF714'*/
) AS MaxDate
INNER JOIN
(SELECT ESRA.healthfleet_member_id
, ESRA.Sponsor
, ESRA.uhc_region
, ESRA.uhc_major_market
, ESRA.uhc_minor_market AS ESRA_UHC_MINOR_MARKET
, ESRA.policy_number AS ESRA_POLICY_NBR
, ESRA.relationship_code
, sf.Market_Segment
, sf.UHC_Minor_Market AS SF_UHC_MINOR_MARKET
, sf.CLIENT_NAME
, sf.Client_Owner
, sf.Payer_Effective_Date
FROM reporting_adhoc.ZZ_ESRA_People ESRA
LEFT JOIN reporting_adhoc.ZZ_Salesforce sf
ON sf.Policy_Number = ESRA.policy_number
/*WHERE ESRA.healthfleet_member_id='47F74C65BA8BB02DE053BC010B0AF714'*/
GROUP BY ESRA.healthfleet_member_id
, ESRA.Sponsor, ESRA.uhc_region
, ESRA.uhc_major_market
, ESRA.uhc_minor_market
, ESRA.relationship_code
, sf.Market_Segment
, sf.UHC_Minor_Market
, sf.CLIENT_NAME
, sf.Client_Owner, ESRA.policy_number
, sf.Payer_Effective_Date
) ESRA_DATA

ON MaxDate.ACCOUNT_ID = pd.ACCOUNT_ID
AND ESRA_DATA.healthfleet_member_id = pd.ACCOUNT_ID

/*WHERE pd.account_id = '47F74C65BA8BB02DE053BC010B0AF714'* --with this
it works fine */
WHERE pd.account_id > 0 --an attempt with getting all account_ids

GROUP BY
pd.ACCOUNT_ID
, do.MEMBER_NAME
, do.GENDER
, MaxDate.EarlyDATE
, MaxDate.LateDATE
... rest of fields
ORDER BY pd.ACCOUNT_ID

最佳答案

如果无法访问您的表格和数据,我们可能无法提出明确的答案,并且由于您的查询缺少部分,这使其更像是一个猜谜游戏。

让我们从这里开始:

SELECT DISTINCT  ---------- bad bad
pd.ACCOUNT_ID
, MaxDate.EarlyDATE AS ACTIVITY_DATE
, MaxDate.LateDATE AS LAST_ACTIVITY_DATE

FROM ZZ_Program_Details pd
LEFT JOIN ZZ_Demographics do ON do.ACCOUNT_ID = pd.ACCOUNT_ID
INNER JOIN (
SELECT DISTINCT ---------- bad bad
cl.ACCOUNT_ID
, MIN(cl.activity_date) AS EarlyDATE
, cl1.activity_date AS LateDATE
FROM ZZ_Class_Attendance cl
INNER JOIN ZZ_Class_Attendance cl1 ON cl1.account_id = cl.ACCOUNT_ID
WHERE cl1.ACTIVITY_DATE > cl.ACTIVITY_DATE

-- no GROUP BY, so how do your get MIN() or MAX() ?

) AS MaxDate ON
( WHAT???? )

首先将大查询分解为更小的 block ,如上所示。 此部分查询有效吗? 可能不会,因为您尝试使用 DISTINCT 而不是 GROUP BY 并且连接条件也未知。

另请注意,您使用了 INNER JOIN。如果该子查询 MaxDate 无法返回任何匹配的行,则整个查询也将不会返回行。重新考虑每个 INNER JOIN(也许尝试将它们作为 LEFT JOINS)。

一旦部分查询开始工作,请添加下一个联接并将相关列添加到顶部 select 子句。处理查询的这一部分,直到成功,然后移至下一部分并重复,直到获得有效的查询。

请尽量避免在涉及多个连接的复杂查询中使用“select unique”。这不是一个好的做法。

<小时/>

您可能会发现这些查询将帮助您确定哪些表(或子查询)需要左连接:

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
INNER JOIN ZZ_Demographics do ON pd.ACCOUNT_ID = do.ACCOUNT_ID
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
LEFT JOIN ZZ_Demographics do ON pd.ACCOUNT_ID = do.ACCOUNT_ID
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
INNER JOIN ZZ_Class_Attendance cl ON pd.ACCOUNT_ID = cl.ACCOUNT_ID
INNER JOIN ZZ_Class_Attendance cl1 ON cl1.account_id = cl.ACCOUNT_ID
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
LEFT JOIN ZZ_Class_Attendance cl ON pd.ACCOUNT_ID = cl.ACCOUNT_ID
LEFT JOIN ZZ_Class_Attendance cl1 ON cl1.account_id = cl.ACCOUNT_ID
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
INNER JOIN reporting_adhoc.ZZ_ESRA_People ESRA ON pd.ACCOUNT_ID = ESRA.healthfleet_member_id
INNER JOIN reporting_adhoc.ZZ_Salesforce sf ON sf.Policy_Number = ESRA.policy_number
;

SELECT COUNT(distinct pd.ACCOUNT_ID) acct_c, COUNT(*) row_c
FROM ZZ_Program_Details pd
LEFT JOIN reporting_adhoc.ZZ_ESRA_People ESRA ON pd.ACCOUNT_ID = ESRA.healthfleet_member_id
LEFT JOIN reporting_adhoc.ZZ_Salesforce sf ON sf.Policy_Number = ESRA.policy_number
;

关于mysql - 未使用带有 ID 的Where子句时没有结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com