2013-07-10 24 views
-2

所以查询我做了忽略组特定的值:如何在SQL

select distinct id, str_replace(convert(varchar,start_time,102),'.','-') [date], 
    min(convert(varchar(8), start_time, 108)) [start time], 
    max(convert(varchar(8), start_time, 108)) [end time], 
    max(o) [o], 
    max(a) [a], case when error_msg != '(null)' then error_msg end [error?], 
    from table group by id order by id desc 

带回了这一点:

id  date  start time end time o a error? 
------------------------------------------------------------  
7338971 2012-06-09 11:06:20 11:06:20 2 5 (null) 
7338970 2012-06-09 11:06:08 11:06:59 362 725 Yes 
7338970 2012-06-09 11:06:08 11:06:59 362 725 (null) 

其中数据是由ID分组。但是,id#7338970有两个条目,因为存在null和实际的错误。是否有任何方法可以忽略空值,并且只显示7338970的一行,对错误列显示yes? 因此,这将是:

id  date  start time end time o a error? 
------------------------------------------------------------  
7338971 2012-06-09 11:06:20 11:06:20 2 5 (null) 
7338970 2012-06-09 11:06:08 11:06:59 362 725 Yes 

在此先感谢

+0

请张贴的原始SQL查询。 –

+0

你使用的是什么rdbms?如果您想要查看某个ID只有一行,那么它的错误是否为空? –

+0

我正在使用sybase 5.5和是的,我想看看它是否为空 –

回答

0

基础上评论:“如果错误是零,那么它应该显示在错误列空”:

select distinct id, str_replace(convert(varchar,start_time,102),'.','-') [date], 
min(convert(varchar(8), start_time, 108)) [start time], 
max(convert(varchar(8), start_time, 108)) [end time], 
max(o) [o], 
max(a) [a], case when error_msg != '(null)' then isnull(error_msg,'null') end [error?], 
from table 
group by id order by id desc 
+0

我想看看如果错误为空如果但是文件的行程没有错误。 –

+0

我不明白你在说什么。如果“没有错误时出现错误”是什么意思? –

+0

如果错误为空,那么它应该在错误列中显示null,而不是完全排除它 –

1

尝试增加:

and error is not null

到您的查询。

+0

我想看看如果错误为空,但如果没有错误的编号 –