我已经简化了我的问题并创建了这个示例以便更好地说明。
我的表:
CREATE TABLE `table_company` (
`source_id` INT(10) UNSIGNED NOT NULL,
`company_id` INT(10) UNSIGNED NOT NULL,
`clicks` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`source_id`,`company_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
我的数据:
INSERT INTO table_company VALUES
(1,100,8),(2,100,7),(3,200,9),(4,300,3),(5,100,4),(6,400,5),(7,100,10),(8,500,4),(9,500,9),(10,400,9);
SELECT source_id,company_id,AVG(clicks) AS avg_click FROM table_company
GROUP BY source_id,company_id
ORDER BY avg_click DESC;
给我:
source_id company_id avg_click
--------- ---------- -----------
7 100 10.0000
3 200 9.0000
9 500 9.0000
10 400 9.0000
1 100 8.0000
2 100 7.0000
6 400 5.0000
5 100 4.0000
8 500 4.0000
4 300 3.0000
我想写一个查询给我:
source_id company_id avg_click
--------- ---------- -----------
7 100 10.0000
3 200 9.0000
9 500 9.0000
10 400 9.0000
4 300 3.0000
即。只保留具有MAX(avg_click)
的company_id答案 0 :(得分:4)
SELECT source_id, company_id, MAX(avg_click) as max_click FROM
(SELECT source_id,company_id,AVG(clicks) AS avg_click FROM table_company
GROUP BY source_id,company_id
ORDER BY avg_click DESC) tmp
GROUP BY company_id
答案 1 :(得分:0)
SELECT MAx(source_id), company_id, MAX(avg_click)
from (SELECT source_id,company_id,AVG(clicks) AS avg_click FROM #table_company
GROUP BY source_id,company_id
)as tbl
GROUP by company_id
答案 2 :(得分:0)
您可以使用相关性:
<asp:GridView runat="server" ID="gridview2" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="weekofyear" HeaderText="Week" />
<asp:BoundField DataField="supportname" HeaderText="Name" />
<asp:BoundField DataField="supid" HeaderText="SupportIDName" Visible="false"/>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="TicketCompleted" runat="server" AutoPostBack="true" OnCheckedChanged="TicketCompletedCompleted_Click" Checked='<%# Convert.ToBoolean(Eval("TicketCompleted")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
您也可以使用WITH子句:
protected void TicketCompleted_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in dgRD.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Below captures the checkbox value (yes or no)
CheckBox chk = (CheckBox)row.FindControl("TicketCompleted");
if (chk.Checked)
{
//Here is where I will run the update
}
}
}