2

目前我正在执行 3 个单独的 SQL 调用。如果可能的话,我想将它们组合成一个,这样我的网格就会正确排序。

我正在使用 4 个单独的表格

BlastAnalytics - 有以下列,粗体是我需要的值... id(primaryKey)、eventType、BlastID、email、ts、bounceDesc

BlastJobs - 有以下列,粗体是我需要的值... JobNumber(primaryKey), MessageFrom, MessageHeader

BlastOpens - id(primaryKey)、AnalyticsID、ts

BlastClicks - id(primaryKey)、AnalyticsID、ts

连接是... BlastAnalytics.BlastID --> BlastJobs.JobNumber BlastAnalytics.id --> BlastOpens.AnalyticsID BlastAnalytics.id --> BlastClicks.AnalyticsID

目前我运行以下 SQL 语句来绑定我的网格......

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

然后在 grid1_RowDataBound 上(创建每个单独的行时)我运行以下语句来获取我的计数...

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)

这一切都很好,但我认为我应该能够使用 GROUP BY 或其他东西将这些语句组合成一个,但我不知道如何。

编辑

这是表中数据类型的示例...

BlastOpens 表

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 表

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

因此,在上面的示例中 blahblah@blah.com 的总打开数为 3,itsa@test.com 的总打开数为 1。

4

1 回答 1

0

虽然我没有对此进行测试,但假设您的 RDBMS 支持子查询,这样的事情应该可以工作:

SELECT BlastAnalytics.eventType, BlastAnalytics.BlastID, BlastAnalytics.email, 
   BlastAnalytics.ts, BlastAnalytics.bounceDesc, BlastJobs.MessageFrom, 
   BlastJobs.MessageHeader, 
   BO.OpenCount,
   BC.ClickCount
FROM BlastAnalytics 
   INNER JOIN BlastJobs ON BlastAnalytics.BlastID = BlastJobs.JobNumber
   LEFT JOIN (
      SELECT COUNT(*) AS OpenCount, BlastAnalytics.BlastID, BlastAnalytics.email
      FROM BlastAnalytics INNER JOIN
         BlastOpens ON BlastAnalytics.id = BlastOpens.AnalyticsID
      GROUP BY BlastAnalytics.BlastID, BlastAnalytics.email
   ) BO ON BlastAnalytics.BlastID = BO.BlastID AND BlastAnalytics.Email = BO.Email
   LEFT JOIN (
      SELECT COUNT(*) AS ClickCount, BlastAnalytics.BlastID, BlastAnalytics.email
      FROM BlastAnalytics INNER JOIN
         BlastClicks ON BlastAnalytics.id = BlastClicks.AnalyticsID
      GROUP BY BlastAnalytics.BlastID, BlastAnalytics.email
   )  BC ON BlastAnalytics.BlastID = BC.BlastID AND BlastAnalytics.Email = BC.Email
WHERE (BlastAnalytics.eventType <> 'open') 
   AND (BlastAnalytics.eventType <> 'click') 
   AND (BlastAnalytics.BlastID = @BlastID)
ORDER BY BlastAnalytics.ts DESC

祝你好运。

于 2013-01-23T02:54:26.437 回答