Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to dynamically create multiple Pivot join

$
0
0

Is it possible for me to do the following in dynamic pivot?  The code below basically in the first select put the 'result' value for each month into the 'Value' field in the temp table #ValueBucket.  The select statement does the same thing, except it does it for the 'rating' field value.  The next two select statements follow the same logic except it populates the temp table with the year to date Result and Rating values. 

I populated this temp table with these fields values, so that down in my pivot I would be able to just pivot on one field(Value) versus having to join on multiple pivot.  This seems to be working fine for me now.  I was just interested in if it was possible for me to code the pivot dynamically?

Current Code: 

BEGIN
-----------------------------------   
 -- Get Result Values --    
 -----------------------------------   
   Select EmployeeID
         ,Employee
         ,EmpGroupID 
         ,PA 
         ,EmpLevel 
         ,ObjectiveName      as Objective
         ,Objectiveid        as Objectiveid
         ,Weighting
         ,ReportingYear
         ,CASE max(MONTH(r.startdate))
            WHEN 1 THEN 'January_Result'
            WHEN 2 THEN 'February_Result'
            WHEN 3 THEN 'March_Result'
            WHEN 4 THEN 'April_Result'
            WHEN 5 THEN 'May_Result'
            WHEN 6 THEN 'June_Result'
            WHEN 7 THEN 'July_Result'
            WHEN 8 THEN 'August_Result'
            WHEN 9 THEN 'September_Result'
            WHEN 10 THEN 'October_Result'
            WHEN 11 THEN 'November_Result'
            WHEN 12 THEN 'December_Result'
          END as [StartMonth] 
         ,MAX([Result]) as Value
  into #ValueBucket
   From #tblResults R
   INNER JOIN @Emphist h 
           ON r.Empgroupid = h.Groupid
   LEFT OUTER JOIN Config.tblRatingDescription rt
                ON r.Rating = rt.RatingID
   LEFT OUTER JOIN Config.tblRatingDescription rtovr
                ON r.RatingOverride = rtovr.RatingID
             WHERE r.startdate BETWEEN  '2012-01-01' AND '2012-05-01'
               AND r.datetype IN ('M','Y')
   GROUP BY 
  EmployeeID, 
Employee, 
EmpGroupID, 
PA, 
EmpLevel, 
ObjectiveName, 
Objectiveid,
ReportingYear,
Weighting               
-----------------------------------   
-- Get rating values --        
-----------------------------------          
   Insert Into #ValueBucket(EmployeeID,Employee,EmpGroupID,PA,EmpLevel,Objective,Objectiveid,Weighting,ReportingYear,StartMonth,Value)          
   Select EmployeeID
         ,Employee
         ,EmpGroupID 
         ,PA 
         ,EmpLevel 
         ,ObjectiveName      as Objective
         ,Objectiveid        as Objectiveid
         ,Weighting
         ,ReportingYear
         ,CASE max(MONTH(r.startdate))
            WHEN 1 THEN 'January_Rating'
            WHEN 2 THEN 'February_Rating'
            WHEN 3 THEN 'March_Rating'
            WHEN 4 THEN 'April_Rating'
            WHEN 5 THEN 'May_Rating'
            WHEN 6 THEN 'June_Rating'
            WHEN 7 THEN 'July_Rating'
            WHEN 8 THEN 'August_Rating'
            WHEN 9 THEN 'September_Rating'
            WHEN 10 THEN 'October_Rating'
            WHEN 11 THEN 'November_Rating'
            WHEN 12 THEN 'December_Rating'
          END as [StartMonth] 
         ,MAX([Rating]) as Value
   From #tblResults R
   INNER JOIN @Emphist h 
           ON r.Empgroupid = h.Groupid
   LEFT OUTER JOIN Config.tblRatingDescription rt
                ON r.Rating = rt.RatingID
   LEFT OUTER JOIN Config.tblRatingDescription rtovr
                ON r.RatingOverride = rtovr.RatingID
             WHERE r.startdate BETWEEN  '2012-01-01' AND '2012-05-01'
               AND r.datetype IN ('M','Y')
  GROUP BY 
  EmployeeID, 
Employee, 
EmpGroupID, 
PA, 
EmpLevel, 
ObjectiveName, 
Objectiveid,
ReportingYear,
Weighting 
-----------------------------------         
-- Get Result YTD values --
-----------------------------------   
   Insert Into #ValueBucket(EmployeeID,Employee,EmpGroupID,PA,EmpLevel,Objective,Objectiveid,Weighting,ReportingYear,StartMonth,Value)          
   Select EmployeeID
         ,Employee
         ,EmpGroupID 
         ,PA 
         ,EmpLevel 
         ,ObjectiveName      as Objective
         ,Objectiveid        as Objectiveid
         ,Weighting
         ,ReportingYear
         ,max(Case When r.DateType = 'Y' and Month(r.StartDate) = 1 AND r.result IS NOT NULL Then 'Result_YTD_13' end) as [StartMonth] 
         ,max(Case When r.DateType = 'Y' and Month(r.StartDate) = 1 AND r.result IS NOT NULL Then r.Result
                   When r.DateType = 'Y' and Month(r.StartDate) = 1 AND r.result IS NULL AND r.rating is NOT NULL THEN COALESCE(rtovr.ratingShortDesc,rt.ratingShortDesc)
               Else null
              END)  as Value
   From #tblResults R
   INNER JOIN @Emphist h 
           ON r.Empgroupid = h.Groupid
   LEFT OUTER JOIN Config.tblRatingDescription rt
                ON r.Rating = rt.RatingID
   LEFT OUTER JOIN Config.tblRatingDescription rtovr
                ON r.RatingOverride = rtovr.RatingID
             WHERE r.startdate BETWEEN  '2012-01-01' AND '2012-05-01'
               AND r.datetype IN ('M','Y')
  GROUP BY 
  EmployeeID, 
Employee, 
EmpGroupID, 
PA, 
EmpLevel, 
ObjectiveName, 
Objectiveid,
ReportingYear,
Weighting 
-----------------------------------   
-- Get Rating YTD values --
-----------------------------------   
   Insert Into #ValueBucket(EmployeeID,Employee,EmpGroupID,PA,EmpLevel,Objective,Objectiveid,Weighting,ReportingYear,StartMonth,Value)          
   Select EmployeeID
         ,Employee
         ,EmpGroupID 
         ,PA 
         ,EmpLevel 
         ,ObjectiveName      as Objective
         ,Objectiveid        as Objectiveid
         ,Weighting
         ,ReportingYear
         ,max(Case When r.DateType = 'Y' and Month(r.StartDate) = 1 Then 'Rating_YTD_13' end) as [StartMonth] 
         ,max(Case When r.DateType = 'Y' and Month(r.StartDate) = 1
         Then dbo.udfGetRating(case when r.RatingOverride is null 
                                    then r.Rating else r.RatingOverride 
                               end
                              ) + '|' + CAST(UserOverride AS CHAR(1)) + '|' + rt.ratingShortDesc
         Else null
      END
    )  as Value
   From #tblResults R
   INNER JOIN @Emphist h 
           ON r.Empgroupid = h.Groupid
   LEFT OUTER JOIN Config.tblRatingDescription rt
                ON r.Rating = rt.RatingID
   LEFT OUTER JOIN Config.tblRatingDescription rtovr
                ON r.RatingOverride = rtovr.RatingID
             WHERE r.startdate BETWEEN  '2012-01-01' AND '2012-05-01'
               AND r.datetype IN ('M','Y')
  GROUP BY 
  EmployeeID, 
Employee, 
EmpGroupID, 
PA, 
EmpLevel, 
ObjectiveName, 
Objectiveid,
ReportingYear,
Weighting 
---------------------------------
-- Pivot --
-----------------------------------   
Select * 
From 
(
   Select v.EmployeeID
         ,v.Employee
         ,v.EmpGroupID 
         ,v.PA 
         ,v.EmpLevel 
         ,v.Objective         as Objective
         ,v.Objectiveid        as Objectiveid
         ,v.Weighting
         ,v.ReportingYear
         ,v.StartMonth
         ,v.Value
   From  #ValueBucket v
) TableDate
PIVOT (
          MAX(Value) 
          FOR [StartMonth] in(January_Result,January_Rating,February_Result,February_Rating,March_Result,March_Rating,April_Result,April_Rating,
                              May_Result,May_Rating,June_Result,June_Rating,July_Result,July_Rating,August_Result,August_Rating,September_Result,September_Rating,
                              October_Result,October_Rating,November_Result,November_Rating,December_Result, December_Rating,Result_YTD_13,Rating_YTD_13
                              )         
      )PivotTable
END


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>