2014-10-10 119 views
1
partyname state  constituency  candidatename district votes 

BJP  Maharashtra Nagpur-East  Nitin Gadkari Nagpur  1200 
AAP  Maharashtra Nagpur-East  Arvind Kejriwal Nagpur  750 
BJP  Maharashtra Nagpur-West  Vicky   Nagpur  4800 
AAP  Maharashtra Nagpur-West  Lucky   Nagpur  3500 
BJP  Maharashtra Nagpur-North Rakesh   Nagpur  100 
AAP  Maharashtra Nagpur-North Ravan   Nagpur  1500 
下面

只選擇最大值的計數我預計輸出上表SQL查詢從表

查詢通過partyname和選民選擇投票組只最大vlue並顯示如下圖所示的輸出。

partyname  Count 

BJP   2 

AAP   1 

bcoz從那格浦爾-east和那格浦爾以西

BJP引線,因此顯示計數2

和AAP從那格浦爾商機 - 北方則僅顯示1個計數。

PLZ建議我詢問......

+0

你可以提供結構和示例數據在sql小提琴 – 2014-10-10 07:27:06

+0

我有板條箱上的所有表架構和數據... plz幫助.. – 2014-10-10 07:42:34

+0

提供的答案與SQL小提琴鏈接。 – 2014-10-10 07:43:34

回答

0

在MySQL中,你需要一個連接來獲得最大票數,然後再獲得派對名稱和勝數:

select c.partyname, count(*) as numwins 
from constituency c join 
    (select constituency, max(votes) as maxvotes 
     from constituency c 
     group by constituency 
    ) cm 
    on c.constituency = cm.constituency and c.votes = cm.votes 
group by c.partyname;