简体   繁体   English

多个选择语句多个条件

[英]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.我在某个时间段内将 1 个实践的每个专业的平均分数与该时间段内所有其他实践的每个代码的平均分数(但不包括我正在比较的那个)进行基准测试。 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.因此,将 1 的平均值与所有的平均值进行比较。

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.您可以使用 Union 来连接这两个查询。 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:我添加了一个[Type]列:

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],'.','')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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