2011-03-14 299 views
0

第一次在网站上发布,所以我希望我不会违反任何规则,并且有人可以帮助我。

我有如下表:

machine_name variable_name variable_value 
machine1  WAV    56789 
machine1  WAV_CONT   5 
machine1  AVI    67890 
machine1  AVI_CONT   3 
machine2  WAV    786579 
machine2  WAV_CONT   20 
machine2  AVI    182641 
machine2  AVI_CONT   9 

我使用的参数($ 1)。我想创建一个查询,给出了以下的结果,当用户输入“WAV” :

machine_name variable_name  space  count 
machine1  WAV    56789  5 
machine2  WAV    786579  20 

或者当用户输入“AVI”这等结果

machine_name variable_name  space  count 
machine1  AVI    67890  3 
machine2  AVI    182641  9 

是否有可能两个字段只使用一个参数?我的意思是,有没有办法将$ 1连接到字符串“_CONT”,以便用户只需输入“WAV”或“AVI”并获得以上任何结果?

感谢

+2

你是不是应该考虑改变你的表呢? – Snowbear 2011-03-14 18:28:35

+0

您正在使用哪些DBMS? – 2011-03-14 19:13:41

回答

0

我会添加表中标记为“类型”的新领域。然后您可以使用“WAV”或“AVI”进行过滤。

如果你只需要一个查询:

declare @param varchar(10) 
set @param = 'WAV' 

Select * from YOUR_TABLE where variable_name = @param 
0
select * from my_table where variable name in (@param, @param + '_CONT') 
0

可以使用,穷人的支点:

select 
    machine_name, 
    @param as variable_name, 
    -- Turn rows into columns: 
    -- These expressions are zero for the rows we don't want. 
    sum(case when variable_name like '%_CONT' then 0 else variable_name end) as space, 
    sum(case when not variable_name like '%_CONT' then 0 else variable_name end) as count 
from TABLE_NAME 
group by machine_name