Solved

SQL Query

Posted on 2011-02-15
4
373 Views
Last Modified: 2012-05-11
Hi,

I have a following  table

consumerno                   billdate                   billamount
54321                              01/01/2011          100
54322                               15/01/2011            50
54321                               02/02/2011              175
54322                               03/02/2011             100


i want a query that will give me bill amount for a consumerno with latest bill date.

i,.e.
consumerno                billdate                           billamount
54321                             02/02/2011                   175
54322                              03/02/2011                  100



Please Help
0
Comment
Question by:santoshmotwani
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34903102
If this is SQL Server 2005+ then you can use row_number()

select consumerno, billdate, billamount
from
(
select *, row_number() over (partition by consumerno order by billdate desc) as rownum
from tbl
) SQ
where rownum=1
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 34903138
great ,

what if i want total billamount ?


Thnx for your help
0
 
LVL 16

Author Closing Comment

by:santoshmotwani
ID: 34903171
thnx !!!
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34909483
>what if i want total billamount ?

In that case, for the exact 3 columns required, a simply group by would suffice.

select consumerno, max(billdate) billdate, sum(billamount) totalamount
from tbl
group by consumerno
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question