SQL Server从连接表中选择列,并在同一选择语句中计数

时间:2013-01-23 02:49:05

标签: sql count group-by

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

我正在使用4个单独的表

BlastAnalytics - 有以下列,粗体是我需要的值......     id(primaryKey), eventType,BlastID,电子邮件,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 BYs或其他东西将这些语句组合成一个,但我无法弄清楚如何。

修改

以下是表格中数据类型的示例...

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。

1 个答案:

答案 0 :(得分: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
祝你好运。