0

I am currently working on building a software that monitors data processing on a server. I have been trying to build a query that can determine what the 'current processed through date' is for a given system. I am trying to accomplish this by first determining what the oldest failure is, and then grabbing the date immediately before that one.

The problem that I am having is that my subquery (referencing the alias for one of the other tables that was joined) seems to be passing the wrong id in the WHERE clause.

Here's the Query I have written:

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

For the first system, it always returns the 'through_date' as 03/16/2013, even though the correct date should be 03/21/2013. I have tried pulling out the subquery and replacing 'p.pvl_id' with an actual pvl_id, and it works correctly, it only seems to function erroneously when it is in a subquery using 'p.pvl_id'

4

1 回答 1

3

I found one source of problem. In this part you set a limit but do not set an order by. So it can return any row:

    and pvl_id = p.pvl_id
limit 1
), '--------------' ) as through_date

Also I simplified the query a bit:

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
于 2013-03-27T16:26:47.873 回答