Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with query using pivot

Author  Topic 

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-06-17 : 11:47:58
I'm using the following query to generate a matrix which maps the amount of work our employees do for certain people.


SELECT
abbreviatedname AS FE,
[NKH] AS NKH,
[AJF] AS AJF,
[CPM] AS CPM,
[HGH] AS HGH,
[REJ] AS REJ,
[PNH] AS PNH,
[ACJ] AS ACJ,
[BRC] AS BRC,
[CJM] AS CJM,
[HRG] AS HRG,
[CST] AS CST,
[SEK] AS SEK,
[GJS] AS GJS,
[JAW] AS JAW,
[RMB] AS RMB,
[BPH] AS BPH,
[JMB] AS JMB,
[CWM] AS CWM,
[DMC] AS DMC,
[DJW] AS DJW,
[GMP] AS GMP,
[MDN] AS MDN,
[JJC] AS JJC,
[SKT] AS SKT,
[ECO] AS ECO
FROM
(
SELECT e.abbreviatedname, n.namecode, w.totalunits
FROM WORKHISTORY AS w
INNER JOIN EMPLOYEE AS e ON w.EMPLOYEENO=e.EMPLOYEENO
INNER JOIN CASES AS c ON c.CASEID=w.CASEID
LEFT OUTER JOIN CASENAME AS cn ON cn.CASEID = c.CASEID AND cn.NAMETYPE IN ('SIG', 'SA')
LEFT OUTER JOIN NAME AS n ON n.NAMENO=cn.NAMENO
LEFT OUTER JOIN CASENAME AS fe ON fe.CASEID = c.CASEID AND fe.NAMETYPE = 'EMP'
LEFT OUTER JOIN NAME AS n3 ON n3.NAMENO=fe.NAMENO
WHERE w.TRANSTYPE=400 AND w.TRANSDATE>=DATEADD(DAY,-180 ,GETDATE())
AND w.WIPCODE NOT IN ('ZPA', 'ZPD', 'ZERO')) i
PIVOT
(
SUM(totalunits)
FOR namecode IN
([NKH], [AJF], [CPM], [HGH], [REJ], [PNH], [ACJ], [BRC], [CJM], [HRG], [CST], [SEK], [GJS],
[JAW], [RMB], [BPH], [JMB], [CWM], [DMC], [DJW], [GMP], [MDN], [JJC], [SKT], [ECO])
) AS pvt
ORDER BY pvt.abbreviatedname


This is working ok but I have to do one more thing and I'm not sure how to go about it. The X axis will display all of our partners followed by the senior associates in order of seniority, and the Y axis every employee incl partners etc. What I need to do is exclude the units being included when the employee (Y axis) matches the senior associate (BPH onwards on the X axis).

Any ideas on this?

If this doesn't make any sense at all I can try and think of a better way of explaining it.

Devart
Posting Yak Master

102 Posts

Posted - 2010-06-18 : 07:05:21
Not sure that I understood everything correctly, but try adding

WHERE abbreviatedname NOT IN ('NKH', 'AJF', 'CPM', 'HGH', 'REJ', 'PNH', 'ACJ', 'BRC', 'CJM', 'HRG', 'CST', 'SEK', 'GJS','JAW', 'RMB', 'BPH', 'JMB', 'CWM', 'DMC', 'DJW', 'GMP', 'MDN', 'JJC', 'SKT', 'ECO')

before

ORDER BY pvt.abbreviatedname

Devart,
Database tools for SQL Server
http://www.devart.com/dbforge/sql
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-06-18 : 12:13:41
can you show sample output of how you want data to displayed?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -