SQL从两个数据库加入查询

时间:2014-02-26 23:47:23

标签: sql sql-server

我有来自两个不同表格的查询,这些表格按yyyy-mm对各自的数据(计数)进行分组。我想从两个查询中获取计数结果并将它们一起显示。以下是我的两个问题:

QUERY 1:

    DECLARE @ThisMonth DATETIME
    SET @ThisMonth = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

    select cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as varchar(7)) AS     [MONTH], COUNT(TransportAppv6.dbo.T_FatigueNCRDetails.NCRID) AS [NCRs]
    FROM TransportAppv6.dbo.T_FatigueNCRs 
    LEFT OUTER JOIN TransportAppv6.dbo.T_FatigueNCRDetails 
    ON T_FatigueNCRs.NCRID = TransportAppv6.dbo.T_FatigueNCRDetails.NCRID 
    LEFT OUTER JOIN TransportAppv6.dbo.T_Driver 
    ON TransportAppv6.dbo.T_FatigueNCRs.DriverID = TransportAppv6.dbo.T_Driver.ID
    WHERE (TransportAppv6.dbo.T_FatigueNCRDetails.CancelYN = 0) 
    AND (TransportAppv6.dbo.T_Driver.FleetID = 2) 
    AND  (TransportAppv6.dbo.T_Driver.Active = 1) 
    AND (TransportAppv6.dbo.T_FatigueNCRDetails.NCRTypeID = 1) 
    AND (TransportAppv6.dbo.T_FatigueNCRDetails.Details <> 'Driving Hours Breach As Per Breach Report') 
    AND (TransportAppv6.dbo.T_FatigueNCRs.Date >= DATEADD(MONTH, -6, @ThisMonth)   
    AND TransportAppv6.dbo.T_FatigueNCRs.Date <  @ThisMonth)
    GROUP BY cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as varchar(7))

返回值:

MONTH     NCRs
2013-08   43
2013-09   34
2013-10   53

QUERY 2:

    DECLARE @ThisMonth DATETIME
    SET @ThisMonth = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

    select cast(cast(landing.dbo.landing_runs.date as date) as varchar(7)) as [MONTH], COUNT(*) as [RUNS]
    from landing.dbo.Landing_Runs
    where (landing.dbo.Landing_Runs.date >= DATEADD(MONTH, -6, @ThisMonth)   
    AND landing.dbo.Landing_Runs.date <  @ThisMonth)
    GROUP BY cast(cast(landing.dbo.landing_runs.date as date) as varchar(7))

返回值:

MONTH     RUNS
2013-08   4626
2013-09   4042
2013-10   4481

我想要做的是以某种方式加入这两个单独的查询,以便结果显示为:

MONTH     RUNS     NCRs
2013-08   4626     43
2013-09   4042     34
2013-10   4481     53

3 个答案:

答案 0 :(得分:4)

您可以将它们作为子查询加入。我在下面选择了INNER JOIN,但可以随意将其更改为其他类型的联接。

DECLARE @ThisMonth DATETIME
SET @ThisMonth = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

select t1.[MONTH], t1.[NCRs], t2.[RUNS]
from
(
    select cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as varchar(7)) AS     [MONTH], COUNT(TransportAppv6.dbo.T_FatigueNCRDetails.NCRID) AS [NCRs]
    FROM TransportAppv6.dbo.T_FatigueNCRs 
    LEFT OUTER JOIN TransportAppv6.dbo.T_FatigueNCRDetails 
    ON T_FatigueNCRs.NCRID = TransportAppv6.dbo.T_FatigueNCRDetails.NCRID 
    LEFT OUTER JOIN TransportAppv6.dbo.T_Driver 
    ON TransportAppv6.dbo.T_FatigueNCRs.DriverID = TransportAppv6.dbo.T_Driver.ID
    WHERE (TransportAppv6.dbo.T_FatigueNCRDetails.CancelYN = 0) 
    AND (TransportAppv6.dbo.T_Driver.FleetID = 2) 
    AND  (TransportAppv6.dbo.T_Driver.Active = 1) 
    AND (TransportAppv6.dbo.T_FatigueNCRDetails.NCRTypeID = 1) 
    AND (TransportAppv6.dbo.T_FatigueNCRDetails.Details <> 'Driving Hours Breach As Per Breach Report') 
    AND (TransportAppv6.dbo.T_FatigueNCRs.Date >= DATEADD(MONTH, -6, @ThisMonth)   
    AND TransportAppv6.dbo.T_FatigueNCRs.Date <  @ThisMonth)
    GROUP BY cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as varchar(7))
) t1
inner join
(
    select cast(cast(landing.dbo.landing_runs.date as date) as varchar(7)) as [MONTH], COUNT(*) as [RUNS]
    from landing.dbo.Landing_Runs
    where (landing.dbo.Landing_Runs.date >= DATEADD(MONTH, -6, @ThisMonth)   
    AND landing.dbo.Landing_Runs.date <  @ThisMonth)
    GROUP BY cast(cast(landing.dbo.landing_runs.date as date) as varchar(7))
) t2 on t1.[MONTH] = t2.[MONTH]

答案 1 :(得分:0)

如果两个查询都返回相同的月份范围,您可以将它们与内部联接一起加入。否则,我会创建一个包含您要显示的所有月份的表变量。如果我可以帮助你,请告诉我。

 DECLARE @ThisMonth DATETIME
    SET @ThisMonth = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

select ncrs.[Month],ncrs.NCRs,runs.Runs
from    (
        select
            cast(cast(ncr.date as date) as varchar(7)) AS [MONTH],
            COUNT(ncrd.NCRID) AS [NCRs]
        FROM TransportAppv6.dbo.T_FatigueNCRs ncr 
            LEFT OUTER JOIN TransportAppv6.dbo.T_FatigueNCRDetails 
                ON T_FatigueNCRs.NCRID = ncrd.NCRID 
            LEFT OUTER JOIN TransportAppv6.dbo.T_Driver 
                ON ncr.DriverID = TransportAppv6.dbo.T_Driver.ID
        WHERE (ncrd.CancelYN = 0) 
            AND (TransportAppv6.dbo.T_Driver.FleetID = 2) 
            AND  (TransportAppv6.dbo.T_Driver.Active = 1) 
            AND (ncrd.NCRTypeID = 1) 
            AND (ncrd.Details <> 'Driving Hours Breach As Per Breach Report') 
            AND (ncr.Date >= DATEADD(MONTH, -6, @ThisMonth)   
            AND ncr.Date <  @ThisMonth)
        GROUP BY cast(cast(ncr.date as date) as varchar(7))
        ) ncrs
    inner join  (
                select
                    cast(cast(landing.dbo.landing_runs.date as date) as varchar(7)) as [MONTH],
                    COUNT(*) as [RUNS]
                from landing.dbo.Landing_Runs
                where (landing.dbo.Landing_Runs.date >= DATEADD(MONTH, -6, @ThisMonth)   
                    AND landing.dbo.Landing_Runs.date <  @ThisMonth)
                GROUP BY cast(cast(landing.dbo.landing_runs.date as date) as varchar(7))
                ) runs
        on runs.[Month] = ncrs.NCRs

答案 2 :(得分:0)

您可以通过联合连接2个查询,使用您指定是否为NCR或RUNS的列来区分这两个查询,然后使用顶层的case语句进行聚合,按月分组。见下文:

DECLARE @ThisMonth DATETIME
SET @ThisMonth = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

select [   MONTH ],
       sum(case
         when aggtype = 'NCR' then
          [ NCRs ]
         else
          0
       end as ncrs, sum(case
         when aggtype = 'RUNS' then
          [ RUNS ]
         else
          0
       end as runs from (select 'NCR' as aggtype,
                          cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as
                               varchar(7)) AS [ MONTH ],
                          COUNT(TransportAppv6.dbo.T_FatigueNCRDetails.NCRID) AS [ NCRs ]
                     FROM TransportAppv6.dbo.T_FatigueNCRs
                     LEFT OUTER JOIN TransportAppv6.dbo.T_FatigueNCRDetails
                       ON T_FatigueNCRs.NCRID =
                          TransportAppv6.dbo.T_FatigueNCRDetails.NCRID
                     LEFT OUTER JOIN TransportAppv6.dbo.T_Driver
                       ON TransportAppv6.dbo.T_FatigueNCRs.DriverID =
                          TransportAppv6.dbo.T_Driver.ID
                    WHERE (TransportAppv6.dbo.T_FatigueNCRDetails.CancelYN = 0)
                      AND (TransportAppv6.dbo.T_Driver.FleetID = 2)
                      AND (TransportAppv6.dbo.T_Driver.Active = 1)
                      AND (TransportAppv6.dbo.T_FatigueNCRDetails.NCRTypeID = 1)
                      AND (TransportAppv6.dbo.T_FatigueNCRDetails.Details <>
                          'Driving Hours Breach As Per Breach Report')
                      AND (TransportAppv6.dbo.T_FatigueNCRs.Date >=
                          DATEADD(MONTH, -6, @ThisMonth) AND
                          TransportAppv6.dbo.T_FatigueNCRs.Date <
                          @ThisMonth)
                    GROUP BY cast(cast(TransportAppv6.dbo.T_FatigueNCRs.date as date) as
                                  varchar(7))
                   union all
                   select 'RUNS' as aggtype,
                          cast(cast(landing.dbo.landing_runs.date as date) as
                               varchar(7)) as [ MONTH ],
                          COUNT(*) as [ RUNS ]
                     from landing.dbo.Landing_Runs
                    where (landing.dbo.Landing_Runs.date >=
                          DATEADD(MONTH, -6, @ThisMonth) AND
                          landing.dbo.Landing_Runs.date < @ThisMonth)
                    GROUP BY cast(cast(landing.dbo.landing_runs.date as date) as
                                  varchar(7))) group by [ MONTH ]