?
Solved

SQL Query

Posted on 2011-02-15
4
Medium Priority
?
382 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

752 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