Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Distinct Query

Author  Topic 

hammock
Starting Member

2 Posts

Posted - 2009-04-20 : 21:40:33
Hello, I am new to SQL and databases (have been learning them for the past month) . I use google and old forum questions all the time to help me, so you guys are great! Was wondering if you could help me with a specific issue here:

I have the following table (3million x larger but you get the idea)


EMAIL LAUNCHID ACTION
a@fgy.com 43 Sent
a@fgy.com 43 Opened
a@fgy.com 43 Clicked
a@fgy.com 44 Sent
a@fgy.com 44 Bounced
bdf@df.com 35 Skipped
bdf@df.com 37 Skipped
uwyb@yrt.com 43 Sent
uwyb@yrt.com 44 Sent


And here's what I would like. I would like to get for each unique email:

1. the number of launches (unique launchID) where there is a SENT action recorded AND no BOUNCE action recorded for that same launch

2. the number of launches (unique launchID) where there is a SENT action recorded AND no BOUNCE action recorded for that same launch
AND an OPEN OR CLICK action recorded.

Can you help me with some ideas please? Thank you!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-04-20 : 22:46:44
First show what did you try?
Go to Top of Page

Qualis
Posting Yak Master

145 Posts

Posted - 2009-04-21 : 11:01:11
Try this:

/*
Create Table #test (email varchar(50), lid int, action varchar(50))
Insert Into #test Select 'a@fgy.com', 43, 'Sent'
Insert Into #test Select 'a@fgy.com', 43, 'Opened'
Insert Into #test Select 'a@fgy.com', 43, 'Clicked'
Insert Into #test Select 'a@fgy.com', 44, 'Sent'
Insert Into #test Select 'a@fgy.com', 44, 'Bounced'
Insert Into #test Select 'bdf@df.com', 35, 'Skipped'
Insert Into #test Select 'bdf@df.com', 37, 'Skipped'
Insert Into #test Select 'uwyb@yrt.com', 43, 'Sent'
Insert Into #test Select 'uwyb@yrt.com', 44, 'Sent'
*/

Select T.email,
Sum(
Case
When Sent.lid Is Not Null
And Bounced.lid Is Null
Then 1
Else 0
End
) As LaunceshSent,
Sum(
Case
When Sent.lid Is Not Null
And Bounced.lid Is Null
And Opened.lid Is Not Null
Then 1
Else 0
End
) As LaunchesOpened

From (Select Distinct email, lid From #test) T
Left Outer Join
(
Select Distinct email, lid From #test
Where action In ('Sent')
) Sent On T.email = Sent.email and T.lid = Sent.lid
Left Outer Join
(
Select Distinct email, lid From #test
Where action In ('Bounced')
) Bounced On T.email = Bounced.email and T.lid = Bounced.lid
Left Outer Join
(
Select Distinct email, lid From #test
Where action In ('Opened', 'Clicked')
) Opened On T.email = Opened.email and T.lid = Opened.lid

Group By T.email
Go to Top of Page

hammock
Starting Member

2 Posts

Posted - 2009-04-21 : 20:26:03
Qualis, so far that has worked perfectly. You guys are freaking geniuses! Can't thank you enough.
Go to Top of Page
   

- Advertisement -