SQL Server:一次为不同的表组合COUNT(*)

时间:2016-04-25 19:47:08

标签: sql sql-server vb.net sql-server-2012 inner-join

我想要将一些查询合并到 ONE 查询中,以便不必多次调用服务器。

我正在使用的查询示例:

SELECT COUNT(*) AS mailCount1 
FROM [WebContact].[dbo].[memberEmails]  
WHERE contactdatetime > '01/01/06'
  AND contactdatetime < '02/01/06'

SELECT COUNT(*) AS mailCount2
FROM [WebContact].[dbo].[otherEmails]
WHERE contactdatetime > '01/01/06'
  AND contactdatetime < '02/01/06'

SELECT COUNT(*) AS mailCount3 
FROM [WebContact].[dbo].[memberEmails]  
WHERE contactdatetime > '02/01/06'
  AND contactdatetime < '03/01/06'

SELECT COUNT(*) AS mailCount4
FROM [WebContact].[dbo].[otherEmails]
WHERE contactdatetime > '02/01/06'
  AND contactdatetime < '03/01/06'

etc etc...

因此,如上例所示,只有改变的是:

  • FROMmemberEmails&amp; otherEmails

  • >&amp; <个月(01/01 / 06,02 / 01/06 | 02/01 / 06,03 / 01/06 |等......)

这可以用单个查询吗?

3 个答案:

答案 0 :(得分:3)

首先,使用select year(contactdatetime) as yyyy, month(contactdatetime) as mm, count(*) from WebContact].[dbo].[memberEmails] group by year(contactdatetime), month(contactdatetime); 并使用两个查询:

select year(contactdatetime) as yyyy, month(contactdatetime) as mm, count(*)
from WebContact].[dbo].[otherEmails]
group by year(contactdatetime), month(contactdatetime);

select coalesce(me.yyyy, oe.yyyy) as yyyy, coalesce(me.mm, oe.mm) as mm,
       coalesce(me.cnt, 0) as memberemailcnt,
       coalesce(oe.cnt, 0) as otheremailcnt
from (select year(contactdatetime) as yyyy, month(contactdatetime) as mm, count(*) as cnt
      from WebContact].[dbo].[memberEmails]
      group by year(contactdatetime), month(contactdatetime)
     ) me full outer join
     (select year(contactdatetime) as yyyy, month(contactdatetime) as mm, count(*) as cnt
      from WebContact].[dbo].[otherEmails]
      group by year(contactdatetime), month(contactdatetime)
     ) oe
     on me.yyyy = oe.yyyy and me.mm = oe.mm;

然后,如果您愿意,可以将这些组合成一个查询:

full outer join

如果两个表都包含所有月份的数据,则无需using System.Activities; using Microsoft.TeamFoundation.Build.Client; using Microsoft.TeamFoundation.Build.Workflow.Activities; namespace MyCompany.TFS.CodeCoverageLibrary { [BuildExtension(HostEnvironmentOption.All)] [BuildActivity(HostEnvironmentOption.All)] public sealed class SimpleCodeActivity : CodeActivity<bool> { protected override bool Execute(CodeActivityContext context) { context.TrackBuildMessage(string.Format("ActivityInstanceId='{0}', WorkflowInstanceId='{1}'", context.ActivityInstanceId, context.WorkflowInstanceId)); return true; } } }

答案 1 :(得分:0)

我没有检查语法或性能,但你可以这样做,

     WITH cte (
        countvalue
        ,description
        )
    AS (
        SELECT COUNT(*)
            ,'mailCount1'
        FROM [WebContact].[dbo].[memberEmails]
        WHERE contactdatetime > '01/01/06'
            AND contactdatetime < '02/01/06'

        UNION ALL

        SELECT COUNT(*)
            ,'mailCount2'
        FROM [WebContact].[dbo].[otherEmails]
        WHERE contactdatetime > '01/01/06'
            AND contactdatetime < '02/01/06'

        UNION ALL

        SELECT COUNT(*)
            ,'mailCount3'
        FROM [WebContact].[dbo].[memberEmails]
        WHERE contactdatetime > '02/01/06'
            AND contactdatetime < '03/01/06'

        UNION ALL

        SELECT COUNT(*)
            ,'mailCount4'
        FROM [WebContact].[dbo].[otherEmails]
        WHERE contactdatetime > '02/01/06'
            AND contactdatetime < '03/01/06'
        )

    SELECT mailCount1
    ,mailCount2
    ,mailCount3
    ,mailCount4
FROM (
    SELECT countvalue
        ,description
    FROM cte
    ) d
pivot(max(countvalue) FOR description IN (mailCount1, mailCount2, mailCount3, mailCount4)) piv;

希望这会有所帮助..

答案 2 :(得分:0)

declare @emailCount table(tablename varchar(20), year int, month int, qty int) 

insert into @emailCount
select 'memberEmails', year(contactdatetime), month(contactdatetime), count(*)
from [WebContact].[dbo].[memberEmails]   
group by year(contactdatetime), month(contactdatetime)

insert into @emailCount
select 'otherEmails',year(contactdatetime), month(contactdatetime), count(*)
from [WebContact].[dbo].[otherEmails]
group by year(contactdatetime), month(contactdatetime)

select tablename, year, month, qty from @emailCount

如果需要,添加WHERE子句以限制日期范围。 (编辑 - 简化为使用year()和month()函数。)