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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-03-23 : 08:51:06
|
| Graham writes "Get ALL the columns from a table, where the record is active. Table:SAL_ProdCommProductCode,CorpID,ConsultantTypeID,DateFrom,HiVal,LoValAn Active record is defined as a ProductCode and ConsultantID with the latest (max) Date.This Gives me the desired Result:SELECT ProductCode, ConsultantTypeID, MAX(DateFrom) AS DteFromFROM SAL_ProdCommWHERE (CorpID = 23)GROUP BY ProductCode, ConsultantTypeIDHowever I need to return all the columns including the HiVal & LowVal Columns in the result. When I try this below, I get inconsistent results, by the looks of it, if there is only one record for the combination, it is not returned.Select ProductCode,ConsultantTypeID, DateFrom, corpid, HiVal,LoVal from Sal_ProdComm r Join (Select ProductCode as PC,Max(DateFrom) as DDate, ConsultantTypeID as CT From Sal_ProdComm Group by ProductCode, consultantTypeID)a on r.productcode=a.pC and r.datefrom=a.ddate and r.ConsultantTypeId=a.CT where Corpid=23Many ThanksGraham---------------Cape Town South Africa" |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-03-23 : 09:13:46
|
| Put the where CorpID = 23 in your derived table and try that.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|
|
|
|
|