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 |
|
kiranmayi
Starting Member
4 Posts |
Posted - 2009-06-05 : 05:31:44
|
| hiiam using .net with sqlserver 2005i have 3 tables tblcustomers and tblcustomerpayments and tblzones.in tblcustomers i have id,customerid,name,status ....id and customerid is the primary keysin tblcustomerpayments i have id,customerid,foweek,amount,status..here id is the primary keyin tblzones i have id,status and so onnow i want customerid,name ,max(forweek),amount,sum(amount) from these tables select c.customerid,c.namefrom tblcustomers c inner join tblzones zon c.status=z.statusand z.status=1group by c.name,c.customeridand in the tblcustomerpayments table i want to select last paid amount paid by the customer with the week how group y customerid? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-05 : 13:07:46
|
| [code]select c.customerid,c.name,max(foweek),sum(amount)from tblcustomers c inner join tblzones zon c.status=z.statusand z.status=1inner join tblcustomerpayments cpon cp.customerid=c.customeridgroup by c.name,c.customerid[/code]for getting last paid amount do you have date column in tblcustomerpayments? also is id in tblcustomerpayments identity field? |
 |
|
|
kiranmayi
Starting Member
4 Posts |
Posted - 2009-06-06 : 00:46:22
|
| hitblcustomers(customerid is primary key)id customerid name status1 2001 meena 1tblcustomerpayments(primary key:id)id customerid amount forweek status1 1 125 2008-10-20 00:00:00.000 16 1 200 2008-11-03 00:00:00.000 19 1 157 2008-10-27 00:00:00.000 1 i want output like thisi want the list last paid amount paid by customer.customerid name amount forweek2001 meena 200 2008-11-03 00:00:00.000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-07 : 03:08:29
|
| [code]select c.customerid, c.name, cp.amount, cp.forweekfrom tblcustomers cinner join tblcustomerpayments cpon cp.customerid=c.customeridinner join (select customerid,max(forweek) as latest from tblcustomerpayments group by customerid) cp1on cp1.customerid=cp.customeridand cp1.latest=cp.forweek[/code] |
 |
|
|
kiranmayi
Starting Member
4 Posts |
Posted - 2009-06-08 : 01:20:14
|
| Thank you |
 |
|
|
|
|
|
|
|