简体   繁体   中英

Multiple select statements multiple conditions

Wondering if someone could help with a query I have on joining statements.

I am benchmarking 1 practice's average score per speciality during a certain time period against all the other practices' average score per code (but not including the one I am comparing) during that time period. I was wondering if there is a way to join my statements. My current code does it separately. I cannot use union as it returns a different number of rows as well as not looking at the same things. Inner join also does not seem to work. It is all pulling from one database/table.

So basically I am looking to join two complex queries each in their own column that compare results out of different filters of the data. So to compare the average of 1 verses the averages of all.

This is taken from the table: 在此处输入图像描述

This is my code:

SELECT [specialty] as [Specialty]
      ,Replace([procedure_code],'.','') AS [Code]
      ,[procedure_description] as [Product Description]
      ,sum([cases]) as [Total Cases]
      ,AVG([Touchtime]) as [AVG Touchtime (mins)]
  FROM TableOutput
  where [trust_code] = 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by [specialty]
      ,Replace([procedure_code],'.','')
      ,[procedure_description]
    

SELECT Replace([procedure_code],'.','') AS [Code]
      , AVG([Touchtime]) as [AVG Touchtime (mins)]
  FROM TableOutput
  where [region_code] = 'Y55'
  AND [trust_code] <> 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by Replace([procedure_code],'.','')

You can use Union to join the two queries. I see both of them doesn't have same number of columns, but as in this case the first query has only five columns and the second one has two matching with first, you can add the rest of columns as null.

SELECT [specialty] as [Specialty]
      ,Replace([procedure_code],'.','') AS [Code]
      ,[procedure_description] as [Product Description]
      ,sum([cases]) as [Total Cases]
      ,AVG([Touchtime]) as [AVG Touchtime (mins)]
  FROM TableOutput
  where [trust_code] = 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by [specialty]
      ,Replace([procedure_code],'.','')
      ,[procedure_description]
    
UNION ALL 

SELECT 
null as [Specialty]
,Replace([procedure_code],'.','') AS [Code]
, null as [Product Description]
, null as [Total Cases]
, AVG([Touchtime]) as [AVG Touchtime (mins)]
  FROM TableOutput
  where [region_code] = 'Y55'
  AND [trust_code] <> 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by Replace([procedure_code],'.','')

This should work and in case you require to differentiate the data from which query the data is from you can add another column for that to further filter it out. I've added a [Type] column:

SELECT [specialty] as [Specialty]
      ,Replace([procedure_code],'.','') AS [Code]
      ,[procedure_description] as [Product Description]
      ,sum([cases]) as [Total Cases]
      ,AVG([Touchtime]) as [AVG Touchtime (mins)]
      , 'FromRBA' as [Type]
  FROM TableOutput
  where [trust_code] = 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by [specialty]
      ,Replace([procedure_code],'.','')
      ,[procedure_description]

UNION ALL 

SELECT 
null as [Specialty]
,Replace([procedure_code],'.','') AS [Code]
, null as [Product Description]
, null as [Total Cases]
, AVG([Touchtime]) as [AVG Touchtime (mins)]
, 'NotFromRBA' as [Type]
  FROM TableOutput
  where [region_code] = 'Y55'
  AND [trust_code] <> 'RBA'
  AND [flag _emergency_case] = 'N' 
  AND [flag _cancellation] = 'N'
  AND CONVERT(datetime,date_of_visit,103) BETWEEN '2019/04/06 00:00:00.000' AND '2020/04/05 00:00:00.000'
  Group by Replace([procedure_code],'.','')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM