我目前正致力于构建一个监控服务器上数据处理的软件。我一直在尝试构建一个查询,该查询可以确定给定系统的“当前处理日期”是什么。我试图通过首先确定最老的失败是什么来实现这一点,然后在那之前抓住日期。
我遇到的问题是我的子查询(引用已加入的其他表之一的别名)似乎在WHERE子句中传递了错误的id。
这是我写的查询:
select
t.team_name as team,
c.client_name as client,
c.client_id as cid,
p.pvl_name as pvl,
p.pvl_id as pid,
f.feeder as feeder,
coalesce((
select to_char( to_timestamp(status_date)::timestamp, 'mm/dd/yyyy' )
from database1.status
where
status_date < (
select status_date
from database1.status
where
status_date not in (
select status_date
from database1.status
where
process_step_id = 5
and process_state_id = 5
and pvl_id = p.pvl_id
and status_date is not null
)
and pvl_id = p.pvl_id
order by status_date
limit 1
)
and pvl_id = p.pvl_id
limit 1
), '--------------' ) as through_date
from
database1.client c
join
database1.pvl p on c.client_id = p.client_id
left join (
select child_pvl_id, count(child_pvl_id) as feeder
from database1.relation
group by child_pvl_id
) f on f.child_pvl_id = p.pvl_id
join
database1.team t on c.team_id = t.team_id
where c.team_id = 1
order by c.client_name, f.feeder desc
对于第一个系统,即使正确的日期应为2013年3月21日,它始终会返回'via_date'作为03/16/2013。我试图拉出子查询并用实际的pvl_id替换'p.pvl_id',并且它正常工作,当它在使用'p.pvl_id'的子查询中时似乎只是错误地运行
答案 0 :(得分:3)
我找到了一个问题来源。在此部分中,您设置了限制但不设置顺序。所以它可以返回任何一行:
and pvl_id = p.pvl_id
limit 1
), '--------------' ) as through_date
我还简化了一下查询:
select
t.team_name as team,
c.client_name as client,
c.client_id as cid,
p.pvl_name as pvl,
p.pvl_id as pid,
f.feeder as feeder,
coalesce((
select to_char(to_timestamp(max(status_date))::timestamp, 'MM/DD/YYYY')
from database1.status
where
status_date < (
select min(status_date)
from database1.status
where
not (
process_step_id = 5
and process_state_id = 5
and pvl_id = p.pvl_id
and status_date is not null
)
and pvl_id = p.pvl_id
)
and pvl_id = p.pvl_id
), '--------------' ) as through_date
from
database1.client c
join
database1.pvl p on c.client_id = p.client_id
left join (
select child_pvl_id, count(child_pvl_id) as feeder
from database1.relation
group by child_pvl_id
) f on f.child_pvl_id = p.pvl_id
join
database1.team t on c.team_id = t.team_id
where c.team_id = 1
order by c.client_name, f.feeder desc