加入三个SQL查询

时间:2013-08-28 19:21:03

标签: sql sql-server-2000

我使用的是sql server 2000,因此无法使用CTE的

DECLARE @reportdate AS DATETIME

SET @reportdate='08/26/2013'

第一次查询

SELECT SUM(NENTEREDACD) AS calls,
       DINTERVALSTART
FROM   dbo.QUEUESTATS
WHERE  ( DINTERVALSTART > @reportdate + ' 00:00:00.000' )
       AND ( DINTERVALSTART < DATEADD(d, 1, @reportdate) + ' 00:00:00.000' )
GROUP  BY DINTERVALSTART
ORDER  BY DINTERVALSTART

第二次查询

SELECT count(cname)   AS AvaiAgents,
       dintervalstart AS startdatetime
FROM   (SELECT dintervalstart,
               cname
        FROM   [dbo].[AGENTSTATS]
        GROUP  BY dintervalstart,
                  cname
        HAVING ( DINTERVALSTART > @reportdate + ' 00:00:00.000' )
               AND ( DINTERVALSTART < DATEADD(d, 1, @reportdate) + ' 00:00:00.000' )) T
GROUP  BY T.dintervalstart

第3次查询

SELECT count(cname)   AS ActiveAgents,
       dintervalstart AS startdatetime
FROM   (SELECT dintervalstart,
               cname
        FROM   [dbo].[AGENTSTATS1]
        GROUP  BY dintervalstart,
                  cname
        HAVING ( DINTERVALSTART > @reportdate + ' 00:00:00.000' )
               AND ( DINTERVALSTART < DATEADD(d, 1, @reportdate) + ' 00:00:00.000' )) T
GROUP  BY T.dintervalstart 

我只想从第一个查询中仅提取记录,所以它应该是类似的 所以我需要从三个不同的查询中获得三列。常见的是时间戳

I inetval     Calls  Avaiagents     Active agents
08/26/2012    20     22              23 

1 个答案:

答案 0 :(得分:0)

cte只是带有using关键字的派生表。你能用老式的方式做到吗?

select somefields
from a_table join
(select etc) temp on t.something = a_table.something