如果在连接的子查询中第一个表为空,如何从第二个表中进行选择

时间:2015-02-13 22:57:07

标签: sql-server sql-server-2008 tsql

我的查询工作正常。它从12个月度数据库中提取数据以生成报告。但是,现在我们将每月导入两次数据,而不是每月导入一次,因此我又添加了12个数据库。一个用于上半部分,一个用于下半个月。以“_A”结尾的数据库是上半部分,“_ B”是下半部分。如果它存在的话,我们想要下半部分的数据。如果没有,我们想要上半年的数据。如何在“_B”中修改我的查询以测试数据,如果“_B”中没有数据,我如何从“_A”中选择?

我尝试过使用IF和CASE。我用谷歌搜索,但没有发现任何解决我的情况。希望知道SQL比我好一点的人可以提供帮助。

这是当前只从“_B”数据库中提取的SQL:

SELECT      JanAct.DDName, 
            JanAct.JanActual, 
            FebAct.FebActual, 
            MarAct.MarActual, 
            AprAct.AprActual, 
            MayAct.MayActual, 
            JunAct.JunActual, 
            JulAct.JulActual, 
            AugAct.AugActual, 
            SepAct.SepActual, 
            OctAct.OctActual, 
            NovAct.NovActual, 
            DecAct.DecActual
FROM       (SELECT DDName, [% Eff] AS JanActual FROM TIP_2_Jan_B.dbo.V_DDSum) AS JanAct
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS DecActual FROM TIP_2_Dec_B.dbo.V_DDSum) AS DecAct ON JanAct.DDName = DecAct.DDName
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS NovActual FROM TIP_2_Nov_B.dbo.V_DDSum) AS NovAct ON JanAct.DDName = NovAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS OctActual FROM TIP_2_Oct_B.dbo.V_DDSum) AS OctAct ON JanAct.DDName = OctAct.DDName
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS SepActual FROM TIP_2_Sep_B.dbo.V_DDSum) AS SepAct ON JanAct.DDName = SepAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS AugActual FROM TIP_2_Aug_B.dbo.V_DDSum) AS AugAct ON JanAct.DDName = AugAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS JulActual FROM TIP_2_Jul_B.dbo.V_DDSum) AS JulAct ON JanAct.DDName = JulAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS JunActual FROM TIP_2_Jun_B.dbo.V_DDSum) AS JunAct ON JanAct.DDName = JunAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS MayActual FROM TIP_2_May_B.dbo.V_DDSum) AS MayAct ON JanAct.DDName = MayAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS AprActual FROM TIP_2_Apr_B.dbo.V_DDSum) AS AprAct ON JanAct.DDName = AprAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS FebActual FROM TIP_2_Feb_B.dbo.V_DDSum) AS FebAct ON JanAct.DDName = FebAct.DDName 
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS MarActual FROM TIP_2_Mar_B.dbo.V_DDSum) AS MarAct ON JanAct.DDName = MarAct.DDName

3 个答案:

答案 0 :(得分:1)

首先创建单个数据集,优先处理和加入数据变得非常容易。

WITH data(DDName, [% Eff], [Period], [Source])
AS(
    -- A Database
    SELECT DDName, [% Eff], 'JanActual', 'A' FROM TIP_2_Jan_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'FebActual', 'A' FROM TIP_2_Feb_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'MarActual', 'A' FROM TIP_2_Mar_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'AprActual', 'A' FROM TIP_2_Apr_B.dbo.V_DDSum UNION ALL

    -- B databases
    SELECT DDName, [% Eff], 'JanActual', 'B' FROM TIP_2_Jan_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'FebActual', 'B' FROM TIP_2_Feb_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'MarActual', 'B' FROM TIP_2_Mar_B.dbo.V_DDSum UNION ALL
    SELECT DDName, [% Eff], 'AprActual', 'B' FROM TIP_2_Apr_B.dbo.V_DDSum
),
PrioritizedData AS(
    SELECT *, [Priority] = ROW_NUMBER() OVER (PARTITION BY DDName, [% Eff], [Period] ORDER BY [Source] DESC)
    FROM data
),
SelectTopPrioData AS(
    SELECT * FROM PrioritizedData
    WHERE [Priority] = 1
)
SELECT * 
FROM SelectTopPrioData
PIVOT
(
    MAX([% Eff])
    FOR [Period] IN ([JanActual], [FebActual], [MarActual], [AprActual])
) AS PivotTable;

答案 1 :(得分:0)

就是这样的。

SELECT      JanAct.DDName, 
            ...
            DecActual = isnull(DecAct.DecActual, DecAct_A.DecActual)
FROM       (SELECT DDName, [% Eff] AS JanActual FROM TIP_2_Jan_B.dbo.V_DDSum) AS JanAct
            ...    
            FULL OUTER JOIN (SELECT DDName, [% Eff] AS DecActual FROM TIP_2_Jan_A.dbo.V_DDSum) AS DecAct_A ON JanAct.DDName = DecAct_A.DDName

答案 2 :(得分:0)

使用动态SQL:

 declare @sel varchar(max)

set @sel = ' SELECT
         .......
         FROM         '

 if (SELECT COUNT(*) FROM   TTIP_2_Jan_B.dbo.V_DDSum)  =0
   begin
     set @sel = @sel+'SELECT DDName, [% Eff] AS DecActual FROM TIP_2_Jan_A.dbo.V_DDSum'
   END
   ELSE 
   BEGIN
    set @sel = @sel+'SELECT DDName, [% Eff] AS DecActual FROM TIP_2_Jan_B.dbo.V_DDSum'
END

exec(@sel)