Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query

Author  Topic 

dhinasql
Posting Yak Master

195 Posts

Posted - 2008-04-15 : 03:00:54
Hi i have a two table names are lk_votefor, and voting,

In voting table i have the column like voteforid, articleid
in lk_votewfor table have columnu like voteforid votefor

Example feelds are

voting
------

id voteforid articleid
1 1 7
2 1 7
3 1 8
4 1 7
5 2 4
6 2 7

lk_votefor
----------
voteforid Votefor
1 Event
2 Blog
3 Forum

here i want to know which article have more vote? it may b event,blog,or forum... But i want to know which article have most occurance in each type.

Here voteforid 1 (Event)occures 4 type, for article 7 , it occures 3 times, so this is the max count... Expected output is article 7 has max vote in Event

Please help me for geting the expected output...

Thanks in advance, its urgent pls

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-04-15 : 03:09:00
What have you tried so far?

HINT: Use GROUP BY
Go to Top of Page

dhinasql
Posting Yak Master

195 Posts

Posted - 2008-04-15 : 03:13:12
I have used this query , using this i can get max number of occurance of voteforid.

select voteforid,count(voteforid)
from voting
group by voteforid

But i want to know which article have most occurance in each type.

Here voteforid 1 (Event)occures 4 type, for article 7 , it occures 3 times, so this is the max count... Expected output is article 7 has max vote in Event
Go to Top of Page

dhinasql
Posting Yak Master

195 Posts

Posted - 2008-04-15 : 03:23:24
If u need more expl. plz ask me, its urgent help me
Go to Top of Page

Vadivu
Starting Member

31 Posts

Posted - 2008-04-15 : 03:27:32
Check if this query works... pls anyone correct me if i am wrong...

select top 1 count(voteforid) AS NoOfoccurance,voteforid,articleid
from voting
group by voteforid, articleid
order by count(voteforid) desc
Go to Top of Page

dhinasql
Posting Yak Master

195 Posts

Posted - 2008-04-15 : 05:57:08
Hi Friend

Thanks for Your valuable help... Thank u very much
Go to Top of Page
   

- Advertisement -