简体   繁体   English

如何:来自互斥的多个 CTE 的 SELECT

[英]How to: SELECT from multiple CTEs that are mutually exclusive

I am in a situation where I need to show stats of a user based on referrals that user made & then stats about those referred users' activities.我处于一种情况,我需要根据用户进行的推荐显示用户的统计信息,然后显示有关这些被推荐用户活动的统计信息。 So, if user A refers user B, C then it means user A referred 2 users.因此,如果用户 A 引用用户 B,C 则表示用户 A 引用了 2 个用户。 Now, I need to get how many of those referred users did take an action ie PURCHASED.现在,我需要知道有多少referred的用户确实采取了行动,即购买。 But they may not purchase which counts to 0. I have following query using CTEs which does work as expected but it also returns some false negative results.但是他们可能不会购买计数为 0 的东西。我使用 CTE 进行了以下查询,它确实按预期工作,但它也返回了一些假阴性结果。 ie IE


    WITH direct_referrals AS (
        SELECT id
        FROM "user"
        WHERE "user"."referredBy" = ${userId}
    ),
    application_stats AS (
        SELECT count(status), status
        FROM "application"
        WHERE "userId" IN (SELECT id FROM direct_referrals)
        GROUP BY status
    )
          
    SELECT *, (SELECT count(id) FROM direct_referrals) AS "totalReferrals" 
    FROM application_stats;

This query returns correct result if at-least 1 referred user took some action but it fails when none has taken any action in which case this query returns the referred users to be 0 which is not true.如果至少有 1 个被推荐的用户采取了一些操作,则此查询返回正确的结果,但当没有人采取任何操作时它会失败,在这种情况下,此查询将referred的用户返回为 0,这是不正确的。

I can see that SELECT is dependent on application_stats CTE which may not return any result & hence the direct_referrals are also not returned.我可以看到SELECT依赖于application_stats CTE,它可能不会返回任何结果,因此也不会返回direct_referrals I am somewhat new to SQL so don't really know many options.我对 SQL 有点陌生,所以真的不知道很多选择。 Any help is appreciated.任何帮助表示赞赏。

Thanks谢谢

Update with sample Data & Expected Results更新样本数据和预期结果

// User model // 用户 model

Id    username    referredBy
----  --------  -------------------
1     jon       NULL
2     jane      1
3     doe       1
4     smith     2
5     john      1

// Application model // 应用 model

Id    userId    status
----  --------  -------------------
1     12       'APPLIED'
2     13       'APPLIED'
3     14       'VIEWED'

Expected Result (for userId = 1):预期结果(对于 userId = 1):

User (referral) stats  Application stats
-------------------    -------------------
3                      0

Actual Result:实际结果:

User (referral) stats  Application stats
-------------------    -------------------
0                      0

Something like this should give you what you want:像这样的东西应该给你你想要的:

WITH REFERRAL AS (
    SELECT REFERREDBY, COUNT(USERNAME) AS "REF_CNT"
    FROM  USER_MODEL
    WHERE REFERREDBY IS NOT NULL
  GROUP BY REFERREDBY
),
APPLICATIONS AS (
    SELECT UM.REFERREDBY, COUNT(ML.APPL_STATUS) AS "APPL_CNT"
    FROM USER_MODEL UM
    LEFT OUTER JOIN APPLICATION_MODEL ML ON UM.ID = ML.USER_ID AND ML.APPL_STATUS = 'PURCHASED'
    GROUP BY UM.REFERREDBY
)
SELECT R.REFERREDBY, R.REF_CNT, A.APPL_CNT
FROM REFERRAL R
LEFT OUTER JOIN APPLICATIONS A ON R.REFERREDBY = A.REFERREDBY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM