2012-05-18 93 views
-1

這個SQL查詢輸出是我的查詢需要幫助得到這樣

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 
+3

能否請你**言傳**你在輸出想要什麼?只是向我們投擲一堆數據,讓我們猜測你想要的東西並不是真的有用......例如,你的第二和第三格數據有什麼區別?他們看起來幾乎一樣..... –

+0

檢查該網格的最後一行.... –

+0

我建議你嘗試這一組的子句。 – Eon

回答

1

看起來你只是想union兩個查詢在一起。要做到這一點,兩個查詢都必須包含所有列,只需將它們設置爲null /零。另一個小難題是,除非你重新考慮整個事情,否則在工會時不能使用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 
+0

一個小警告:派生表需要一個別名。 –

+0

@NikolaMarkovinović - Thx,回答已更新。 – Jamiec