2

如果图像被标记为活动并且有人点击链接,他们应该被带到活动图像,否则他们应该被带到(MAX)最近上传的图像。我的第一个选择有效,但不完全有效。

这是我的选择:

    select
    b.image_id, b.active, c.id as event_id
from event.dbo.lite() a
left outer join event.dbo.dia_tracker b on a.code = b.patient_id 
    and image_id = (select max(image_id) from event.dbo.dia_tracker where patient_id = {$patient->hex})
left outer join event.dbo.ev c on a.code = c.patient_id and c.task_id in ('WR','RC') and c.status<>0
where a.code = {$patient->hex}

这是我需要一些帮助的新选择。

select 
    image_id, 
    active,
    case when active <>1 than image_id = (select max(image_id) from event.dbo.dia_tracker where patient_id = 'DSMA')
from event.dbo.dia_tracker

where active = '1' and image_id = (select max(image_id) from event.dbo.dia_tracker where patient_id = 'DSMA')

自动取款机有点乱。我在想我可以使用 isnull 以更少的代码行完成更多工作。

更新:

select 
    *,
    ISNULL(active,image_id) as  image
from event.dbo.dia_tracker

    where active = '1' and image_id = (select max(image_id) from event.dbo.dia_tracker where patient_id = 'DSMA')

仍然需要帮助。

4

2 回答 2

1

如果 active = 1,则子查询正在分组并将结果带到活动图像,否则为最新图像。

select *, t1.image_id as  image
from event.dbo.dia_tracker t1
WHERE EXISTS (select 1
              from event.dbo.dia_tracker t2                      
              where t2.patient_id = 'DSMA'
              group by case when t2.active = '1' then t2.image_id end
              having MAX(t2.image_id ) = t1.image_id)
于 2013-02-08T22:44:34.927 回答
1

如果您发布所有相关的 DDL 和示例数据会容易得多,但您可以尝试这样的事情来获取您的image_id

SELECT COALESCE(
  (SELECT image_id FROM dia_tracker WHERE patient_id = 'DSMA' AND active=1), 
  (SELECT MAX(image_id) FROM dia_tracker WHERE patient_id = 'DSMA')
) AS image_id

如果它 ( image_id) 唯一标识您要查找的行,您可以尝试

SELECT * FROM dia_tracker
WHERE image_id = (SELECT COALESCE(
  (SELECT image_id FROM dia_tracker WHERE patient_id = 'DSMA' AND active=1), 
  (SELECT MAX(image_id) FROM dia_tracker WHERE patient_id = 'DSMA')
) AS image_id)
于 2013-02-08T23:24:14.157 回答