nkarmi

Untitled

Apr 26th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.54 KB | None | 0 0
  1. set statistics time on
  2.  
  3.                 DECLARE @month nvarchar(2) = '1', @year nvarchar(4) = '2018', @siteId int = 73, @responseFormat int = 1
  4.                 , @categoryId int = 13, @assetId int  = 1983
  5.                 DECLARE @date DATETIME = CONVERT(DATETIME, '01/'+@month+'/'+@year, 103)                
  6.                
  7.                 SELECT sp.asset_id, assets.asset_name, categories.category_id, categories.category_name, categories.class_name
  8.                 , sections.section_id, sections.section_name, mainDt.sub_section_id, sub_sections.sub_section_name, sub_sections.sub_section_name_he
  9.                 , mainDt.data_point_id, mainDt.data_point_text , mainDt.data_point_text_he ,mainDt.response_type, mainDt.response_format
  10.                 , procedure_typesV2.procedure_type_id AS answer_id , procedure_typesV2.procedure_type_name AS answer_name
  11.                 , procedure_typesV2.response_text_hebrew AS answer_name_heb, CAST(CHARINDEX(LTRIM(STR(procedure_typesV2.procedure_type_id)), data_entriesV2.internal_raw_value, 1) as BIT) as selected_answer
  12.                 , unit_groups.unit_id ,units.unit_name, CAST(CASE WHEN data_entriesV2.unit_id = units.unit_id THEN 1 ELSE 0 END AS BIT) as selected_unit
  13.                 , data_entriesV2.unit_id as selected_id, data_entriesV2.calculation_method_id, data_entriesV2.calc_comment
  14.                 , data_entriesV2.internal_raw_value as value, @date as date_reported, mainDt.dependent_data_point_id, mainDt.dependent_data_point_value
  15.                 , units.unit_name, ISNULL(unit_groups.is_default, 0) as selected_default_unit
  16.                 , (SELECT unit_groups.unit_id
  17.                     FROM unit_groups
  18.                     WHERE unit_groups.group_id = mainDt.UoM_group AND unit_groups.is_default = 1 ) AS default_unit_id
  19.                 , (SELECT de.unit_id FROM data_entriesV2 AS de
  20.                     WHERE de.site_id = @siteId
  21.                         AND de.data_point_id = mainDt.data_point_id
  22.                         AND de.asset_id = @assetId
  23.                         AND date_reported = DATEADD(yy, DATEDIFF(yy, 0, @date), 0)
  24.                     ) AS first_month_unit_id
  25.                 , CAST(CASE WHEN files.data_point_id = mainDt.data_point_id THEN 1 ELSE 0 END AS BIT) as has_file_updated
  26.                 , CAST(CASE WHEN comments.data_point_id = mainDt.data_point_id THEN 1 ELSE 0 END AS BIT) as has_comments                
  27.                 ,(SELECT TOP 1 preselected_de.calculation_method_id FROM data_entriesV2 AS preselected_de
  28.                  WHERE preselected_de.site_id = @siteId
  29.                         AND preselected_de.data_point_id = mainDt.data_point_id
  30.                         AND preselected_de.asset_id = @assetId                     
  31.                         AND preselected_de.calculation_method_id != 0
  32.                         AND preselected_de.date_reported <= @date
  33.                  ORDER BY preselected_de.date_reported DESC
  34.                  ) AS preselected_calc_method
  35.  
  36.                 FROM data_pointsV2 as mainDt
  37.                 JOIN selected_parameters AS sp ON mainDt.data_point_id = sp.data_point_id AND sp.asset_id = @assetId AND sp.site_id = @siteId
  38.                     AND sp.category_id = @categoryId AND sp.response_format = mainDt.response_format
  39.                 JOIN sub_sections ON sub_sections.sub_section_id = mainDt.sub_section_id
  40.                 JOIN sections ON sections.section_id = sub_sections.section_id
  41.                 JOIN categories ON categories.category_id = sections.category_id
  42.                 LEFT JOIN procedure_typesV2 ON procedure_typesV2.data_point_id = mainDt.data_point_id
  43.                 LEFT JOIN data_entriesV2 ON mainDt.data_point_id = data_entriesV2.data_point_id AND data_entriesV2.date_reported = @date
  44.                     AND data_entriesV2.site_id = sp.site_id  AND data_entriesV2.asset_id = sp.asset_id                
  45.                 LEFT JOIN unit_groups ON unit_groups.group_id = mainDt.UoM_group
  46.                 LEFT JOIN units ON units.unit_id = unit_groups.unit_id
  47.                 LEFT JOIN assets ON assets.asset_id = sp.asset_id
  48.                 LEFT JOIN files on files.data_point_id = mainDt.data_point_id AND files.asset_id = @assetId AND files.site_id = @siteId AND files.date_reported = @date
  49.                 LEFT JOIN comments on comments.data_point_id = mainDt.data_point_id AND comments.asset_id = @assetId AND comments.site_id = @siteId AND comments.date_reported = @date
  50.                 WHERE mainDt.response_format = @responseFormat AND
  51.                 sections.category_id = @categoryId             
  52.                 ORDER BY sections.section_id, mainDt.sub_section_id ,mainDt.order_of_appearance ,mainDt.data_point_id, procedure_typesV2.procedure_type_id DESC            
  53.  
  54. set statistics time off
Add Comment
Please, Sign In to add comment