Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot


Pivot

Author
Message
UnionAll
UnionAll
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 637
Hello,
I am trying to count distinct employees by month for each department. Below is my source table.
IF OBJECT_ID('TempDB..#Table') IS NOT NULL
DROP TABLE #Table
CREATE TABLE #Table (Department VARCHAR(20), EmpID INT, [Month] INT)
INSERT INTO #Table
SELECT 'Marketing',268638,3 UNION ALL
SELECT 'Marketing',268638,3 UNION ALL
SELECT 'Marketing',268638,6 UNION ALL
SELECT 'Marketing',268638,6



The result set should look like this.
IF OBJECT_ID('TempDB..#Results') IS NOT NULL
DROP TABLE #Results
CREATE TABLE #Results (Department VARCHAR(20),[1] INT,[2] INT, [3] INT,[4] INT,[5] INT,[6] INT,[7] INT,[8] INT,[9] INT,[10] INT,[11] INT,[12] INT)
INSERT INTO #Results
SELECT 'Marketing',0,0,1,0,0,1,0,0,0,0,0,0



There is the same employee 268638 twice in the month of 3(March) and 6(June). I need to pivot the result set with distinct counts by month.
Any help please! Thanks.
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5726 Visits: 13305
You can pre-aggregate the results in the pivot source:


IF OBJECT_ID('TempDB..#Table') IS NOT NULL
DROP TABLE #Table
CREATE TABLE #Table (Department VARCHAR(20), EmpID INT, [Month] INT)
INSERT INTO #Table
SELECT 'Marketing',268638,3 UNION ALL
SELECT 'Marketing',268638,3 UNION ALL
SELECT 'Marketing',268638,6 UNION ALL
SELECT 'Marketing',268638,6





IF OBJECT_ID('TempDB..#Results') IS NOT NULL
DROP TABLE #Results
CREATE TABLE #Results (Department VARCHAR(20), [1] INT,[2] INT, [3] INT,[4] INT,[5] INT,[6] INT,[7] INT,[8] INT,[9] INT,[10] INT,[11] INT,[12] INT)



INSERT INTO #Results
SELECT Department,
   ISNULL([1],0),
   ISNULL([2],0),
   ISNULL([3],0),
   ISNULL([4],0),
   ISNULL([5],0),
   ISNULL([6],0),
   ISNULL([7],0),
   ISNULL([8],0),
   ISNULL([9],0),
   ISNULL([10],0),
   ISNULL([11],0),
   ISNULL([12],0)
FROM (
   SELECT Department, Month, CNT = COUNT(DISTINCT EmpId)
   FROM #Table
   GROUP BY Department, Month
) AS SRC
PIVOT (SUM(CNT) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS PVT


SELECT * FROM #Results



Does it do the trick for you?

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
UnionAll
UnionAll
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 637
Perfect. Thank you so much.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search