有条件的,在哪里;SQLServer2008的Sql查询/TSql

有条件的,在哪里;SQLServer2008的Sql查询/TSql,sql,sql-server-2008,tsql,ssrs-2008,Sql,Sql Server 2008,Tsql,Ssrs 2008,我真的很想自己想出解决这个问题的办法,但事实证明,这比我想象的要困难一些 我试图检索信息的表以更简单的形式如下所示 表:车辆特征 +---+---+---+---+-----+ |Car|Nav|Bth|Eco|Radio| +---+---+---+---+-----+ |a |y |n |n |y | +---+---+---+---+-----+ |b |n |y |n |n | +---+---+---+---+-----+ |c |n |n |y |

我真的很想自己想出解决这个问题的办法,但事实证明,这比我想象的要困难一些

我试图检索信息的表以更简单的形式如下所示

表:车辆特征

+---+---+---+---+-----+
|Car|Nav|Bth|Eco|Radio|
+---+---+---+---+-----+
|a  |y  |n  |n  |y    |
+---+---+---+---+-----+
|b  |n  |y  |n  |n    |
+---+---+---+---+-----+
|c  |n  |n  |y  |n    |
+---+---+---+---+-----+
|d  |n  |y  |y  |n    |
+---+---+---+---+-----+
|e  |y  |n  |n  |n    |
+---+---+---+---+-----+
在SSRS报告中,我需要显示具有给定参数的所有特征的所有汽车。这将接收来自报告的参数,如:导航是/否、Bth是/否、Eco是/否、无线电是/否

例如,如果导航参数输入为“是”,其他参数输入为“否”,则结果表应如下所示:

+---+----------+
|Car|Features  |
+---+----------+
|a  |Nav, Radio|
+---+----------+
|e  |Nav       |
+---+----------+
我原以为这会很简单,但当我试图完成查询时,这有点让我发疯。下面是我最初认为可以得到我需要的东西,但没有

select Car,
 case when @nav = 'y' then 'Nav ' else '' end +
 case when @bth = 'y' then 'Bth ' else '' end +
 case when @eco = 'y' then 'Eco ' else '' end +
 case when @radio = 'y' then 'Radio ' else '' end As Features
from CarFeatures
where (nav = @nav -- here I don't want the row to be picked if the input is 'n' 
or bth = @bth
or eco = @eco
or radio = @radio)
基本上,逻辑应该是这样的,如果每个参数都有一行是“是”,那么就用该行的“是”列出所有特性,即使其他特性的参数是“否”

此外,我不考虑对报告进行过滤。我希望这是存储过程本身

考虑到我有4个参数,4 in if的排列可能不是一件更好的事情,我当然希望避免多个if


谢谢。

您的模式很笨拙且未规范化,您应该有3个表

Car

Feature

CarFeature
CarFeature
表应包括两列,
CarId
FeatureId
。然后你可以这样做

SELECT DISTINCT
            cr.CarId
    FROM
            CarFeature cr
    WHERE
            cr.FeatureId IN SelectedFeatures;
咆哮 {

不仅可以在不改变模式的情况下轻松添加功能, 由于支持基于集合的操作,因此提供了更好的性能 由良好的索引覆盖,总体使用较少的存储空间,因为您没有 不再需要存储
No
值,您将很好地遵守一些规则 经过40多年的思考和实践,形成了成熟的模式 开发工作和澄清

}


如果出于任何原因,您无法更改数据或架构,您可以
UNPIVOT
这样的列

或者,你可以像这样用老式的方式

从本质上讲,反规范化为子查询。两个查询的结果都是这样的

CAR    FEATURE 
A      Nav 
A      Radio 
B      Bth 
B      Radio 
C      Eco 
D      Bth 
D      Eco 
E      Nav 

试试这个它的工作

declare @table table (Car char(1),Nav char(1),Bth char(1),Eco char(1),Radio char(1))
insert into @table 
select 'a',  'y' , 'n' , 'n',  'y'
union all
select 'b',  'n' , 'y' , 'n',  'n'
union all
select 'c',  'n' , 'n' , 'y',  'n'
union all
select 'd',  'n' , 'y' , 'y',  'n'
union all
select 'e',  'y' , 'n' , 'n',  'n'

select * from @table


select a.car,
Nav = left((case when a.nav = 'y' then 'Nav, ' else '' end)  +
 (case when a.bth = 'y' then 'Bth, ' else '' end)+
(case when a.Eco = 'y' then 'Eco, ' else '' end)+
 (case when a.Radio = 'y' then 'Radio,' else '' end),
 (len(((case when a.nav = 'y' then 'Nav, ' else '' end)  +
 (case when a.bth = 'y' then 'Bth, ' else '' end)+
(case when a.Eco = 'y' then 'Eco, ' else '' end)+
 (case when a.Radio = 'y' then 'Radio,' else '' end)))-1))
from @table a
--啊哈!我有点了解自己(非常高兴):)。由于列的值只能是“y”或“n”,因此在参数值为“否”时忽略, --我只会要求它寻找永远不存在的价值。 --如果有人有更好的方法来做这件事或加强我(首选)将不胜感激。 --感谢所有回复的人。因为这是已经存在的表的一部分,也是一个大的存储过程的一部分,所以我不愿意接受前面对这个问题的回答

--在这里声明变量和赋值

select Car,
case when @nav = 'y' then 'Nav ' else '' end +
case when @bth = 'y' then 'Bth ' else '' end +
case when @eco = 'y' then 'Eco ' else '' end +
case when @radio = 'y' then 'Radio ' else '' end As Features
from CarFeatures
where (nav =    (case when @nav = 'y' then 'Y' else 'B' end 
                OR case when @bth = 'y' then 'Y' else 'B' end 
                OR case when @eco = 'y' then 'Y' else 'B' end 
                OR case when @radio = 'y' then 'Y' else 'B' end 
    )   

您可以在存储过程中使用
if-else
逻辑您可以声明@SQL-nvarchar(max),根据需要构建查询字符串,并将EXEC(@SQL)执行到临时表中,或者直接返回调用者。您还可以查看XML用法的详细信息。这样,你就可以将你的汽车特性连接成一列,而另一辆车则是一个有问题的数据。@ MelePavelv——如果ELSE考虑所有四个参数的情况,这不会太多。@彼埃尔-这可能解决这个问题,但这是一个现有的存储过程,其中包含许多其他部分,我不确定Sql存储过程中的查询字符串。我认为查询字符串是web dev.@jordell的一部分-你可能是对的,我同意你的解释。然而,这是一个现有的表,在这一点上对这种情况进行规范化至少对我来说是超出范围的。如果我认为有太多的情况需要考虑的话,我可能不得不考虑。
declare @table table (Car char(1),Nav char(1),Bth char(1),Eco char(1),Radio char(1))
insert into @table 
select 'a',  'y' , 'n' , 'n',  'y'
union all
select 'b',  'n' , 'y' , 'n',  'n'
union all
select 'c',  'n' , 'n' , 'y',  'n'
union all
select 'd',  'n' , 'y' , 'y',  'n'
union all
select 'e',  'y' , 'n' , 'n',  'n'

select * from @table


select a.car,
Nav = left((case when a.nav = 'y' then 'Nav, ' else '' end)  +
 (case when a.bth = 'y' then 'Bth, ' else '' end)+
(case when a.Eco = 'y' then 'Eco, ' else '' end)+
 (case when a.Radio = 'y' then 'Radio,' else '' end),
 (len(((case when a.nav = 'y' then 'Nav, ' else '' end)  +
 (case when a.bth = 'y' then 'Bth, ' else '' end)+
(case when a.Eco = 'y' then 'Eco, ' else '' end)+
 (case when a.Radio = 'y' then 'Radio,' else '' end)))-1))
from @table a
Try This, I believe this will solve your purpose.. 

SELECT   Car,  
     tblPivot.Property AS Features, 
     tblPivot.Value
     INTO #tmpFeature
 FROM     
 (SELECT CONVERT(sql_variant,Car) AS Car,CONVERT(sql_variant,NAV) AS NAV,         CONVERT(sql_variant,BTH) AS BTH,     CONVERT(sql_variant,ECO) AS ECO,  
CONVERT(sql_variant,Radio) AS Radio   FROM CarFeatures) CarFeatures  
 UNPIVOT (Value For Property In (NAV,BTH, ECO, Radio)) as tblPivot
 Where tblPivot.Value='y'


 SELECT 
  Car,
  STUFF((
    SELECT ', ' + Features 
    FROM #tmpFeature 
    WHERE (Car = Results.Car) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS Features
FROM #tmpFeature Results
GROUP BY Car
select Car,
case when @nav = 'y' then 'Nav ' else '' end +
case when @bth = 'y' then 'Bth ' else '' end +
case when @eco = 'y' then 'Eco ' else '' end +
case when @radio = 'y' then 'Radio ' else '' end As Features
from CarFeatures
where (nav =    (case when @nav = 'y' then 'Y' else 'B' end 
                OR case when @bth = 'y' then 'Y' else 'B' end 
                OR case when @eco = 'y' then 'Y' else 'B' end 
                OR case when @radio = 'y' then 'Y' else 'B' end 
    )