-2

所以我提出的查询:

    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

提前致谢

4

2 回答 2

1

尝试添加:

and error is not null

到您的查询。

于 2013-07-10T15:26:10.893 回答
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
于 2013-07-10T15:35:39.230 回答