Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.51 KB | None | 0 0
  1.  
  2. ----here's the query I use to figure out what the max number of elements in my future pivots are going to be.
  3. --I run this twice. Once for all components of a goal's measurement constructor, once for its INPUTS ONLY [Dropdowns, textboxes]
  4. --;WITH countGoalItems AS (
  5. --SELECT dm2.MeasurementID, COUNT(DISTINCT dmi.MeasurementItemID) AS ItemCount
  6. --FROM dbo.dtMeasurements AS dm2 -- dtMeasurements [not to be confused with dMeasurements] is where the template of a measure [or goal] lives
  7. --JOIN dbo.dtMeasurementItem AS dmi -- dtMeasurementItems [not to be confused with dMeasurementItems] is where the components of a measure live.
  8. --     ON dmi.MeasurementID = dm2.MeasurementID --joins on the [template] MeasurementID
  9. --            AND dmi.GoalRank > 0 --if this is greater than 0, it's a goal item.
  10. --            --AND dmi.Type <> 'Label' -- this is how you run for inputs only!!! uncomment this
  11. --LEFT JOIN dbo.dtFieldPopulatorItems AS dfpi
  12. --     ON  CONVERT(VARCHAR(36), dmi.Stmt) = CONVERT(VARCHAR(36), dfpi.FieldPopulatorID)
  13.  
  14. --WHERE dm2.Active = 1  --if you only care about active measures, uncomment this. If you want a full history, don't.
  15.              
  16. --GROUP BY dm2.MeasurementID
  17. --)
  18.  
  19. ----This is how I arrived at the highest number of items in an active goal currently being 14, including labels, 8 without labels.
  20. --SELECT MAX(ItemCount) FROM countGoalItems
  21.  
  22. --now the real query.
  23.  
  24. IF OBJECT_ID('tempdb..#NormalizedGoalConstructors') IS NOT NULL DROP TABLE #NormalizedGoalConstructors
  25.  
  26. SELECT
  27.        dm3.ModuleID  
  28.        , dm3.Modulename
  29.        , dm2.MeasurementID
  30.        , dm2.MeasurementName
  31.        , CASE dmi.Type WHEN 'Label' THEN dmi.Stmt ELSE '#' END AS OutputStatement
  32.        -- the field name of a component in the goal constructor, or "Text" if it's a box, or empty-string if it's a label.
  33.        , FieldPopName = ISNULL(dfp.Field, IIF(dmi.Type = 'Text Box', 'Text', ''))
  34.        -- here's where some magic occurs.
  35.        , ROW_NUMBER() OVER(PARTITION BY dm3.ModuleID, dm2.MeasurementID ORDER BY dmi.GoalRank) AS ComponentIndex
  36. INTO #NormalizedGoalConstructors
  37. FROM dbo.dtMeasurements AS dm2
  38. -- I slip this between the measure constructor and its contents because it confers a 1:1, it's optimized here as opposed to below, ie: does less work.
  39. JOIN dbo.dtModules AS dm3 ON dm3.ModuleID = dm2.ModuleID --this is solely to get the name of the module.
  40. JOIN dbo.dtMeasurementItem AS dmi ON dmi.MeasurementID = dm2.MeasurementID --joins constructor to its components
  41.        AND dmi.GoalRank > 0 --if this is greater than 0, it's a goal item.
  42. LEFT JOIN dbo.dtFieldPopulator AS dfp ON CONVERT(VARCHAR(36), dfp.FieldPopulatorID) = CONVERT(VARCHAR(36), dmi.Stmt) -- the name of the field pop lives here
  43. WHERE dm2.Active = 1 --again, constrain active
  44.  
  45. IF OBJECT_ID('tempdb..#NormalizedGoalConstructorInputsOnly') IS NOT NULL DROP TABLE #NormalizedGoalConstructorInputsOnly
  46.  
  47. --now another.. but this one excludes labels and gets inputs only
  48. SELECT
  49.        dm3.ModuleID  
  50.        , dm3.Modulename
  51.        , dm2.MeasurementID
  52.        , dm2.MeasurementName
  53.        -- the field name of a component in the goal constructor, or "Text" if it's a box, or empty-string if it's a label.
  54.        , FieldPopName = ISNULL(dfp.Field, 'Text')
  55.        -- here's where some magic occurs.
  56.        , DENSE_RANK() OVER(PARTITION BY dm3.ModuleID, dm2.MeasurementID ORDER BY dmi.GoalRank) AS ComponentIndex
  57. INTO #NormalizedGoalConstructorInputsOnly
  58. FROM dbo.dtMeasurements AS dm2
  59. -- I slip this between the measure constructor and its contents because it confers a 1:1, it's optimized here as opposed to below, ie: does less work.
  60. JOIN dbo.dtModules AS dm3 ON dm3.ModuleID = dm2.ModuleID --this is solely to get the name of the module.
  61. JOIN dbo.dtMeasurementItem AS dmi ON dmi.MeasurementID = dm2.MeasurementID --joins constructor to its components
  62.        AND dmi.GoalRank > 0 --if this is greater than 0, it's a goal item.
  63.        AND dmi.Type <> 'Label'
  64. LEFT JOIN dbo.dtFieldPopulator AS dfp ON CONVERT(VARCHAR(36), dfp.FieldPopulatorID) = CONVERT(VARCHAR(36), dmi.Stmt) -- the name of the field pop lives here
  65. WHERE dm2.Active = 1 --again, constrain active
  66.  
  67. IF OBJECT_ID('tempdb..#PivotedConstructor') IS NOT NULL DROP TABLE #PivotedConstructor
  68.  
  69. --now the fun part
  70. SELECT
  71.        Modulename
  72.        , MeasurementID
  73.        , MeasurementName
  74.        --concats are rad, they do something really wicked. This is a T-SQL thing.
  75.        , CONCAT(
  76.               [1] + ' '
  77.               , [2] + ' '
  78.               , [3] + ' '
  79.               , [4] + ' '
  80.               , [5] + ' '
  81.               , [6] + ' '
  82.               , [7] + ' '
  83.               , [8] + ' '
  84.               , [9] + ' '
  85.               , [10] + ' '
  86.               , [11] + ' '
  87.               , [12] + ' '
  88.               , [13] + ' '
  89.               , [14]
  90.               ) AS Constructor
  91. INTO #PivotedConstructor
  92. FROM (
  93.        SELECT
  94.               g.ModuleName
  95.               , g.MeasurementID
  96.               , g.MeasurementName
  97.               , g.ComponentIndex
  98.               , g.OutputStatement        
  99.        FROM #NormalizedGoalConstructors AS g
  100.        ) AS SourceTable --this name is just another alias, you can name it anything
  101. PIVOT
  102. (
  103.        MAX(OutputStatement)
  104.        FOR ComponentIndex IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14])
  105. ) AS PivotTable --again, just a name
  106.  
  107.  
  108. IF OBJECT_ID('tempdb..#PivotedConstructorInputs') IS NOT NULL DROP TABLE #PivotedConstructorInputs
  109.  
  110. --now the fun part 2
  111. SELECT
  112.        Modulename
  113.        , MeasurementID      
  114.        , MeasurementName
  115.        , [1]
  116.        , [2]
  117.        , [3]
  118.        , [4]
  119.        , [5]
  120.        , [6]
  121.        , [7]
  122.        , [8]
  123. INTO #PivotedConstructorInputs
  124. FROM (
  125.        SELECT
  126.               g.ModuleName
  127.               , g.MeasurementID
  128.               , g.MeasurementName
  129.               , g.ComponentIndex
  130.               , g.FieldPopName
  131.        FROM #NormalizedGoalConstructorInputsOnly AS g
  132.        ) AS SourceTable --this name is just another alias, you can name it anything
  133. PIVOT
  134. (
  135.        MAX(FieldPopName)
  136.        FOR ComponentIndex IN ([1], [2], [3], [4], [5], [6], [7], [8])
  137. ) AS PivotTable --again, just a name
  138.  
  139. SELECT
  140.        pci.Modulename
  141.        , pci.MeasurementName
  142.        , pc.Constructor
  143.        , pci.[1]
  144.        , pci.[2]
  145.        , pci.[3]
  146.        , pci.[4]
  147.        , pci.[5]
  148.        , pci.[6]
  149.        , pci.[7]
  150.        , pci.[8]    
  151. FROM #PivotedConstructor AS pc
  152. JOIN #PivotedConstructorInputs AS pci
  153.        ON pc.MeasurementID = pci.MeasurementID
  154. ORDER BY pci.Modulename
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement