SQL Server有办法绕过重复的条件行吗

SQL Server有办法绕过重复的条件行吗,sql,sql-server-2008,Sql,Sql Server 2008,您好,我正在尝试清理一个查询,其中有一行重复了六次。 有没有办法在SQL中设置常量之类的内容 以下是问题的一个例子: select Distinct DSF.CityName, ( Select count (Distinct DSF1.IncdtKey) from dbo.IncidentFile DSF1 Where DSF1.IncidentMostSevere in ('1', '2', '4', '5', '6') and DSF1.CategoryKey in ('1

您好,我正在尝试清理一个查询,其中有一行重复了六次。 有没有办法在SQL中设置常量之类的内容

以下是问题的一个例子:

select Distinct DSF.CityName, 
( Select count (Distinct DSF1.IncdtKey)
  from dbo.IncidentFile DSF1 
  Where DSF1.IncidentMostSevere in ('1', '2', '4', '5', '6')
  and DSF1.CategoryKey in ('15', '01', '02', '03', '04', '05', '06')<-----
  and DSF1.CityName = DSF.CityName)  as 'Weapons Possession 11-12',
( Select count (Distinct DSF2.IncdtKey)
  from dbo.IncidentFile DSF2 
  Where DSF2.IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
  and DSF2.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
  and DSF2.CityName = DSF.CityName)  as 'Drugs Related 11-12',
( Select count (Distinct DSF3.IncdtKey)
  from dbo.IncidentFile DSF3 
  Where DSF3.IncidentMostSevere in ('14', '15', '17', '20', '21', '22', '26') 
  and DSF3.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
  and DSF3.CityName = DSF.CityName)  as 'Incident with Injury 11-12',
( Select count (Distinct DSF4.IncdtKey)
  from dbo.IncidentFile DSF4 
  Where DSF4.IncidentMostSevere in ('16', '18', '19', '23', '24', '25')
  and DSF4.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
  and DSF4.CityName = DSF.CityName)  as 'Incident no Injury 11-12',
( Select count (Distinct DSF5.IncdtKey)
  from dbo.IncidentFile DSF5 
  Where DSF5.IncidentMostSevere in ('3', '13', '29', '31', '32', '33')
 and DSF5.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
 and DSF5.CityName = DSF.CityName)  as 'Other reason for 11-12',
( Select count (Distinct DSF6.IncdtKey)
  from dbo.IncidentFile DSF6 
  Where DSF6.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
  and DSF6.CityName = DSF.CityName)  as 'Total Incidents'
  from dbo.IncidentFile DSF
group by DSF.CityName
Order by DSF.CityName

谢谢

您应该能够使用CTE,然后使用带有大小写表达式的聚合函数:

;with cte as
(
  select distinct CityName,
    IncidentMostSevere,
     IncdtKey
  from dbo.IncidentFile
  where CategoryKey in ('15', '01', '02', '03', '04', '05', '06')
)
select CityName,
  count(case 
        when IncidentMostSevere in ('1', '2', '4', '5', '6')
        then IncdtKey end) as 'Weapons Possession 11-12',
  count(case 
        when IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
        then IncdtKey end) as 'Drugs Related 11-12',
  count(case 
        when IncidentMostSevere in ('14', '15', '17', '20', '21', '22', '26')
        then IncdtKey end) as 'Incident with Injury 11-12',
  count(case 
        when IncidentMostSevere in ('16', '18', '19', '23', '24', '25')
        then IncdtKey end) as 'Incident no Injury 11-12',
  count(case 
        when IncidentMostSevere in ('3', '13', '29', '31', '32', '33')
        then IncdtKey end) as 'Other reason for 11-12',
  count(case 
        when IncidentMostSevere in ('15', '01', '02', '03', '04', '05', '06')
        then IncdtKey end) as 'Total Incidents'
from cte
group by CityName
order by CityName

这种方法的工作原理是将子查询移动到from子句中,并在where子句中添加一次类别列表

select 
  DSF.CityName, 
  Dsf1.cnt as 'Weapons Possession 11-12',
  Dsf2.cnt as 'Drugs Related 11-12',
  ...
  Dfsn.cnt as 'Total Incidents'

From
  dbo.IncidentFile DSF

  Inner join
  (
     Select 
       DSF.CategoryKey,
       DSF.CityName,
       count (Distinct DSF.IncdtKey) as cnt
     from 
       dbo.IncidentFile DSF
     Where
       DSF.IncidentMostSevere in ('1', '2', '4', '5', '6').  
     group by
       1, 2
   ) DSF1
   On  DSF1.CityName = DSF.CityName

   Inner join
   ( 
     Select 
       DSF.CategoryKey,
       DSF.CityName,
       count (Distinct DSF.IncdtKey) as cnt
     from 
       dbo.IncidentFile DSF
     Where
       DSF.IncidentMostSevere in ('7', '8', '9', '10', '11', '12') 
     group by
       1, 2
   ) DSF2
   On DSF2.CityName = DSF.CityName 

   ...

   Inner join
   ( 
     Select 
       DSF.CategoryKey,
       DSF.CityName,
       count (Distinct DSF.IncdtKey) as cnt
     from 
       dbo.IncidentFile DSF
     Where
       DSF.IncidentMostSevere in ('7', '8', '9', '10', '11', '12') 
     group by
       1, 2
   ) DSFn
   On dfsn.CityName = DSF.CityName   

 Where 
   dfsn.CategoryKey in ('15','01','02,'03','04','05','06')


 Order by DSF.CityName

加入具有这些值的表格?谢谢,效果很好。我曾经考虑过使用case,但可以解决它。@user2250917不客气,我很高兴它对您有效,欢迎使用StackOverflow。谢谢另一个好主意。