Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All i have a table in SQL as shown below. I want to display top most vote getting image per week.

HTML
vote	imagename	weekd
4	a	         1
3	b	         1
1	c	         1
1	d	         1
2	c	         2
3	d	         2


Ans
Week 1 -a
Week 2 -d

Please help
Posted
Updated 13-Nov-13 19:52pm
v2
Comments
Mehdi Gholam 13-Nov-13 9:18am    
week 1 should be a, right?
Raj tilak Bose 13-Nov-13 9:20am    
No for Week 1 :- display will a
No for Week 2 :- display will d
Thava Rajan 13-Nov-13 9:33am    
give the table name please

something like this should work

SELECT vote, imagename, weekd FROM <tableName> GROUP BY vote, weekd ORDER BY vote DESC
 
Share this answer
 
SQL
CREATE Table #Temp
( vote int,imagename varchar(20),weekid int)

INSERT INTO #Temp
SELECT 4,'a',1
UNION
SELECT 3,'b',1
UNION
SElect 1,'c',1
UNION
Select 1,'d',1
UNION
SElect 2,'c',2
UNION
Select 3,'d',2


SELECT T.* FROM #Temp T
INNER JOIN (SELECT max(vote) as vote, weekid FROM #Temp GROUP BY weekid) SQ ON SQ.weekid = T.Weekid AND SQ.vote = T.Vote
ORDER BY weekid

Drop TABLE #Temp
 
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