This may seem silly, but what is the countifs equivalent in sql server? I have 2 columns, and want to count where both repeat. So if column 1 is date and there are 2 dates of 1/1/2016 and column 2 is name and there are 2 names of Bill, how would I show this as a 3rd column called count, which would produce the number 2 as the result?
Asked
Active
Viewed 3,474 times
0
-
Can you please post sone example input data and example output data? – Dai Mar 28 '16 at 19:46
-
Have you tried a GROUP BY with any success? – codemonkeyliketab Mar 28 '16 at 19:48
-
Possible duplicate of [Sql Server equivalent of a COUNTIF aggregate function](http://stackoverflow.com/questions/582637/sql-server-equivalent-of-a-countif-aggregate-function) – Tab Alleman Mar 28 '16 at 20:21
2 Answers
4
if you are using sql server 2005 + then you can use the OVER() clause with COUNT Over Clause info
select
date,
name,
count(*) over (partition by date, name) cnt
from
table

JamieD77
- 13,796
- 1
- 17
- 27
1
SELECT ColumDate, ColumName, COUNT(*) as Count
FROM Table
GROUP BY ColumDate, ColumName

Hassan
- 1,413
- 1
- 10
- 12