views:

370

answers:

5

Hi, I have parent / child tables:

parent table:

id | description

1 | Alexandra 2 | Natalia

child table:

id | id_parent | description

1 | 1 | Programmer 2 | 1 | Surgery 3 | 2 | Programmer 4 | 2 | IT

How to return set of record according filters, like if we want to get all records with "Programmer" we would like to have set:

response table:

id_parent | id_child | description (from child)

1     |    1     |  Programmer
2     |    3     |  Programmer

BUT if filter looks like: "Programmer" AND "Surgery", query MUST return only:

response table:

id_parent | id_child | description (from child)

1     |    1     |  Programmer
1     |    2     |  Surgery

As you can see I also need some kind of "join" in order to "connect" description and code in child table.

Thanks in advance.

A: 

select * from parent p left join child c on p.id = c.id_parent where c.description = 'Programmer' or c.description = 'Surgen'

Daniel Brink
Hi Daniel,Thank for you help, but this would not work. I'm sorry if I wasn't clear, I wanted to be brief.so, if child will look like:id_par, id_ch, descr1, 1, prog1, 2, surg1, 3, surg2, 4, prog2, 5, surg2, 6, surg3, 7, it3, 8, progif request child's "prog", we want:parent_id, child_id, child_descr1,1,prog2,4,prog3,8,progbut if "prog" AND "surg":parent_id, child_id, child_desc1, 1, prog1, 2, surg1, 3, surg2, 4, prog2, 5, surg2, 6, surgrecords in child might be duplicated and we want them. Only child records with ALL conditions must be in output.Thanks,Boris
Boris
A: 
SELECT t.id_parent,
       t.id,
       t.description
  FROM CHILD t
  JOIN CHILD p ON p.id_parent = t.id_parent AND p.description = 'Programmer'
  JOIN CHILD s ON s.id_parent = t.id_parent AND s.description = 'Surgery'

Based on your SQL, you need to use:

  SELECT p.ID, 
         c.ID, 
         c.ID_SpecMatrix 
    FROM Parent p
    JOIN Child c ON p.ID = c.ID_Parent AND c.ID_SpecMatrix = 4
    JOIN Child c2 ON p.ID = c2.ID_Parent AND c2.ID_SpecMatrix = 5
GROUP BY p.ID, 
         c.ID, 
         c.ID_SpecMatrix

The key is JOINing additional copies of the CHILD table. The ID_parent to ID links the records together; the AND filters that copy of the CHILD table to contain only rows where the ID_SpecMatrix matches your criteria.

The query needs to use dynamic SQL to be scalable to however many criteria you want to use.

OMG Ponies
Hi rexem,Thank for your answer, but this not work. Please take a look at my next post / comments to Daniel's post. Appreciate,Boris
Boris
@Boris: It's been corrected - I was using PARENT when it should've been CHILD, and the JOIN needed to be using the id_parent rather than ID.
OMG Ponies
Hi rexem. Thank you for patience, but still don't work. I can't put a big example here, system don't allow to post more than 600 symbols. My current query, work for 1 condition (I changed field names, but the same case):SELECT distinct Parent.ID, Child.ID, Child.ID_SpecMatrixFROM Parentinner JOIN Child ON Parent.ID = Child.ID_Parentwhere 1=1 and Child.ID_SpecMatrix = 4So i need to add second "and Child.ID_SpecMatrix = 5", and it don't work, cause no child records have at the same time codes "4" and "5".Thanks,Boris
Boris
Boris
rexem,I really appreciate your efforts, but still problems - I just tried your query on my database - "wrong" records - with "ID_SpecMatrix" other than 4,5 were fetched in addition to "right" ones. I need child's records only with id_specmatrix =4 and =5 (whatever that were given in condition)... Perhaps this happens cause of 1st join?Thanks,Boris
Boris
and sure i use dynamic sql, but it becomes increasingly difficult to manage it :) there are more than 100 parameters..
Boris
@Boris: No, all the joins to the CHILD table are on the same id.
OMG Ponies
rexem,If you still interested in, you could check/compare this with query that suggested Kosta (above). His sql works. But it very hard to figure out how to automate building such strings dynamically, depending on input parameters. I'm trying now idea of using 2 queries - 1-st selet with "standard" filters and define list (of id) of parent's records, then use 2nd query, add this list like primary filter like " and id in (..)" and then add list of child's conditions, like " and specmatrix in (...)". All queries constructed in code behind - c#...Thanks anyway,Boris
Boris
A: 

Hi Daniel,

Thank for you help, but this would not work. (I'm sorry if I wasn't clear, I wanted to be brief):

so, if child will look like:

id_parent, id_child, description

1, 1, "programmer"

1, 2, "surgery"

1, 3, "surgery"

2, 4, "programmer"

2, 5, "surgery"

2, 6, "surgery"

3, 7, "it"

3, 8, "programmer"

if request child's description is "programmer", we have to receive:

parent_id, child_id, child_description

1,1,programmer 2,4, programmer 3,8, programmer

but if = "programmer" AND "surgery":

parent_id, child_id, child_description

1, 1, "programmer"

1, 2, "surgery"

1, 3, "surgery"

2, 4, "programmer"

2, 5, "surgery"

2, 6, "surgery"

so, records in child might be duplicated and we want to see them. Only child records which include ALL conditions most be in output set. This is like kind of "intersection"...

Thanks, Boris

Boris
A: 

Hey, seems I found answer by myself - look:

SELECT distinct parent.id

FROM parent p JOIN Child s1 ON s1.id_parent = p.id AND s1.id_specmatrix = 4 JOIN Child s2 ON s2.id_parent = p.id AND s2.id_specmatrix = 5

hmm.. but how now I can retrieve child's id and "id_specmatrix" in select clause? If I'll use s1.id_specmatrix, I'll get records from first join and so on... ???

Seems only successive requests, but again - how to get records from all selects?

Thanks in advance. Boris.

Boris
i'm not really sure as to what the problem is, but have you tried a UNION? ie: select * from child where description = 'programmer' union select * from child where description = 'surgery'
Daniel Brink
This will not work, because I need only parents records that have both records together from child - with "programmer" and "surgery". Your query will bring parent's records that could have only one of this.Thanks,Boris
Boris
A: 

How about this

SELECT * from CHILD where id_specmatrix in (4,5) and Parent_ID IN (SELECT distinct parent.id FROM parent p JOIN Child s1 ON s1.id_parent = p.id AND s1.id_specmatrix = 4 JOIN Child s2 ON s2.id_parent = p.id AND s2.id_specmatrix = 5)

Kosta
Yep! Man, this works! Thanks a lot! The only question - how to make this flexible - parameters number could change and fields like "id_specmatrix" about 100... Anyway I appreciate, idea was right!Thanks,Boris
Boris