Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
I am having a table ba
date          baname        bal
2013-12-23    ib             10000
2013-12-23    ib             9500
2013-12-23    ib             9000
2013-12-23    hb             6726
2013-12-23    hb             8354.40
2013-12-23    ib             9500


Now i need to get the value of last bal for each baname
i.e., Output is like
date          baname        bal
2013-12-23    hb            8354.40
2013-12-23    ib            9500

Suggest a solution
Posted
Updated 4-Jan-14 3:56am
v3
Comments
Maciej Los 4-Jan-14 9:56am    
Hey! Do not remove your questions, if there are answers!!!
Rather than removing, improve question.
Respect CP QA rules!
sivamzcet 4-Jan-14 10:03am    
i was not removed but some members closed my question. I dont know why?
Suggest me a correct solution
CPallini 4-Jan-14 12:37pm    
In my opinion your problem is ambiguous, since there is not an exact order in your requirement.

Please, read my comment to the question first.

You can't remove your question if there exists one or more answer. Please, read CP QA rules[^].

There were interesting solutions for MS SQL server where ROW_NUMBER function was used. Unfortunately, MySQL does not provides ROW_NUMBER function, but there is a hope ;) - using variables.

Try this:
SQL
SELECT date,
   (
    CASE baname
       WHEN @curType THEN @curRow := @curRow + 1
       ELSE @curRow := 1 AND @curType := baname
     END) +1 AS RowNo,
    baname,
    bal
FROM tmp t2, (SELECT @curRow := 0, @curType := '') t1
WHERE `date` = '2013-12-23' 
ORDER BY baname 


Above query returns:
|                            DATE | ROWNO | BANAME |    BAL |
|---------------------------------|-------|--------|--------|
| December, 23 2013 00:00:00+0000 |     1 |     hb |   6726 |
| December, 23 2013 00:00:00+0000 |     2 |     hb | 8354.4 |
| December, 23 2013 00:00:00+0000 |     1 |     ib |  10000 |
| December, 23 2013 00:00:00+0000 |     2 |     ib |   9500 |
| December, 23 2013 00:00:00+0000 |     3 |     ib |   9000 |
| December, 23 2013 00:00:00+0000 |     4 |     ib |   9500 |


All what you need to do now, is to select baname with the highest RowNo. Tip: use MAX() function ;)

SQLFiddle example[^]


[EDIT]
It should produce expected result set:
SQL
SELECT date, MAX(RowNo), baname, bal
FROM (
  SELECT date,
   (
    CASE baname
       WHEN @curType THEN @curRow := @curRow + 1
       ELSE @curRow := 1 AND @curType := baname
     END) +1 AS RowNo,
    baname,
    bal
  FROM tmp t2, (SELECT @curRow := 0, @curType := '') t1
  WHERE `date` = '2013-12-23' 
  ORDER BY baname 
) T
GROUP BY T.baname

[/EDIT]
 
Share this answer
 
v3
Comments
sivamzcet 4-Jan-14 10:44am    
nice query :)
But i need only that two values as output. I cant get when i put Max(baname)
Maciej Los 4-Jan-14 10:57am    
Not MAX(baname), but MAX(RowNo) ;)
sivamzcet 4-Jan-14 11:00am    
SELECT baname,
(
CASE baname
WHEN @curType THEN @curRow := @curRow + 1
ELSE @curRow := 1 AND @curType := baname
END) +1 AS RowNo,
baname,
bal
FROM tmp t2, (SELECT @curRow := 0, @curType := '') t1
WHERE `date` = '2013-12-23'
ORDER BY baname,MAX(RowNo)

is this right?
Maciej Los 4-Jan-14 11:04am    
No ;(
sivamzcet 4-Jan-14 11:08am    
Ouch!
HI Siva,

this may not be the efficient solution.
but this will gives you the expected result..

SQL
declare @tbloutput table ( datee varchar(20), baname varchar(15) , bal int , ignore int )
declare @baname varchar(15)
declare cur cursor for select distinct baname from test open cur
fetch next from cur into @baname
while @@FETCH_STATUS =0
begin
 insert into @tbloutput select top 1  datee, baname, bal,  ROW_NUMBER() over ( order by datee) as Number  from test where baname = @baname  order by Number desc
fetch next from cur into @baname
end
close cur
deallocate cur
select datee,baname,bal from @tbloutput
 
Share this answer
 
Comments
sivamzcet 4-Jan-14 22:52pm    
thanks for your query!! but i cant able to get correct ans
Karthik_Mahalingam 4-Jan-14 23:22pm    
try this link

sqlfiddle
Karthik_Mahalingam 4-Jan-14 23:31pm    
its is working fine...
sivamzcet 5-Jan-14 1:20am    
but in mysql its not working.. i tried using ctrl c + v and changed column name as per my table column name
Karthik_Mahalingam 5-Jan-14 1:22am    
its sql query..
tell me, cursor can be used in mysql ??
Select a.bname, (select b.bal from banktable b where a.baname=b.baname order by b.date desc limit 1) from banktable a group by a.baname
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900