Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a reasonably equivalent solution using Pivot.</p> <pre><code>Declare @Comm TABLE ( [CommunicationId] uniqueidentifier PRIMARY KEY DEFAULT NEWID(), [Type] nvarchar(1), -- ('C' for call, 'M' for meeting, etc.) [Date] datetime ) Declare @Person TABLE ( [PersonId] uniqueidentifier PRIMARY KEY DEFAULT NEWID(), [Type] Nvarchar(1) -- ('E' for employee, 'C' for customer) ) Declare @PersonComm TABLE ( [PersonId] uniqueidentifier, -- (Can contain GUIDs for employees or clients, see Person Table below) [CommunicationId] uniqueidentifier ) INSERT INTO @Person(Type) Select 'C' UNION ALL Select 'E' UNION ALL Select 'C' UNION ALL Select 'E' INSERT INTO @Comm([Type],[Date]) Select 'C', '01/04/2010' UNION ALL Select 'C', '01/04/2010' UNION ALL Select 'C', '04/04/2010' UNION ALL Select 'C', '05/01/2010' UNION ALL Select 'C', '08/04/2009' UNION ALL Select 'C', '09/01/2009' UNION ALL Select 'M', '01/04/2010' UNION ALL Select 'M', '03/20/2010' UNION ALL Select 'M', '04/04/2010' UNION ALL Select 'M', '06/01/2010' UNION ALL Select 'M', '04/10/2009' UNION ALL Select 'M', '04/10/2009' INSERT INTO @PersonComm Select E.PersonID , Comm.[CommunicationId] FROM @Person E ,@Comm Comm Where E.[Type] = 'E' INSERT INTO @PersonComm Select E.PersonID , Comm.[CommunicationId] FROM @Person E ,@Comm Comm Where E.[Type] = 'C' Select EmployeeID, ClientID, Year, [JanuaryC] AS [Jan Calls], [JanuaryM] AS [Jan Meetings], [FebruaryC], [FebruaryM], [MarchC], [MarchM], [AprilC], [AprilM], [MayC], [MayM], [JuneC], [JuneM], [JulyC], [JulyM], [AugustC], [AugustM], [SeptemberC] , [SeptemberM], [OctoberC] , [OctoberM], [NovemberC], [NovemberM], [DecemberC], [DecemberM] FROM ( Select P.PersonId EmployeeID, Client.PersonId ClientID, YEAR(C.Date) Year, DateName(m,C.Date) Month, COUNT(*) Amount, C.Type CommType, DateName(m,C.Date) + C.Type PivotColumn -- JanuaryC FROM @Comm C INNER JOIN @PersonComm PC ON PC.CommunicationId = C.CommunicationId INNER JOIN @Person P ON P.PersonId = PC.PersonId INNER JOIN @PersonComm PCC ON PCC.CommunicationId = PC.CommunicationId INNER JOIN @Person Client ON Client.PersonId = PCC.PersonId AND Client.Type = 'C' Where P.Type = 'E' Group By P.PersonId, CLient.PersonId, YEAR(C.Date), DateName(m,C.Date), C.Type ) SourceTable PIVOT ( MAX(Amount) FOR PivotColumn IN ([JanuaryC], [JanuaryM],[FebruaryC], [FebruaryM],[MarchC], [MarchM], [AprilC], [AprilM], [MayC], [MayM], [JuneC], [JuneM], [JulyC], [JulyM], [AugustC], [AugustM],[SeptemberC] , [SeptemberM],[OctoberC] ,[OctoberM],[NovemberC], [NovemberM], [DecemberC], [DecemberM] ) )As PivotTable </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload