两个查询联盟

时间:2014-10-16 03:57:39

标签: sql-server union

我尝试将两个查询合并如下(别名是我尝试解决此问题之一);

SELECT a.Day, a.[Away Days], a.[Office Days] 
FROM (SELECT DATENAME(dw, Date) AS 'Day', SUM(Hours)/8 AS 'Away Days', NULL AS 'Office Days' 
    FROM  TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314) 
        AND id_Leave IS NOT NULL AND id_Leave != 4)
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)) a

UNION

SELECT b.Day, b.[Away Days], b.[Office Days] 
FROM (SELECT DATENAME(dw, Date) AS 'Day', NULL AS 'Away Days', SUM(Hours)/8 AS 'Office Days' 
    FROM TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314) 
        AND Offshore = 0 AND (id_Leave IS NULL OR id_Leave != 4))
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)) b

问题在于,这并没有做出正确的结合。我想知道是否可能是Group By Part的那些破坏事情,但我不这么认为?

这是一个样本结果;

Day         Away Days   Office Days
Friday      NULL        23.0125
Friday      7           NULL
Monday      NULL        24
Monday      6           NULL
Thursday    NULL        26
Thursday    5.5         NULL
Tuesday     NULL        25.9375
Tuesday     7.5         NULL
Wednesday   NULL        26.05
Wednesday   8           NULL

我正在寻找的是两个值都在同一条线上,而不是有NULL,我不太确定为什么会发生这种情况(解释和解决方案)我将不胜感激。)

我希望保留别名,因为我认为添加第4列的比例以及可能的其他内容会很好。

5 个答案:

答案 0 :(得分:1)

您可以加入这两个并使用相同的逻辑。

SELECT a.Day, ISNULL(a.[Away Days],b.[Away Days]) as [Away Days], ISNULL(a.[Office Days],b.[Office Days]) as [Office Days] 
FROM (SELECT DATENAME(dw, Date) AS 'Day', SUM(Hours)/8 AS 'Away Days', NULL AS 'Office Days' 
    FROM  TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314) 
        AND id_Leave IS NOT NULL AND id_Leave != 4)
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)) a

JOIN   (SELECT DATENAME(dw, Date) AS 'Day', NULL AS 'Away Days', SUM(Hours)/8 AS 'Office Days' 
    FROM TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314) 
        AND Offshore = 0 AND (id_Leave IS NULL OR id_Leave != 4))
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)) b ON a.Day = b.Day

答案 1 :(得分:0)

如果一个查询选择Away Days,其他Office Days,为什么不简单地加入两个而不是UNIONing它们?将它们写为两个派生表并将它们链接在外部查询中?概要:

SELECT
  a.day,
  a.[Away Days],
  b.[Office Days]
FROM
  ( Q1 ) AS a
  INNER JOIN ( Q2 ) AS b ON
    a.day=b.day

答案 2 :(得分:0)

这表现得如预期。联合不会对字段进行合并,但会从n选择中为您提供组合集。

您可能想要考虑加入

答案 3 :(得分:0)

使用CASE获取所需的输出

SELECT DATENAME(dw, Date) AS 'Day',
       (CASE WHEN (id_Leave IS NOT NULL AND id_Leave != 4) THEN SUM(Hours)/8
            ELSE NULL
       END)
        AS 'Away Days', 
       (CASE WHEN (Offshore = 0 AND (id_Leave IS NULL OR id_Leave != 4)) THEN SUM(Hours)/8
            ELSE NULL
       END)
       AS 'Office Days' 
    FROM TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314))
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)

答案 4 :(得分:0)

是否需要UNOIN,请检查以下查询并希望这对您有用:

SELECT a.Day, a.[Away Days], a.[Office Days] 
FROM (SELECT DATENAME(dw, Date) AS 'Day', SUM(Hours)/8 AS 'Away Days', SUM(Hours)/8 AS 'Office Days' 
    FROM  TimesheetDays 
    WHERE id_LineItem IN 
        (SELECT idLineItem FROM TimesheetLineItems 
        WHERE id_Timesheet IN 
            (SELECT idTimesheet 
            FROM Timesheets 
            WHERE id_User = 314) AND Offshore = 0 
        AND id_Leave IS NOT NULL AND id_Leave != 4)
    AND Date < GETDATE()
    GROUP BY DATENAME(dw,Date)) a