SQL Server连接表语句

时间:2011-06-15 10:47:51

标签: sql sql-server join

我是否可以加入以下3个语句,在一行中显示sick_total_dayother_total_dayother_total_day(相同的where子句)?

SELECT staff_key, 
       sum(days_applied) as sick_total_day 

from Tleave_sick_leave 

where staff_key = 131 
and from_date>='2011/4/1 00:00:00' 
and to_date<='2011/4/30 00:00:00';


SELECT staff_key, 
       sum(days_applied) as other_total_day 
from Tleave_other_leave 
where staff_key = 131 
and from_date>='2011/4/1 00:00:00' 
and to_date<='2011/4/30 00:00:00';


SELECT staff_key, 
       sum(days_applied) as other_total_day 

from Tleave_vleave 
where staff_key = 131 
and from_date>='2011/4/1 00:00:00' 
and to_date<='2011/4/30 00:00:00';

4 个答案:

答案 0 :(得分:2)

这会处理重复的where子句,但会带来性能损失。

更好的解决方案可能是EMP建议的。规范化表格以删除重复数据。

SELECT  staff_key
        , SUM(CASE WHEN type = 'Sick' THEN days_applied ELSE 0 END) as sick_total_day       
        , SUM(CASE WHEN type = 'Othert' THEN days_applied ELSE 0 END) as othert_total_day       
        , SUM(CASE WHEN type = 'Otherv' THEN days_applied ELSE 0 END) as otherv_total_day       
FROM    (
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    , [Type] = 'Sick'
            FROM    Tleave_sick_leave
            UNION ALL
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    , 'Othert'
            FROM    Tleave_other_leave
            UNION ALL
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    , 'Otherv'
            FROM    Tleave_vleave
        )
WHERE   staff_key = 131 
        AND from_date>='2011/4/1 00:00:00' 
        AND to_date<='2011/4/30 00:00:00';
GROUP BY
        staff_key

答案 1 :(得分:1)

您可能在一个语句中执行此操作,但您仍然需要3个单独的WHERE子句,因为每个表中的from_date和to_date列完全独立,尽管名称相同。

听起来你在桌面设计中发现了一个问题。您有3个表具有非常相似的信息。如果你可以将它们组合成一个表,那么你也可以通过一个查询来解决这个问题。

答案 2 :(得分:1)

您可以将三个表联合起来制作一组数据。然后,您可以使用PIVOT将SUM转换为列。

这应该有效。虽然我没有用任何数据测试它。

SELECT
    [Tleave_sick_leave],
    [Tleave_other_leave],
    [Tleave_vleave]
FROM
(
    SELECT
        'Tleave_sick_leave' [Table],
        [staff_key],
        [days_applied],
        [from_date],
        [to_date]
    FROM
        [Tleave_sick_leave]
    UNION ALL
    SELECT
        'Tleave_other_leave' [Table],
        [staff_key],
        [days_applied],
        [from_date],
        [to_date]
    FROM
        [Tleave_other_leave]
    UNION ALL
    SELECT
        'Tleave_vleave' [Table],
        [staff_key],
        [days_applied],
        [from_date],
        [to_date]
    FROM
        [Tleave_vleave]
) [PivotData]
PIVOT
(
    SUM([days_applied])
FOR
    [Table]
IN
    (
        [Tleave_sick_leave],
        [Tleave_other_leave],
        [Tleave_vleave]
    )
) [Data]
WHERE
    [staff_key] = 131
AND
    [from_date] >= '2011/4/1 00:00:00' 
AND
    [to_date] <= '2011/4/30 00:00:00'

注意:这是一种SQL Server 2005 up方法。如果你有2000或更低,那么你将需要使用列文的答案,这有效地做同样的事情。

答案 3 :(得分:0)

Lieven提供的代码略有修改,现在可以使用......

SELECT  a.staff_key
        , SUM(CASE WHEN a.type = 'Sick' THEN 
                        a.days_applied ELSE 0 END) as sick_total_day       
        , SUM(CASE WHEN a.type = 'Othert' THEN
                        a.days_applied ELSE 0 END) as othert_total_day       
        , SUM(CASE WHEN a.type = 'Otherv' THEN 
                        a.days_applied ELSE 0 END) as otherv_total_day       
FROM    
(
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    , type = 'Sick'
            FROM    Tleave_sick_leave
            UNION ALL
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    ,type = 'Othert'
            FROM    Tleave_other_leave
            UNION ALL
            SELECT  staff_key
                    , days_applied
                    , from_date
                    , to_date
                    ,type = 'Otherv'
            FROM    Tleave_vleave
) a
WHERE staff_key = '131' AND 
      from_date>='2011/4/1 00:00:00' AND 
      to_date<='2011/4/30 00:00:00'
GROUP BY staff_key