Solved

SQL Query

Posted on 2011-02-15
4
344 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now