Author Topic: SQL Query to conditionally select a row from a table depending on join existence  (Read 3398 times)

Jezvon

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
I have a query that joins 3 tables and I am trying to retrieve a conditional set of rows depending on whether the record exists in the third table.

If there is a match in the 3rd table then I just want the matching records from the 1st table. if there isn't a match from the 3rd table then I just want one record from the first table.

Code: [Select]
select o.Object_ID,
reqCon.Connector_ID,
req.Object_ID as Requirement_ID

from t_object o

left join t_connector reqCon on reqCon.End_Object_ID = o.Object_ID and reqCon.Stereotype = 'deriveReqt'
left join t_object req on reqCon.Start_Object_ID = req.Object_ID and req.Stereotype = 'functionalRequirement'

This produces the following type of result, but the ones highlight with the > ... < are the actual rows that are required.
Object_IDConnector_IDRequirement_ID
4093643259
4093643260
>409364329938013<
4094343264
4094343265
>409434329838014<
>4408846245<
4408846246
>4266944655<
4266944656
>4267044657<
4267044658
« Last Edit: July 20, 2018, 10:08:37 pm by Jezvon »
Drogues populaires viagrasansordonnancefr.com medecins recommandent

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
You should ask that on StackOverflow since it's a SQL question and not an EA specific.

q.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8085
  • Karma: +118/-20
    • View Profile
Replace left join with inner join.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile