[英]SQL Group By + Count with multiple tables
I'm studying for an interview next week which has a small data analysis component. 我正在研究下周的面试,其中包含一个小的数据分析组件。 The recruiter gave me the following sample SQL question which I'm having trouble wrapping my mind around a solution.
招聘人员给了我以下示例SQL问题,使我难以解决某个解决方案。 I'm hoping that I'm not biting off more than I can chew ;)..
我希望我不会被咬得更多;)..
SAMPLE QUESTION: 示例问题:
You are given two tables:
给出两个表:
AdClick Table (columns: ClickID, AdvertiserID, UserID, and other fields) and AdConversion Table (columns: ClickID, UserID and other fields).
AdClick表(列:ClickID,AdvertiserID,UserID和其他字段)和AdConversion表(列:ClickID,UserID和其他字段)。
You have to find the total conversion rate (# of conversions/# of clicks) for users with 1 click, 2 click etc.
您必须找到1次点击,2次点击等的用户的总转化率(转化次数/点击次数)。
I've been playing with this for about an hour and keep hitting road blocks. 我已经玩了大约一个小时,并且一直遇到障碍。 I understand COUNT and GROUP BY but suspect I'm missing a simple SQL feature that I'm unaware of.
我了解COUNT和GROUP BY,但是怀疑我缺少我不知道的简单SQL功能。 This also makes it difficult for me to find any possible pointers/solutions via Google: not knowing the magic keywords to search on.
这也使我很难通过Google找到任何可能的指针/解决方案:不知道要搜索的魔术关键字。
Example Input 输入示例
dbo.AdConversion
----------------
ClickID UserID
1 1
2 1
4 1
5 3
6 2
7 2
12 1
9 4
10 4
dbo.AdClick
-----------
ClickID AdvertiserID UserID
1 1 1
2 2 1
3 1 2
4 1 1
5 1 3
6 2 2
7 3 2
8 1 1
9 4 4
10 2 4
11 3 4
12 2 1
Expected Result:
----------------
UserClickCount ConversionRate
4 80.00%
2 66.67%
1 100.00%
Explanation/Clarification: 说明/澄清:
Users with 4 AdConversion.ClickIDs (aka Conversions) have an 80% conversation rate. 拥有4个AdConversion.ClickID(又称转化)的用户的会话率达到80%。 Here there's just one user, UserID 1, which has 5 AdClicks with 4 AdConversions.
这里只有一个用户,用户ID 1,其中有5个AdClicks和4个AdConversions。
Users with 2 Conversions have a combined 6 Adclicks with 4 conversions for a conversion rate of 66.67%. 拥有2次转化的用户总共拥有6次Adclicks和4次转化,转化率为66.67%。 Here, that'd be UserID 2 and 4.
在这里,它是用户ID 2和4。
Users with 1 Conversion, here only UserID 3, has 1 conversion against 1 AdClick for a 100% conversion rate. 拥有1个转化的用户(这里只有UserID 3)针对1个AdClick进行了1次转化,转化率为100%。
Here's one possible solution I've come up with after some direction from Zack's comment. 从Zack的评论中获得一些指导之后,这是我想出的一种可能的解决方案。 I can't imagine that it's the ideal solution or whether it has bugs in it or not:
我无法想象这是理想的解决方案,或者它是否包含错误:
DECLARE @Conversions TABLE
(
UserID int NOT NULL,
AdConversions int
)
INSERT INTO @Conversions (UserID, AdConversions)
SELECT adc.UserID, COUNT(adc.UserID)
FROM dbo.AdConversion adc
GROUP BY adc.UserID;
DECLARE @Clicks TABLE
(
UserID int NOT NULL,
AdClicks int
)
INSERT INTO @Clicks(UserID, AdClicks)
SELECT UserID, Count (ClickID)
FROM dbo.AdClick
GROUP BY UserID;
SELECT co.AdConversions, CONVERT(decimal(6,3), (CAST(SUM(co.AdConversions) AS float) / SUM(cl.AdClicks))) * 100
FROM @Conversions co
INNER JOIN @Clicks cl
ON co.UserID = cl.UserID
GROUP BY co.AdConversions;
Any advice would be greatly appreciated! 任何建议将不胜感激!
Thanks, Michael 谢谢迈克尔
Your logic seems good. 您的逻辑看起来不错。 Here is a version with common table expressions and a little update with the numeric conversion:
这是具有常用表表达式的版本,并带有数字转换的一些更新:
WITH tConversions as
(SELECT UserID, COUNT(ClickID) as AdConversions
FROM AdConversion
GROUP BY UserID),
tClicks as
(SELECT UserID, COUNT(ClickID) as AdClicks
FROM AdClick
GROUP BY UserID)
SELECT co.AdConversions, CONVERT(decimal(10,2),CAST(SUM(co.AdConversions) as float) / SUM(cl.AdClicks) * 100) as ConversionRate
FROM tConversions co
INNER JOIN tClicks cl
ON co.UserID = cl.UserID
GROUP BY co.AdConversions
You can also use subqueries directly: 您还可以直接使用子查询:
SELECT co.AdConversions, CONVERT(decimal(10,2),CAST(SUM(co.AdConversions) as float) / SUM(cl.AdClicks) * 100) as ConversionRate
FROM
(SELECT UserID, COUNT(ClickID) as AdConversions
FROM AdConversion
GROUP BY UserID)
as co
INNER JOIN
(SELECT UserID, COUNT(ClickID) as AdClicks
FROM AdClick
GROUP BY UserID)
as cl
ON co.UserID = cl.UserID
GROUP BY co.AdConversions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.