Click here to Skip to main content
15,883,771 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i have two table.
one is
section table
____________

id name class section
1 sankar VI A
2 pandian VI A
3 raja III C

second one is
paid_table
_____________

id sectionid paid_amount Balance
100 1 500 200
101 1 200 0
102 2 1000 500
103 2 200 300

i need is


name balance

101 0
103 300


-------------------------------------------
what should i do for generate query....

( i need last transaction of balance for VI class A section student. )
Posted
Comments
Amir Mahfoozi 10-Dec-11 0:11am    
Why don't you save pay date time ? you will get into trouble probably in the future.

Just off the top of my head, not tested
SQL
SELECT Name, Balance
FROM section
JOIN paid_table ON paid_table.sectionid = section.id
WHERE paid_table.id = (SELECT MAX(id) FROM paid_table WHERE sectionid = section.id)
 
Share this answer
 
Comments
Rubaba 11-Dec-11 6:23am    
5+
You could also write it in this way:

SQL
SELECT Name, Balance FROM section, paid_table
WHERE paid_table.sectionid = section.id AND 
paid_table.id = (SELECT MAX(id) FROM paid_table WHERE sectionid = section.id)
 
Share this answer
 
I love to use the RANK() function.
I have been successful in improving performance using it, especially if you have to deal with very large tables.

SQL
SELECT t.Name, t.Balance
FROM
	(
	SELECT	s.Name AS Name
		, p.Balance As Balance
		, RANK() OVER (PARTITION BY s.name ORDER BY p.id DESC) as rowNum
	FROM section s
	INNER JOIN paid_table p
	ON p.sectionid = s.id
	) t
Where t.rowNum=1
 
Share this answer
 
Comments
Monjurul Habib 10-Dec-11 15:17pm    
review your code it will return 103 as first row.
Try the following, and hope it will work;

SQL
SELECT     sec.name, paid.Balance
FROM         section sec INNER JOIN
                      paid_table paid ON paid.sectionid = sec.id
WHERE     (paid.id =
                          (SELECT     MAX(id)
                            FROM          paid_table
                            WHERE      (sectionid = sec.id)))
 
Share this answer
 
v2
Comments
RaviRanjanKr 10-Dec-11 16:13pm    
My 5!
Monjurul Habib 10-Dec-11 17:51pm    
thank you
Rubaba 11-Dec-11 6:22am    
5+
Monjurul Habib 11-Dec-11 7:43am    
thank you rubaba
thatraja 11-Dec-11 7:21am    
5!
Try this one:

SQL
SELECT       name, balance
FROM         section
INNER JOIN   paid_table ON paid_table.sectionid = section.sectionid
WHERE        paid_table.id = (SELECT max(id) FROM paid_table 
                                             WHERE class = 'VI'
                                             AND section = 'A')


*remove max if you want to get 101 and 103 :) what i did here is what you said on your last sentence. :)
( i need last transaction of balance for VI class A section student. )

Regards,
Eduard
 
Share this answer
 
v2
Comments
Monjurul Habib 10-Dec-11 15:14pm    
your script is not ok. it will return only one.
[no name] 11-Dec-11 7:53am    
because the OP said

( i need last transaction of balance for VI class A section student. )

and i am quite confused with his result query because it returned 2 rows. I just followed what he said from the highlighted part.
Monjurul Habib 11-Dec-11 8:16am    
As sankar & pandian belongs to class=VI & section=A, it should return both.
[no name] 13-Dec-11 20:34pm    
thank u for deducting 16 consecutive pts whoever u are! hater! :)

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