Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
B. Storm
Starting Member
3 Posts |
Posted - 2010-08-02 : 09:38:02
|
| I have 3 tables with the following fields:tblCustomer: idCustomer, Filenr, Name, AddresstblStatusPerCustomer: idStatusPerCustomer, idCustomer, idStatusDescription, DateLastmodificationtblStatusDescription: idStatusDescription, StatusDescription Relations: tblCustomer.idCustomer > tblStatusPerCustomer.idCustomer tblStatusPerCustomer.idStatusDescription > tblStatusDescription.idStatusDescription Required return values:tblCustomer.idCustomer, tblCustomer.Filenr, tblCustomer.Name, tblStatusPerCustomer.DateLastmodification, tblStatusDescription.StatusDescriptionWHERE tblStatusPerCustomer.DateLastmodification = MAX(tblStatusPerCustomer.DateLastmodification)Tested query:SELECT DISTINCT TOP (100) PERCENT cl.Name, sc.StatusDescription , MAX(sc.DateLastmodification) AS MaxDateFROM StatusPerCustomer AS spc INNER JOIN (SELECT tblStatusDescription.StatusDescription, MAX(tblStatusPerCustomer.DateLastmodification) AS DateLastModification, tblStatusPerCustomer.idCustomerFROM StatusPerCustomer LEFT OUTER JOIN tblStatusDescription ON StatusPerCustomer.Status = tblStatusDescription.idStatusDescriptionGROUP BY tblStatusDescription.StatusDescription, StatusPerCustomer.idCustomer) AS sc ON sc.idCustomer = spc.idCustomer INNER JOIN tblCustomer AS cl ON cl.idCustomer = spc.idCustomerGROUP BY cl.Filenr, cl.Name, sc.StatusDescription, sc.DateLastModificationORDER BY cl.Name, MaxDate DESC, cl.Filenr, sc.StatusDescriptionResultset returns ALL previous Statusdescriptions instead of only the last Status per customer added to tblStatusPerCustomerWhat am I doing wrong here? |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2010-08-02 : 09:49:19
|
| i think u have missed the join condition on dateadd this condition AND spc.DateLastmodification = SC.DateLastmodification and check it once. |
 |
|
|
B. Storm
Starting Member
3 Posts |
Posted - 2010-08-02 : 10:41:04
|
| Thank you bkrl.I tried your suggestion at different positions in the query.This didn't help either.Can you paste your suggestion into my query?Thanks,Berend |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-08-02 : 11:11:26
|
Try thisSELECT cl.Name, cl.idCustomer,cl.Filenr, sc.StatusDescription ,sp.DateLastmodificationFROM tblCustomer clINNER JOIN (SELECT ROW_NUMBER() OVER(PARTITION BY idCustomer ORDER BY DateLastmodification DESC) AS seq, * FROM tblStatusPerCustomer) spON sp.idCustomer = cl.idCustomer AND sp.seq = 1INNER JOIN tblStatusDescription sc ON sc.idStatusDescription = sp.idStatusDescriptionORDER BY cl.Name,sp.DateLastmodification,cl.Filenr, sc.StatusDescription |
 |
|
|
B. Storm
Starting Member
3 Posts |
Posted - 2010-08-02 : 12:15:39
|
| Thank you all for responding.After combining several responses (also from other sites) I managed to solve my problem. |
 |
|
|
|
|
|
|
|