2014-10-04 56 views
-1

我有一個表,其中每個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

id count(*) 
1 4 
2 2 
+0

我希望它返回的ID,COUNT(*); 1 4; 2 2 – baciyan 2014-10-04 02:22:24

+0

爲什麼我不能選擇p.tasks_code? – baciyan 2014-10-04 02:27:07

+1

結果是美國廣播公司,我想只是AC(所以你爲什麼不嘗試從查詢中刪除B?)... – 2014-10-04 02:29:12

回答

1

將字段名的次數和組內。

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; 
+0

它的工作原理!對於我正在處理的表格,我需要5列和3個連接。你能給我一些關於如何去做的提示嗎?對不起,我沒有足夠的積分投票 – baciyan 2014-10-04 02:29:26

+0

這是一次性adhoc查詢?如果是這樣,請將數據轉儲到#LOCALTEMP表。然後運行選擇。對於多個聯接,只需繼續聲明即可。有關更多示例,請參閱聯機叢書(MSDN)FROM子句。 – 2014-10-04 02:38:12

+0

+1,你在我之前,我還在鍵入@CRAFTY DBA – radar 2014-10-04 02:43:53

0

使用此HTML >>

<script data-require="[email protected]*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script> 

<script data-require="[email protected]*" data-semver="0.3.0" src="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.js"></script> 



<link data-require="[email protected]*" data-semver="0.3.0" rel="stylesheet" href="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.css" /> 

<link data-require="[email protected]*" 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> 

和該腳本>>

表與分組

Preview 
Edit 
Code 

index.html 
script.js 
style.css 

的script.js

變種應用= angular.module( '主', [ 'ngTable'])。

控制器( 'DemoCtrl',函數($範圍,$過濾器,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())); 

    } 

}); 

})