0

我正在使用 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'''

All 结果如下所示。

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
4

1 回答 1

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 方法非常聪明,但我认为这不是最简单的方法。

于 2012-12-18T20:33:47.080 回答