TSQL-快速获得所有表计数的方法

时间:2019-01-31 14:01:00

标签: tsql count sql-server-2014 ssms-17

如果数据库d1的表T1,T2,T3,T4的字段均为"Date1"

获取日期早于3天的所有表中所有记录的计数的最佳方法是什么?

我知道一个人可以进行联合,我假设没有一种漂亮的语法可以忽略所有表(就像C ++中的“父”对象一样。)

这可能意味着更高的效率,或者只是T-SQL中令人愉悦的语法。

这是针对SSMS 17.7。 Microsoft SQL Server 2014(SP2)

4 个答案:

答案 0 :(得分:2)

如果您事先知道表名,那么对union all进行简单查询可能是最简单的方法:

SELECT COUNT(*)
FROM
(
     SELECT Date1
     FROM T1
     UNION ALL
     SELECT Date1
     FROM T2
     SELECT Date1
     FROM T3
     SELECT Date1
     FROM T4
) As t
WHERE Date1 <= DATEADD(DAY, -3, GETDATE())

如果您事先不知道表名,则可以使用information_schema.columns动态建立联合查询。

答案 1 :(得分:1)

没有UNION吗?

由于没有COUNT的{​​{1}}返回1值,为什么不一次使用GROUP BY

CROSS JOIN

或者是SELECT t1.Cnt AS [T1], t2.Cnt AS [T2], t3.Cnt AS [T3], t4.Cnt AS [T4], (t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234] FROM (SELECT COUNT(*) AS Cnt FROM T1 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t1 CROSS JOIN (SELECT COUNT(*) AS Cnt FROM T2 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t2 CROSS JOIN (SELECT COUNT(*) AS Cnt FROM T3 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t3 CROSS JOIN (SELECT COUNT(*) AS Cnt FROM T4 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t4

CROSS APPLY

Sql Server的示例片段:

SELECT 
 t1.Cnt AS [T1], 
 t2.Cnt AS [T2], 
 t3.Cnt AS [T3],
 t4.Cnt AS [T4],
 (t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234]
FROM (SELECT CAST(GetDate()-3 AS DATE) as Dt) d
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T1 WHERE [Date1] < d.Dt) AS t1
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T2 WHERE [Date1] < d.Dt) AS t2
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T3 WHERE [Date1] < d.Dt) AS t3
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T4 WHERE [Date1] < d.Dt) AS t4

返回:

declare @T1 table (id int primary key identity(1,1), [Date1] date);
declare @T2 table (id int primary key identity(1,1), [Date1] date);
declare @T3 table (id int primary key identity(1,1), [Date1] date);
declare @T4 table (id int primary key identity(1,1), [Date1] date);

insert into @T1 ([Date1]) values (getdate()-6),(getdate()-5),(getdate()-4),(getdate()-3),(getdate()-2),(getdate()-1),(getdate()-0);
insert into @T2 ([Date1]) select top 6 [Date1] from @T1 order by [Date1] desc;
insert into @T3 ([Date1]) select top 5 [Date1] from @T1 order by [Date1] desc;
insert into @T4 ([Date1]) select top 4 [Date1] from @T1 order by [Date1] desc;

SELECT 
 t1.Cnt AS [T1], 
 t2.Cnt AS [T2], 
 t3.Cnt AS [T3],
 t4.Cnt AS [T4],
 (t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234]
FROM 
(SELECT COUNT(*) AS Cnt FROM @T1 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t1
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM @T2 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t2
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM @T3 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t3
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM @T4 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t4

答案 2 :(得分:1)

那么,您对父对象感兴趣,那么该对象就是一个视图。您可以将其重新用于各种查询。或者,根据需要添加更多列:

 long startTime=0L,timeInMilliseconds=0L,timeSwapBuff=10000L,updateTime=0L;

    Runnable updateTimerThread=new Runnable() {
    @Override
    public void run() {

        timeInMilliseconds=-(SystemClock.uptimeMillis()-startTime);
        updateTime=timeSwapBuff+timeInMilliseconds;
        int secs=(int)(updateTime/1000);

        int mins=secs/60;
        secs%=60;
        int milliseconds =(int)(updateTime%1000);
        txtTimer.setText(String.format("%02d",secs)+":"+String.format("%03d",milliseconds));
        costumHandler.postDelayed(this,0);
        System.out.println(updateTime);
    }
};

现在,可以按照您想要的方式查询

CREATE VIEW parent AS
SELECT Date1 FROM t1 UNION ALL
SELECT Date1 FROM t2 UNION ALL
SELECT Date1 FROM t3 UNION ALL
SELECT Date1 FROM t4;

答案 3 :(得分:1)

您可以使用CTE(公用表表达式)代替创建视图。它像视图一样工作,但不能持久存储在数据库中。请尝试:

WITH CteDate( Date1 )
AS ( SELECT Date1 FROM t1 UNION ALL
     SELECT Date1 FROM t2 UNION ALL
     SELECT Date1 FROM t3 UNION ALL
     SELECT Date1 FROM t4
    )
SELECT COUNT(*) FROM CteDate WHERE Date1 <= DATEADD(DAY, -3, GETDATE())        

它适用于大于或等于2005的所有SQL Server。