0

次のようなテーブル構造があります。

ClassId
ClassDescription

私のテーブルのデータは次のようになります。

ClassId  ClassDesc
4           --- 
4           ---  
4           ---
4
4
2
2
2

今私がやろうとしているのは、classId個々のclassId意味に基づいてのカウントを取得することです。例として、上記の表は、classId = 4 のすべてに対して 5 のカウントを返し、classId = 2 に対して 3 のカウントを返します。

したがって、私のSqlステートメントはそれらを個別にカウントし、Order BY DESC On Count of ClassId
4は5に
数えます 2は3に数えます

最終的な出力は次のようになります。

ClassId       ClassDesc
    4           ----  
    2           ----
4

3 に答える 3

0

これを試して:

SELECT ClassId, COUNT(ClassDesc) AS ClassDesc
FROM MyTable
GROUP BY ClassId
ORDER BY COUNT(ClassDesc) DESC;

出力:

╔═════════╦═══════════╗
║ CLASSID ║ CLASSDESC ║
╠═════════╬═══════════╣
║       4 ║         5 ║
║       2 ║         3 ║
╚═════════╩═══════════╝

このSQLFiddleを参照してください

于 2013-05-09T05:45:22.667 に答える
0

あなたの質問のタイトルはあなたの答えです。あなたは少し努力する必要があります。

以下のようなものを試してみたい:

declare @temp  table
(
classid int,
classdesc varchar(10)
)

insert into @temp
select 4, 'test'
union all
select 4, 'test1'
union all
select 4, 'test2'
union all
select 4, 'test 3'
union all
select 4, 'test 4'
union
select 2, 'test'
union
select 2, 'test 2'

select classid, count(*) classcount
from @temp
group by classid
order by classcount desc

それが役立つことを願っています!

于 2013-05-09T05:49:25.987 に答える
0

グループ化の使用

  SELECT classID, ClassDescription FROM  
     (SELECT classID, ClassDescription, COUNT(1) cnt FROM  mytable GROUP
      BY classID, ClassDescription) AS A
            ORDER BY cnt DESC

また

   SELECT classID, ClassDescription FROM  <table> GROUP BY classID, ClassDescription
ORDER BY COUNT(1) DESC
于 2013-05-09T05:41:09.497 に答える