我有一张表格,其中每个id
作为pk有多个tasks_code
。我正在尝试将相对tasks_code
分组到其id
。当我在代码下面执行时,group by
函数不起作用。如果我只需要按两列分组,但选择多列显示,如何按功能分组使用?
Select i.id, p.tasks_code, count(*)
From problem p
Left join id_code i On p.id = i.id
Group by i.id
结果返回为:
id tasks_code count(*)
1 1 1
2 3 1
1 2 1
1 4 1
2 5 1
1 9 1
我希望它将结果返回为:
id count(*)
1 4
2 2
答案 0 :(得分:1)
将字段名称放在count和group by。
中SELECT
i.id,
count(p.tasks_code) as count_codes
FROM problem p
LEFT JOIN id_code i
ON p.tasks_code = i.id
GROUP BY i.id
I wanted to also give you a quick test and another way to add to your tool belt.
COUNT(FLD) works but you can use COUNT() OVER() as a window function. Same result.
-- Simple test
select * into #test
from
(
values
(1, 1, 1),
(2, 3, 1),
(1, 2, 1),
(1, 4, 1),
(2, 5, 1),
(1, 9, 1)
) as work(id, task, cnt);
go
-- group solution
select id, count(task) as task_cnt
from #test
group by id;
-- windowing solution
select distinct id, count(*) over(partition by id) as task_cnt
from #test
-- Remove temp table
drop table #test;
答案 1 :(得分:0)
使用此HTML>>
<script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>
<script data-require="ng-table@*" data-semver="0.3.0" src="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.js"></script>
<link data-require="ng-table@*" data-semver="0.3.0" rel="stylesheet" href="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.css" />
<link data-require="bootstrap-css@*" data-semver="3.0.0" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<table ng-table="tableParams" class="table">
<tbody ng-repeat="group in $groups">
<tr class="ng-table-group">
<td colspan="{{$columns.length}}">
<a href="" ng-click="group.$hideRows = !group.$hideRows">
<span class="glyphicon" ng-class="{ 'glyphicon-chevron-right': group.$hideRows, 'glyphicon-chevron-down': !group.$hideRows }"></span>
<strong>{{ group.value }}</strong>
</a>
</td>
</tr>
<tr ng-hide="group.$hideRows" ng-repeat="user in group.data">
<td sortable="name" data-title="'Name'">
{{user.name}}
</td>
<td sortable="age" data-title="'Age'">
{{user.age}}
</td>
</tr>
</tbody>
</table>
和此脚本&gt;&gt;
分组表
Preview
Edit
Code
index.html
script.js
style.css
的script.js
var app = angular.module(&#39; main&#39;,[&#39; ngTable&#39;])。
控制器(&#39; DemoCtrl&#39;,功能($ scope,$ filter,ngTableParams){
var data = [{name: "Moroni", age: 50, role: 'Administrator'},
{name: "Tiancum", age: 43, role: 'Administrator'},
{name: "Jacob", age: 27, role: 'Administrator'},
{name: "Nephi", age: 29, role: 'Moderator'},
{name: "Enos", age: 34, role: 'User'},
{name: "Tiancum", age: 43, role: 'User'},
{name: "Jacob", age: 27, role: 'User'},
{name: "Nephi", age: 29, role: 'Moderator'},
{name: "Enos", age: 34, role: 'User'},
{name: "Tiancum", age: 43, role: 'Moderator'},
{name: "Jacob", age: 27, role: 'User'},
{name: "Nephi", age: 29, role: 'User'},
{name: "Enos", age: 34, role: 'Moderator'},
{name: "Tiancum", age: 43, role: 'User'},
{name: "Jacob", age: 27, role: 'User'},
{name: "Nephi", age: 29, role: 'User'},
{name: "Enos", age: 34, role: 'User'}];
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
groupBy: 'role',
total: data.length,
getData: function($defer, params) {
var orderedData = params.sorting() ?
$filter('orderBy')(data, $scope.tableParams.orderBy()) :
data;
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
})