Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Assume that you have multiple record of customer transactions but you only want one record for each customer with minimum transaction date field... How would you do it ????

Please check the following query

Click here to copy the following block
create table #tmp(cust_code varchar(10),cdate datetime,amount money)

insert into #tmp values('BW','1/1/07',1000)
insert into #tmp values('BW','1/2/07',500)
insert into #tmp values('BW','1/3/07',4000)
insert into #tmp values('MS','1/1/07',23000)
insert into #tmp values('MS','1/2/07',10)

select a.cust_code,a.cdate,a.amount
from (select t.cust_code,t.cdate,t.amount
    from #tmp t where cdate in (select min(cdate) from #tmp where cust_code=t.cust_code)
    group by t.cust_code,t.cdate,t.amount
    ) a
group by a.cust_code,a.cdate,a.amount

go

drop table #tmp

/*
Output :

cust_code cdate          amount
---------- ----------------------- ---------------------
BW     2007-01-01 00:00:00.000 1000.00
MS     2007-01-01 00:00:00.000 23000.00

*/


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.