按累积列值对mysql结果进行分组

时间:2016-01-19 02:48:43

标签: mysql sql-server

我有一个数据库表 <!DOCTYPE html> <html lang="en"> <head> <!-- title and meta --> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv='REFRESH' content='43200'/> <meta name="Pragma" content="no-cache;"> <title>Web</title> <link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link type="text/css" rel="stylesheet" href="css/theme.css"> </head> <body ng-app="app"> <div ng-init="init()"> <div class="module"> <div class="module-head"> <center><h3> <b>Evaluation</b> </h3></center> </div> <center> <div class="module-body" > <div> Questionnaire About the Project/Initiative</div> <hr> <form name="qForm" ng-submit="evaluate_Report(qForm.$valid)" novalidate> <hr> <div>Question Set</div> <hr> <div ng-repeat="myQuestion in questionnaire"> <table id="personal_table" class="table table-striped table-bordered table-hover" style="width:70%"> <tbody> <hr> <center><thead>{{myQuestion.header}}</thead></center> <tr style="width:100%"> <th rowspan="{{questionnaire[$index].answer.length+1}}" scope="rowgroup"style="width:50%">{{myQuestion.question}}</th> <th style="width:2%">:</th> <th style="width:48%">Tick all that apply <div ng-show="qForm.$submitted && qForm.ansCheck.$error.required">Please select atleast one checkbox</div> </th> </tr> <tr ng-repeat="answer in questionnaire[$index].answer"> <td><input type="checkbox" name="ansCheck" ng-model="answer.isChecked" ng-change="selectAnswer($parent.$index, $index, answer)" ng-required="!anySelected($parent.$index,answer)" > </td><td> {{answer.value}} </td> </tr> <tr style="width:100%"> <td style="width:50%" id="q2">Special Notes</td> <td style="width:1%">:</td> <td style="width:49%"><input type=text ng-model="personal_info_notes">{{myQuestion.notes}} </td> </tr> </tbody> </table> </div> <center><button type=submit id="btn_evaluate">Evaluate Report</button></center> <hr> <hr> </div> </div> </div> </center> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src= "scripts/core/angular/angular.js"></script> <script src= "scripts/core/angular/angular-route.js"></script> <script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script> <script src="scripts/controllers/questionnaire.js" type="text/javascript"></script> </body> </html>和一个表var aString="\"abcde\""; //where this is yout variable 。为特定事件下的所有投注都位于aString.replace(/"/g,""); 表中,而有关该事件的信息存储在events表中。

假设我有这些表格:

bets

我希望能够按bets排序,即为该事件投放了多少投注,并events即该事件的总金额。

按奖品分类

显然这个查询不起作用,但我想做这样的事情:

events table:
id    event_title
1     Call of Duty Finals
2     DOTA 2 Semi-Finals
3     GTA V Air Race


bets table:
id    event_id    amount
1     1           $10
1     2           $50
1     2           $100
1     3           $25
1     3           $25
1     3           $25
上面查询中的

popularity应该是添加在一起的prize的所有下注金额的累积值,因此此查询将返回

SELECT * FROM bets GROUP BY event_id SORT BY amount

按人气分类

显然这个查询也不起作用,但我想做这样的事情:

amount
上面查询中的

event_id应该是Array ( [0]=>Array( 'event_id'=>2 'amount'=>$150 ) [1]=>Array( 'event_id'=>3 'amount'=>$75 ) [2]=>Array( 'event_id'=>1 'amount'=>$10 ) ) 表中存在的行数,因此此查询将返回

SELECT * FROM bets GROUP BY event_id SORT BY total_rows

我不一定需要它来返回total_rows值,因为我可以计算它,但它确实需要按{{1}中特定bets的出现次数排序表格。

2 个答案:

答案 0 :(得分:1)

我认为数和总和是你的朋友:

SELECT COUNT(event_id) AS NumberBets, 
    SUM(amount) AS TotalPrize
FROM bets
GROUP BY event_id

应该做的伎俩。 然后,您可以根据需要订购NumberBets(流行度)或TotalPrize。只有在需要事件标题时才需要加入。

答案 1 :(得分:1)

您可以使用SUMCOUNT汇总功能:

SELECT
    e.id AS event_id, SUM(amount) AS sum_amount
FROM [events] e
LEFT JOIN bets b
    ON b.event_id = e.id
GROUP BY
    e.id
ORDER BY
    sum_amount DESC

SELECT
    e.id AS event_id, COUNT(e.event_id) AS no_of_events
FROM [events] e
LEFT JOIN bets b
    ON b.event_id = e.id
GROUP BY
    e.id
ORDER BY
    no_of_events DESC