0

I have these query:

select b.type,b.name,a.name_b,a.start_time
         from runinfo a left JOIN definition b
                        on a.sched_table=b.parent_table
                        and a.name_b=b.name
                        where sched_table not like 'PLAN' 
         and to_char(start_time,'YYYYMMDD') = to_char(current_date-1,'YYYYMMDD')
         order by start_time desc;

Tables are:

DEFINITION
----------

TYPE: COMMAND
NAME: DAVE
.........

RUNINFO
-------

START_TIME: 2019/08/15 23:59
NAME_B: DAVE
.........
START_TIME: 2019/08/15 23:58
NAME_B: DAVE
........

I get duplicate rows and I can't understand:

TYPE     NAME   START_TIME           NAME_B
COMMAND  DAVE   2019/08/15 23:59     DAVE
COMMAND  DAVE   2019/08/15 23:59     DAVE
COMMAND  DAVE   2019/08/15 23:58     DAVE
COMMAND  DAVE   2019/08/15 23:58     DAVE

Why I get two rows of every record?

thanks and sorry for my English!

3
  • 1
    Joining a table with one record to a table with two records gives a result set with at most two records, period (even for a cross join). I speculate that your starting tables already have duplicate data. Commented Aug 16, 2019 at 10:16
  • But I would like to extract the results of table runfinfo, add the column type of the other table. Is possible to make that? Thanks! Commented Aug 16, 2019 at 10:20
  • 1
    You can use select distinct to handle the symptom, but your underlying tables would seem to have duplicates. Commented Aug 16, 2019 at 12:15

1 Answer 1

1

I guess you need an INNER JOIN rather than LEFT JOIN -

SELECT DISTINCT b.type,b.name,a.name_b,a.start_time
FROM runinfo a
INNER JOIN definition b ON a.sched_table=b.parent_table
                       AND a.name_b=b.name
WHERE sched_table NOT LIKE '%PLAN%' 
AND TO_CHAR(start_time,'YYYYMMDD') = TO_CHAR(current_date-1,'YYYYMMDD')
ORDER BY start_time DESC;

Also I have updated your Like predicate.

Sign up to request clarification or add additional context in comments.

1 Comment

There must be some duplicates in your underlying tables. I have added DISTINCT clause. This might solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.