sql查询基于输入的不同列

时间:2012-12-18 19:26:46

标签: sql

我正在使用MS-SQL 2008.我有一个基于其中具有“Y”或Null值的位置的不同列的表。除调查结果的位置外,该表还有其他数据。我已经设置了一个临时表@TempLocation来保存基于一个或所有的位置。我需要根据日期范围内一个或多个位置行的“Y”从表格中选择行。

TableID Northwest Northeast Southwest Southeast Batchno first_choice date_completed
1       Y                   Y         Y         1       A            2012-11-10
2                 Y                   Y         1       SA           2012-19-10       
3       Y         Y                             1       N            2012-07-10
4       Y         Y                   Y         2       A            2012-10-10
5                           Y                   2       A            2012-16-10
6       Y                             Y         2       D            2012-21-10
7                 Y                             NULL    A            2012-19-10
8       Y         Y         Y         Y         3       SA           2012-11-10
9       Y                                       3       A            2012-10-10
10                          Y         Y         3       A            2012-07-10  

我已经创建了一个动态SQL语句来成功拉出一个位置但是可以拉出所有这些吗?

select  ''' + (SELECT * FROM @TempLocation) + ''',
count(batchno),
count(case when first_choice is not null then batchno end),
count(case when t.First_choice =''SD'' then 1 end) ,
count(case when t.First_choice=''D'' then 1 end) ,
count(case when t.First_choice=''N'' then 1 end) ,
count(case when t.First_choice=''A'' then 1 end) ,
count(case when t.First_choice=''SA'' then 1 end) 
from    customer_satisfaction_survey t
where   t.date_completed>= ''' + CAST(@beg_date AS VARCHAR) + '''
and     t.date_completed < ''' + CAST(dateadd(day,1,@end_date) AS Varchar) + '''
and     t.' + (SELECT * FROM @TempLocation) + ' = ''Y'''

所有结果都是这样的。

Number  Location   Total  Total2  SA  A  N  D  SD
1       Northwest  6      6       1   3  1  1  0
2       Northeast  5      4       2   2  1  0  0
3       Southwest  4      4       1   3  0  0  0
4       Southeast  6      6       2   3  0  1  0

1 个答案:

答案 0 :(得分:2)

我必须认为您正在以错误的方式接近此问题,因为您的数据未正常化。您应该做的第一件事是使用UNPIVOT规范化数据。我假设您使用的是SQL Server,因为您的语法表明了这一点。不过,最好用数据库标记所有问题。

您可以使用以下语句取消数据:

select BatchNo, FirstChoice, DateCompleted, Location
from d
unpivot (val for location in (Northwest, Northeast, Southwest, Southeast)) as unpvt

接下来,将临时表设置为每个位置都有一个单独的行。然后,您可以在没有动态SQL的情况下进行连接。类似的东西:

with dnorm as (
    THE NORMALIZATION QUERY HERE
)
select dnorm.location, count(*) as total,
       sum(case when dnorm.first_choice is not null then 1 else 0 end) as total2,
       sum(case when dnorm.first_choice = 'SA' then 1 else 0 end) as SA,
       . . .
from dnorm join
     @TempLocation tl
     on dnorm.location = tl.location
where ALL YOUR WHERE CONDITIONS HERE

最终查询类似于:

with dnorm as (
    select BatchNo, FirstChoice, DateCompleted, Location
    from d
    unpivot (val for location in (Northwest, Northeast, Southwest, Southeast)) as unpvt
)
select dnorm.location, count(*) as total,
       sum(case when dnorm.first_choice is not null then 1 else 0 end) as total2,
       sum(case when dnorm.first_choice = 'SA' then 1 else 0 end) as SA,
       . . .
from dnorm join
     @TempLocation tl
     on dnorm.location = tl.location
where ALL YOUR WHERE CONDITIONS HERE  

动态SQL方法非常聪明,但我认为这不是最简单的方法。