Reputation: 15
I have a table like this:
c1|c2
a| 2
b| 1
a| 1
b| 3
a| 2
how count this to get result like this
col1| col2| number of occurrences
a| 1| 1
a| 2| 2
b| 3| 1
And after get max value like this:
col1col2
a|2
b|1
Upvotes: 1
Views: 722
Reputation: 1912
Count 1 is your friend, count * leads to a longer load time.
SELECT C1, C2, COUNT(1) AS numberofoccurrences
FROM TABLE
GROUP BY C1, C2
Sorry for delay. To get Max use following;
; WITH CTE AS(
SELECT c1, c2, COUNT(1) AS NoOfOccurence FROM TEST
GROUP BY c1, c2
), BTE AS(
SELECT c1, c2, ROW_NUMBER() OVER(PARTITION BY c1 ORDER BY NoOfOccurence DESC) AS RN FROM CTE
)SELECT c1, c2 FROM BTE WHERE RN = 1
Upvotes: 2
Reputation: 7918
Here's a complete solution which includes the final max value output. Also note that an index on c1 and c2 will drastically improve performance.
-- Sample data
-- including an id for use as a tie-breaker for a unique pk/clustered index on c1 & c2
DECLARE @tbl TABLE
(id int identity not null, c1 char(1), c2 tinyint, primary key (c1,c2,id));
INSERT @tbl VALUES ('a', 2), ('b', 1), ('a', 1), ('b', 3), ('a', 2);
-- Solution
SELECT c1, MaxOccurs = MAX(c)
FROM
(
SELECT c1, c2, c = COUNT(1)
FROM @tbl
GROUP BY c1, c2
) x
GROUP BY c1;
Upvotes: 0