2013-03-28 69 views
0

當我運行此查詢,我得到:尋找從當多個日期是目前最高日ID

SELECT DISTINCT r2.ApplicationBK, 
       max(r2.DatumApplication) maxdatum, 
       p.Name 
FROM FCT_Recruitment r2, 
    DIM_WervingsProject wp, 
    DIM_Persoon p 
WHERE r2.WervingsProjectID = wp.WervingsProjectID 
    AND r2.PersoonID=p.PersoonID 
GROUP BY ApplicationBK, 
     p.name 

的名稱是站在TALBLE DIM_PERSOON P
的「ApplicationBK」正站在DIM_Wervingsproject
和所有的日期(DatumAppliocation)正站在FCT_Recruitment

BK - Date - Name 
012 20-03-1999 - name1 
023 21-03-1999 - name1 
033 22-03-1999 - name1 
112 20-03-1990 - name2 
123 20-03-1999 - name2 
133 20-03-1990 - name2 

但我需要爲每名最高的日期..所以,每個名稱只能站在一次在「名欄」與相應的id(ApplicationBK)和日期。我不需要休息。

當這個沒有工作,我想別的東西......

SELECT WervingsprojectBK, 
     r.ApplicationBK, 
     p.Name, 
     count(*) number,upper(r.StatusWP) status, 
         upper(r.StatusApplication)statusdetail, 
         r.DateApplication 
FROM FCT_Recruitment AS r, 
    DIM_WervingsProject wp, 
    DIM_Persoon p 
INNER JOIN 
    (SELECT DISTINCT r2.ApplicationeBK, 
        max(r2.DateApplication) maxdatum 
    FROM FCT_Recruitment r2, 
     DIM_WervingsProject wp, 
     DIM_Persoon p 
    WHERE r2.WervingsProjectID=wp.WervingsProjectID 
    AND r2.PersoonID=p.PersoonID 
    GROUP BY ApplicationBK) AS r2 ON (r.ApplicationBK=r2.ApplicationBK 
            AND r.DateApplication=r2.maxdatum) 
GROUP BY WervingsprojectBK, 
     r.ApplicationBK, 
     p.Naam, 
     r.StatusApplication, 
     r.StatusWP 
ORDER BY 5 

但後來我得到這些錯誤..:

消息4104,級別16,狀態1,3號線多部分標識符 「r.ApplicationBK」不能被綁定。 消息4104,級別16,州 1,行3 多部分標識符「r.DateApplication」可能 不被綁定。

您可以請幫助這個新的.. 謝謝。

回答

0

試試這個:

select 
    r2.ApplicationBK, 
    r22.maxdatum, 
    p.Name 
from FCT_Recruitment r2 
INNER JOIN 
(
    SELECT WervingsProjectID, max(r2.DatumApplication) maxdatum 
    FROM FCT_Recruitment 
    GROUP BY WervingsProjectID 
) AS r22 ON r2.WervingsProjectID = r22.WervingsProjectID 
     AND r2.DatumApplication = r22.maxdatum 
INNER JOIN DIM_WervingsProject AS wp ON r2.WervingsProjectID = wp.WervingsProjectID 
INNER JOIN DIM_Persoon   AS p ON r2.PersoonID   = p.PersoonID; 
+0

確定沒有錯誤,但我仍然有幾次相同的名稱,我只需要名稱與最高日期。 – user2219095 2013-03-28 10:14:50

+0

我真的很感謝你從上面查詢;-)謝謝! – user2219095 2013-03-28 10:40:13

0

試試這個。並更改列名稱:

SELECT max(r.AllDate),r.ProjectID,p.name 
FROM FCT_Recruitment r,DIM_Wervingsproject pr, DIM_PERSOON p 
where p.PID=r.PID and r.ProjectId=pr.ProjectID 
group by r.PID; 
相關問題