-1

我有一个包含以下字段的表。

Col A  Col B
-----  -----
I1      10
I1      10
I2      16
I3      14
I2      16

我需要一个应该给出如下输出的查询:

ColA   Col B  Count
----   -----  -----
I1      10      2
I2      16      2
I3      14      1

如何为此编写查询。请帮忙

4

7 回答 7

3
select colA, colB, count(colB) as `Count`
from your_table
group by colA, colB
于 2012-07-10T08:57:59.280 回答
1
SELECT ColA, ColB, Count(ColB) FROM T GROUP BY ColA, ColB
于 2012-07-10T08:58:25.520 回答
1

利用GROUP BY

SELECT  colA, colB, COUNT(colB) as Total
   FROM tableName
   GROUP BY colA, colB 
于 2012-07-10T08:59:48.227 回答
1

如果ColB对于 的特定值始终相同ColA,则可以使用max

select  ColA
,       max(ColB)
,       count(*)
from    YourTable
group by
        ColA

否则,您必须解释ColB结果中的值是如何计算的。

于 2012-07-10T06:27:02.987 回答
0

尝试这个:

SELECT ColA, colB, COUNT(colB) AS Count
FROM TableName
GROUP BY ColA,ColB 
ORDER BY ColA,ColB

请参阅此 SQL 小提琴。

于 2012-08-02T04:54:02.643 回答
0

请试试这个,让我知道

select [Col A] ,[Col B] ,count(*) [Count]
    from <table>
    group by [Col A] ,[Col B]
    order by [Col A]
于 2012-07-10T06:39:14.113 回答
0

您可以使用此解决方案:

SELECT ColA, ColB, COUNT(1) AS Count
FROM tbl
GROUP BY ColA, ColB
于 2012-07-10T06:44:33.750 回答