2013-05-12 89 views
0

我需要下面的代碼將所有三個proj1,proj4proj5列按照日期一起放在一行中。您可以看到日期類似,但它顯示在不同的記錄中。需要重新安排所有記錄到相同的行

enter image description here

MySQL查詢如下:

select DISTINCT dates,proj1,proj4, proj5 from 
    (SELECT DISTINCT tc.dates AS dates , IF(tc.project_id = 1, tc.minutes, '') AS 'proj1', 
    IF(tc.project_id = 5, tc.minutes, '') AS 'proj5', IF(tc.project_id = 4, tc.minutes, '') AS 'proj4' 
FROM timecard AS tc where (tc.dates between '2013-04-01' AND '2013-04-05')) as X 

我需要所有三個proj1proj4proj5記錄顯示所有在同一行,然後查詢應該只有5行

+1

http://en.m.wikipedia.org/wiki/Database_normalization – HerrSerker 2013-05-12 11:13:50

回答

1

您可以通過dates,然後使用組來max()顯示,不爲空值

select dates, max(proj1) as proj1, max(proj4) as proj4, max(proj5) as proj5 
from timecard 
where tc.dates between '2013-04-01' AND '2013-04-05' 
group by dates 
0

試試這個SQL。

select dates, 
     (case t1.proj1 
     when t1.proj1 not null then t1.proj1 
     when t2.proj1 not null then t2.proj1 
     when t3.proj1 not null then t3.proj1 
     end) as "proj1", 
     (case t1.proj2 
     when t1.proj2 not null then t1.proj2 
     when t2.proj2 not null then t2.proj2 
     when t3.proj2 not null then t3.proj2 
     end) as "proj2", 
     (case t1.proj3 
     when t1.proj3 not null then t1.proj3 
     when t2.proj3 not null then t2.proj3 
     when t3.proj3 not null then t3.proj3 
     end) as "proj3" 
from timecard t1,timecardt2,timecardt3 
where t1.dates=t2.dates 
and t2.dates=t3.dates 
group by t1.dates 
相關問題