2016-04-09 160 views
2

我下面查詢獲取記錄從表penilaian_header & penilaian_detail查詢顯示錯誤結果

select *  
from 
( 
    select 
     row, a.tanggal,b.OutletCode,c.Nilai,a.Nip,b.Fullname,a.KodePenilaian, 
     f.Description as posisilama, d.ShortDesc as posisibaru 
    from penilaian_header a 
     left join Employee b on a.Nip = b.Nip 
     left join Position f on b.PositionCode = f.PositionCode 
     left join Position d on a.PositionCode = d.PositionCode 
     left join Penilaian_Detail e on a.KodePenilaian = e.KodePenilaianH 
     left join arealeader g on g.OutletCode = b.OutletCode 
     left join (
        select 
         ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, 
         Nilai, 
         KodePenilaianH 
        from penilaian_Detail 
        ) c on a.KodePenilaian = e.KodePenilaianH 
    where a.Outlet like '%%' and Periode like '%%' 
) nilai 
pivot 
( 
    sum(nilai) for row in ([1],[2],[3],[4],[5]) 
) piv; 

我在penilaian_header enter image description here

記錄我的紀錄penilaian_detail

enter image description here

WHE ñ我運行我的查詢,我得到了這個結果。 enter image description here

結果應該與penilaian_detail相同。請告訴我如何解決它。

http://sqlfiddle.com/#!3/f7e4d/8

感謝@dhruvjoshi的說明。現在的結果是像什麼,我預計

首先,我刪除此 left join Penilaian_Detail e on a.KodePenilaian = e.KodePenilaianH

然後我在結果改變這種 on a.KodePenilaian = e.KodePenilaianHon a.KodePenilaian = c.KodePenilaianH

+0

我從來沒有使用sqlfiddle。但我嘗試一下,這裏是我的鏈接http://sqlfiddle.com/#!3/1ec6e/1 – Boby

+0

你還需要在表中插入一些數據來評估查詢 – DhruvJoshi

+0

我已經插入了一些http:// sqlfiddle.com/#!3/f7e4d/8 – Boby

回答

2

的問題是,由於JOINs你有重複的行設置爲內部查詢。 See inner query fiddle evaluation

更正查詢低於

select *  
from 
(
    select 
     distinct ---added distinct here to avoid repetition 
     row, a.tanggal,b.OutletCode,c.Nilai,a.Nip,b.Fullname,a.KodePenilaian,f.Description as posisilama,d.ShortDesc as posisibaru 
    from penilaian_header a 
     left join Employee b on a.Nip = b.Nip 
     left join Position f on b.PositionCode = f.PositionCode 
     left join Position d on a.PositionCode = d.PositionCode 
     left join Penilaian_Detail e on a.KodePenilaian = e.KodePenilaianH 
     left join arealeader g on g.OutletCode = b.OutletCode 
     left join (
        select 
         ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, 
         Nilai,KodePenilaianH 
        from penilaian_Detail 
        ) c on a.KodePenilaian = e.KodePenilaianH 
) nilai 
pivot 
(
    sum(nilai) 
    for row in ([1],[2],[3],[4],[5]) 
) piv; 

SQL demo link

+0

好吧,這是工作知道。謝謝你。我只需要添加不同的? – Boby

+0

我嘗試做另一個輸入。請檢查http://sqlfiddle.com/#!3/e4111/1 – Boby

+0

@Boby再次嘗試沒有明顯的查詢。更正的查詢鏈接http://sqlfiddle.com/#!3/e4111/5 – DhruvJoshi