To get a count of distinct values over a windowing partition:
DENSE_RANK() OVER (PARTITION BY e.Dept_Desc order by e.Staff_No)
+ DENSE_RANK() OVER (PARTITION BY e.Dept_Desc order by e.Staff_No desc)
- 1
Showing posts with label Count. Show all posts
Showing posts with label Count. Show all posts
Thursday, 24 May 2018
Thursday, 6 October 2011
Using Count function on a uniqueidentifier field - SQL
SELECT COUNT(D.DOCUMENTID) AS DocCount, P.TITLE
FROM DOCUMENT AS D inner join
PROJECT AS P ON D.PROJECTID = P.PROJECTID
GROUP BY P.PROJECTID, P.TITLE
When running this query I encountered the following error:
Msg 409, Level 16, State 2, Line 1
The count aggregate operation cannot take a uniqueidentifier data type as an argument.
To avoid this error when trying to Count a uniqueidetifier field you have to CAST it into a char :
SELECT COUNT(CAST(D.DOCUMENTID AS char(36))) AS DocCount, P.TITLE
FROM DOCUMENT AS D inner join
PROJECT AS P ON D.PROJECTID = P.PROJECTID
GROUP BY P.PROJECTID, P.TITLE
FROM DOCUMENT AS D inner join
PROJECT AS P ON D.PROJECTID = P.PROJECTID
GROUP BY P.PROJECTID, P.TITLE
When running this query I encountered the following error:
Msg 409, Level 16, State 2, Line 1
The count aggregate operation cannot take a uniqueidentifier data type as an argument.
To avoid this error when trying to Count a uniqueidetifier field you have to CAST it into a char :
SELECT COUNT(CAST(D.DOCUMENTID AS char(36))) AS DocCount, P.TITLE
FROM DOCUMENT AS D inner join
PROJECT AS P ON D.PROJECTID = P.PROJECTID
GROUP BY P.PROJECTID, P.TITLE
Labels:
aggregate,
Cast,
Count,
Msg 409,
uniqueidentifier
Subscribe to:
Posts (Atom)