I have situation like below and i answered it like follows. I did count of emails. But what is SUMEMAILCnt ? And also an efficient way to count a billion rows which i never did. PLease help me with your suggestions. Thanks in advance.
There are 3 tables. UserProfile, UserProfileClick, UserProfileEmail. UserProfile stores information about a user’s profile. UserProfileClick stores a record every time someone clicks on a user profile. UserProfileEmail stores a record every time someone sends an email from a user profile.
UserProfile | |||
UserProfileID | CreateDate | ProfileName | EmailAddress |
1 | 5/1/2013 | SeattleBrian | |
2 | 6/1/2013 | KimOverHere | |
UserProfileClick | |||
UserProfileID | SessionGuid | DateTime | |
1 | 0000059f6b18435ead3869a10b240616 | 8/1/2013 | |
1 | 00001bc8c95f44a281be2f37b82b34db | 8/1/2013 | |
2 | 00002213-0221-4758-a088-35075c79e2e7 | 8/1/2013 | |
UserProfileEmail | |||
UserProfileID | SessionGuid | DateTime | |
2 | 0000b885-6854-4131-8b93-631c83bb35f7 | 8/1/2013 | |
2 | 00010c51e820408e9ec4839fe84b7f8f | 8/1/2013 | |
2 | 00012926-5f93-4272-9cc2-ae9ebf7825f1 | 8/1/2013 | |
1 | 000114fa0aa441e5bf667e881194e75a | 8/1/2013 |
Need a query to show the ProfileName and the SUM of all Emails and Clicks. Results should be
ProfileName, SUMEmailCnt, SUMClickCnt. These tables are rather large with around 1 billion records in each of the Click and Email tables.
Answer:
select UP.UserProfileID AS PROFILE_NAME, COUNT(UPE.SessionGuid) AS EMAIL_COUNT
, COUNT(UPC.SessionGuid) AS CLICK_COUNT
from UserProfile UP, UserProfileClick UPC, UserProfileEmail UPE
where UP.UserProfileID = UPC.UserProfileID AND UPC.UserProfileID = UPE.UserProfileID
GROUP BY UP.UserProfileID
svk