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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 subquery - question

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-02-14 : 08:11:38
Rohit writes "I am having trouble creating a query to get a result set I want. Please help me on this.

I have a table "customerHistory" as follows.

customerId varchar(10) not null primary key,
modifiedColomnName varchar(10) not null,
newValue varchar(10),
dateModified datetime.

Below are the rows in the table.
1) 1111111, customerName, Hector, 20050101
2) 2222222, customerName, Mike , 20050104
3) 1111111, customerAge, 45, , 20050103
4) 1111111, customerName, Michael, 20050106
5) 1111111, customerAge, 55, 20050106

From the data you will come to know that,
for the customerId 1111111 , the customerName was modified to Hector on 20050101. For the same customerId 1111111, the customerName was again modified on 20050106.

I want a query such that if I enter the dateModified = '20050106' and modifiedColomnName='customerName'
it should give me the lastest customerId , newValue before that day. one record per customerId. So in the above case it will return

2222222, customerName, Mike , 20050104
1111111, customerName, Michael, 20050106

I am trying to track the lastest information of a certian colomn on a given day for all the customers."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-02-14 : 08:22:15

Select * from Table T where datemodified in (Select Max(DateModified) from Table where CustomerId=T.CustomerId group by CustomerId)

Madhivanan
Go to Top of Page
   

- Advertisement -