Solved

SQL Query

Posted on 2011-02-15
4
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

717 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