Currently I am performing 3 separate SQL calls. I would like to combine them into one if possible so my grid will sort properly.
I am working with 4 separate Tables
BlastAnalytics – has the following columns and bold are the ones I need the values…
id(primaryKey), eventType, BlastID, email, ts, bounceDesc
BlastJobs – has the following columns and bold are the ones I need the values…
JobNumber(primaryKey), MessageFrom, MessageHeader
BlastOpens – id(primaryKey), AnalyticsID, ts
BlastClicks – id(primaryKey), AnalyticsID, ts
The Joins are…
BlastAnalytics.BlastID –> BlastJobs.JobNumber
BlastAnalytics.id –> BlastOpens.AnalyticsID
BlastAnalytics.id –> BlastClicks.AnalyticsID
Currently I run the following SQL Statement to bind my grid…
SELECT BlastAnalytics.eventType, BlastAnalytics.BlastID, BlastAnalytics.email,
BlastAnalytics.ts, BlastAnalytics.bounceDesc, BlastJobs.MessageFrom,
BlastJobs.MessageHeader
FROM BlastAnalytics INNER JOIN
BlastJobs ON BlastAnalytics.BlastID = BlastJobs.JobNumber
WHERE (BlastAnalytics.eventType <> 'open')
AND (BlastAnalytics.eventType <> 'click')
AND (BlastAnalytics.BlastID = @BlastID)
ORDER BY BlastAnalytics.ts DESC
Then on grid1_RowDataBound (when each individual row is created) I run the follow statements to get my Counts…
SELECT COUNT(*) AS OpenCount, BlastAnalytics.email
FROM BlastAnalytics INNER JOIN
BlastOpens ON BlastAnalytics.id = BlastOpens.AnalyticsID
WHERE (BlastAnalytics.BlastID = @BlastID)
AND (BlastAnalytics.email = @email)
SELECT COUNT(*) AS ClickCount, BlastAnalytics.email
FROM BlastAnalytics INNER JOIN
BlastClicks ON BlastAnalytics.id = BlastClicks.AnalyticsID
WHERE (BlastAnalytics.BlastID = @BlastID)
AND (BlastAnalytics.email = @email)
This all works fine but I would think I should be able to combine those statements into one using GROUP BYs or something, but I can’t figure out how.
EDIT
Here is an example of the type of data in the tables…
BlastOpens Table
id AnalyticsID ts BlastID
2958 38289 1358546399 479
2959 38852 1358546391 479
2960 38280 1358546391 479
2961 38280 1358546400 479
2965 38282 1358546396 480
2986 38284 1358546398 480
BlastAnalytics Table
id eventType BlastID email ts bounceDesc
38280 open 479 blahblah@blah.com 1358546555 NULL
38289 open 479 blahblah@blah.com 1358546555 NULL
38352 open 479 itsa@test.com 1358550528 NULL
38115 send 479 blahblah@blah.com 1358545375 NULL
So in the example above blahblah@blah.com has a total Open Count of 3 and itsa@test.com has 1.
While I haven’t tested this, something like this should work assuming your RDBMS supports sub queries:
Good luck.