-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

union2 つのクエリを一緒にしたいだけのようです。これを行うには、両方のクエリにすべての列を含める必要があります。必要に応じてそれらを null/zero に設定するだけです。もう 1order 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 に答える