Mostly Bollogs, I'm afraid

But occasionally, a glimmer of truth.
If you find one, please let me know.



Thursday, 13 February 2014

SQL

If anyone can help me with this I'll pay in chocolate/beer.

I have some objects which are linked via a link table, which has Parent/Child columns.

Here is the scenario. The ones above are parents, the ones below are children.

The table linking these like this is thus:

P    C
1    2
1    3
1    4
1    5
1    6
2    7
2    8
2    9
7   10
7   11
10  12
10  13
4    12
6    13

Where P=Parent, C=Child.

I want to devise a query where if I pick a Child (say 13) I get the rows back which are affected by said child.

As in the ones asterisked:

P    C
1    2 *
1    3
1    4
1    5
1    6
2    7 *
2    8
2    9
7   10 *
7   11
10  12
10  13 *
4    12
6    13 *

I know it's doable. I'm too thick/tired to work it out. It must be easy.

The DB is SQLserver.



1 comment:

Anonymous said...

Which version of SQL server ?

No access to test just now, but sounds like you need help from analytic functions. http://technet.microsoft.com/en-us/library/hh231256.aspx