我想根据SampleData Table & Output生成表格输出。我的SQL查询如下,但它没有给我最后三列的正确结果
SELECT
UserLogin,
COUNT(DISTINCT consumerID) AS TotalUniqueconsumerIDs,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted > 0 AND QuizCompleted <= 0 THEN consumerID END)) AS YesActivityNoQuiz,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted <= 0 AND QuizCompleted > 0 THEN consumerID END)) AS NoActivityYesQuiz,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted >= 1 AND QuizCompleted >= 1 THEN consumerID END)) AS YesActivityYesQuiz
FROM
TableName
GROUP BY
UserLogin
非常感谢任何帮助。
更多信息:
也许我上面并不清楚所以提供更多信息:
我创建了另一个样本表,其中只有1个UserLogin的数据,看起来像this,如果我在Excel外部转移此示例数据集,我会看到this
正如您所看到的,此透视屏幕截图中此表中的consumerID都有超过1个ActivitiesCompleted以及1个QuizCompleted。
基于我上面的查询的SQL输出看起来像this
然而,基于Pivot截图,我期望YesActivityYesQuiz列中的1和YesActivityNoQuiz&amp;中的0。 NoActivityYesQuiz
示例数据:
Date,UserLogin,consumerID,ActivitiesCompleted,QuizCompleted
6/8/2018,aasufhar,consumerA,0,1
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,2,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,1,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerA,0,0
6/8/2018,aasufhar,consumerB,0,1
6/8/2018,aasufhar,consumerB,0,0
6/8/2018,aasufhar,consumerB,2,0
6/8/2018,aasufhar,consumerB,1,0
6/8/2018,aasufhar,consumerB,1,0
6/8/2018,aasufhar,consumerB,1,0
6/8/2018,aasufhar,consumerB,0,0
6/8/2018,aasufhar,consumerB,0,0
6/8/2018,aasufhar,consumerB,0,0
6/8/2018,aasufhar,consumerB,0,0
6/8/2018,aasufhar,consumerB,0,0
答案 0 :(得分:2)
有问题的查询似乎对我来说是正确的(至少它会返回预期结果中显示的内容)
不过,我可以提出更简洁的版本(BigQuery Standard SQL)
#standardSQL
SELECT UserLogin,
COUNT(DISTINCT consumerID) AS TotalUniqueconsumerIDs,
COUNT(DISTINCT IF(ActivitiesCompleted > 0 AND QuizCompleted <= 0, consumerID, NULL)) AS YesActivityNoQuiz,
COUNT(DISTINCT IF(ActivitiesCompleted <= 0 AND QuizCompleted > 0, consumerID, NULL)) AS NoActivityYesQuiz,
COUNT(DISTINCT IF(ActivitiesCompleted >= 1 AND QuizCompleted >= 1, consumerID, NULL)) AS YesActivityYesQuiz
FROM TableName
GROUP BY UserLogin
根据OP
的最新示例/解释更新了查询
#standardSQL
SELECT UserLogin,
COUNT(DISTINCT consumerID) AS TotalUniqueconsumerIDs,
COUNT(DISTINCT IF(ActivitiesCompleted > 0 AND QuizCompleted <= 0, consumerID, NULL)) AS YesActivityNoQuiz,
COUNT(DISTINCT IF(ActivitiesCompleted <= 0 AND QuizCompleted > 0, consumerID, NULL)) AS NoActivityYesQuiz,
COUNT(DISTINCT IF(ActivitiesCompleted >= 1 AND QuizCompleted >= 1, consumerID, NULL)) AS YesActivityYesQuiz,
COUNT(DISTINCT IF(ActivitiesCompleted <= 0 AND QuizCompleted <= 0, consumerID, NULL)) AS NoActivityNoQuiz
FROM (
SELECT UserLogin,consumerID,
SUM(ActivitiesCompleted) ActivitiesCompleted,
SUM(QuizCompleted) QuizCompleted
FROM TableName
GROUP BY UserLogin,consumerID
)
GROUP BY UserLogin
答案 1 :(得分:1)
也许你只想要max()
:
SELECT UserLogin, COUNT(DISTINCT consumerID) AS TotalUniqueconsumerIDs,
MAX(CASE WHEN ActivitiesCompleted > 0 AND QuizCompleted <= 0 THEN 1 ELSE 0 END) AS YesActivityNoQuiz,
MAX(CASE WHEN ActivitiesCompleted <= 0 AND QuizCompleted > 0 THEN 1 ELSE 0 END) AS NoActivityYesQuiz,
MAX(CASE WHEN ActivitiesCompleted >= 1 AND QuizCompleted >= 1 THEN 1 ELSE 0 END) AS YesActivityYesQuiz
FROM TableName
GROUP BY UserLogin;
答案 2 :(得分:0)
您必须在group by子句中包含ActivitiesCompleted,因为您在case语句中使用了它。它没有抛出你的错误?
SELECT
UserLogin,
COUNT(DISTINCT consumerID) AS TotalUniqueconsumerIDs,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted > 0 AND QuizCompleted <= 0 THEN consumerID END)) AS YesActivityNoQuiz,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted <= 0 AND QuizCompleted > 0 THEN consumerID END)) AS NoActivityYesQuiz,
COUNT(DISTINCT (CASE
WHEN ActivitiesCompleted >= 1 AND QuizCompleted >= 1 THEN consumerID END)) AS YesActivityYesQuiz
FROM
TableName
GROUP BY
UserLogin,ActivitiesCompleted