需要帮助来获得这样的SQL查询输出

时间:2012-05-18 07:24:22

标签: sql-server

这是我的查询

select 
    dtfromdate, dttodate,
    (SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
    fltspl 
from dbo.tblHR_SpecialLeaveTransaction 
where
    nvrempcode = 'MCL1218' and nvrstatus = 1 
order by 
    dtfromdate

结果:

dtfromdate              dttodate                NumberOfDays fltspl
----------------------- ----------------------- ------------ ----------------------
2012-05-01 00:00:00     2012-05-31 00:00:00     30           30

另一个查询

select 
    dtfromdate, dtTodate,
    (SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
    fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop, 
    isnull(fltflexiL, 0) as fltflexiL  
from 
    tblhr_leavetransaction 
where 
    nvrempcode = 'MCL1218' and nvrstatus = 1 
order by 
    dtfromdate

结果:

dtfromdate              dtTodate                NumberOfDays fltcl                  fltsl                  fltpl                  fltcompoff             fltod                  fltlop                 fltflexiL
----------------------- ----------------------- ------------ ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
2011-01-14 00:00:00     2011-01-14 00:00:00     0            1                      0                      0                      0                      0                      0                      0
2011-01-17 00:00:00     2011-01-17 00:00:00     0            1                      0                      0                      0                      0                      0                      0
2011-01-25 00:00:00     2011-01-25 00:00:00     0            0                      0                      0                      0                      1                      0                      0
2011-04-01 00:00:00     2011-04-02 00:00:00     1            0                      0                      0                      0                      2                      0                      0
2011-05-14 00:00:00     2011-05-14 00:00:00     0            0                      0                      0                      0                      1                      0                      0
2011-05-16 00:00:00     2011-05-16 00:00:00     0            0                      0                      0                      1                      0                      0                      0
2011-05-18 00:00:00     2011-05-18 00:00:00     0            1                      0                      0                      0                      0                      0                      0
2011-05-19 00:00:00     2011-05-20 00:00:00     1            0                      2                      0                      0                      0                      0                      0
2011-05-21 00:00:00     2011-05-21 00:00:00     0            1                      0                      0                      0                      0                      0                      0
2011-05-23 00:00:00     2011-05-23 00:00:00     0            0                      0                      0                      1                      0                      0                      0

我需要像这样的输出,

dtfromdate              dtTodate                NumberOfDays fltcl                  fltsl                  fltpl                  fltcompoff             fltod                  fltlop                 fltflexiL                fltspl
----------------------- ----------------------- ------------ ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------  ----------
2011-01-14 00:00:00     2011-01-14 00:00:00     0            1                      0                      0                      0                      0                      0                      0                          0
2011-01-17 00:00:00     2011-01-17 00:00:00     0            1                      0                      0                      0                      0                      0                      0                          0
2011-01-25 00:00:00     2011-01-25 00:00:00     0            0                      0                      0                      0                      1                      0                      0                          0
2011-04-01 00:00:00     2011-04-02 00:00:00     1            0                      0                      0                      0                      2                      0                      0                          0
2011-05-14 00:00:00     2011-05-14 00:00:00     0            0                      0                      0                      0                      1                      0                      0                          0
2011-05-16 00:00:00     2011-05-16 00:00:00     0            0                      0                      0                      1                      0                      0                      0                          0
2011-05-18 00:00:00     2011-05-18 00:00:00     0            1                      0                      0                      0                      0                      0                      0                          0
2011-05-19 00:00:00     2011-05-20 00:00:00     1            0                      2                      0                      0                      0                      0                      0                          0
2011-05-21 00:00:00     2011-05-21 00:00:00     0            1                      0                      0                      0                      0                      0                      0                          0
2011-05-23 00:00:00     2011-05-23 00:00:00     0            0                      0                      0                      1                      0                      0                      0                          0
2012-05-01 00:00:00     2012-05-31 00:00:00     30           0                      0                      0                      0                      0                      0                      0                          30

1 个答案:

答案 0 :(得分:1)

看起来您只想要union两个查询。为此,两个查询都必须包含所有列,只需根据需要将这些列设置为null / zero。另一个轻微的困难是你在工会时不能使用order by,除非你再选择整个事情。

没有order by

select 
    dtfromdate, dttodate,
    (SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
    0 as fltcl,
    0 as fltsl,
    0 as fltpl,
    0 as fltcompoff,
    0 as fltod,
    0 as fltlop,
    0 as fltflexiL,
    fltspl 
from dbo.tblHR_SpecialLeaveTransaction 
where
    nvrempcode = 'MCL1218' and nvrstatus = 1 
union
select 
    dtfromdate, dtTodate,
    (SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
    fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop, 
    isnull(fltflexiL, 0) as fltflexiL  ,
    0 as fltspl 
from 
    tblhr_leavetransaction 
where 
    nvrempcode = 'MCL1218' and nvrstatus = 1 

如果您需要特定订单:

SELECT * FROM
(
    select 
        dtfromdate, dttodate,
        (SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
        0 as fltcl,
        0 as fltsl,
        0 as fltpl,
        0 as fltcompoff,
        0 as fltod,
        0 as fltlop,
        0 as fltflexiL,
        fltspl 
    from dbo.tblHR_SpecialLeaveTransaction 
    where
        nvrempcode = 'MCL1218' and nvrstatus = 1 
    union
    select 
        dtfromdate, dtTodate,
        (SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
        fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop, 
        isnull(fltflexiL, 0) as fltflexiL  ,
        0 as fltspl 
    from 
        tblhr_leavetransaction 
    where 
        nvrempcode = 'MCL1218' and nvrstatus = 1 
) src
order by dtfromdate