-1

这是我的查询

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
4

1 回答 1

1

看起来您只想将union两个查询放在一起。为此,两个查询都必须包含所有列,只需根据需要将它们设置为空/零。另一个小困难是你不能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
于 2012-05-18T07:38:22.187 回答