sql server - Using dynamic subqueries as column header -
i have table columns foo, bar , baz, foo repeated once each entry in bar, possible entry in baz each bar. example:
|-----|-----|-----| | foo | bar | baz | |-----|-----|-----| | 1 | 1 |alpha| | 1 | 2 |beta | | 1 | 3 |gamma| | 2 | 1 |delta| | 2 | 2 |beta | | 2 | 3 |kappa| | 3 | 1 | | | 3 | 2 | | | 3 | 3 |iota | |-----|-----|-----| etc this table, in practice, has millions of entries. i'm looking situations 2 different foo entries have matching bar+baz entries (for instance, find foo 1 , foo 2 above, since both have beta in bar 2), given range of foo, it's not looking @ entire table.
i'm not sure if possible , various permutations of subqueries haven't worked far, i've been trying make query lists foo, next columns baz bar = x , foo = [the foo current row]
so table above, if looking @ bar 2 , bar 3, be:
|-----|-----|-----| | foo |bar=2|bar=3| |-----|-----|-----| | 1 |beta |gamma| | 2 |beta |kappa| | 3 | |iota | |-----|-----|-----| i'm relatively inexperienced sql, may not aware of particular syntax , haven't been able google up. i've tried using
select foo, (baz bar=2) 'bar=2', (baz bar=3) 'bar=3' from.... to no result, wasn't entirely unexpected. i've considered putting full select queries column header, can't see way reference 'foo line' in them. chasing impossible idea here? alternative use external application run many smaller queries , store data way instead, that's plan if cannot done entirely within sql server.
i'm looking situations 2 different foo entries have matching bar+baz entries (for instance, find foo 1 , foo 2 above, since both have beta in bar 2)
that's quite simple. can accomplish self-joining.
select * table1 t1 inner join table1 t2 on t1.foo != t2.foo t1.bar = t2.bar , t1.baz = t2.baz see working live here.
the rest of question don't understand @ all. it's has nothing asked above. why looking @ bar = 2 , bar = 3. , why gamma, kappa , iota listed in result? please clarify...
Comments
Post a Comment