HELP: Select within select (looping through records)

  • I have a situation that I don't know how to solve using either t-sql or just select/join statements:

    Step 1:

    I have a select statement that selects a set of records from tables, as follows:

    select attrA from tableA inner join tableB on tableA.attrC = tableB.attrA where attrC = 'ABC'

    Step 2:

    I want to use the results in step 1 (attrA) to feed the 'where clause' of my next selection:

    select tableC.attrA, sum(tableC.attrB) as sumA

    from tableD

    inner join tableC on tableD.atrrB = tableC.atrrB

    where tableC.attrC = '1234'

    group by tableC.attrC

    Step 2's result would be my final selection which has only one record. But I want to combine step 1 and 2. This is like using many, many 'where tableC.attrC = '1234' or 'where tableC.attrC = '5678', etc. But I want to combine them and make a single select statement or combined selects into a single query. How would I do it?

    Thanks,

    Tom

  • I have a select statement that selects a set of records from tables, as follows:

    select attrA from tableA inner join tableB on tableA.attrC = tableB.attrA where attrC = 'ABC'

    Step 2:

    I want to use the results in step 1 (attrA) to feed the 'where clause' of my next selection:

    select tableC.attrA, sum(tableC.attrB) as sumA

    from tableD

    inner join tableC on tableD.atrrB = tableC.atrrB

    where tableC.attrC = '1234' group by tableC.attrC

    Step 2's result would be my final selection which has only one record. But I want to combine step 1 and 2. This is like using many, many 'where tableC.attrC = '1234' or 'where tableC.attrC = '5678', etc. But I want to combine them and make a single select statement or combined selects into a single query. How would I do it?

    Thanks,

    Tom

    Are you talking about something like this:

    select tableC.attrA, sum(tableC.attrB) as sumA

    from tableD

    inner join tableC on tableD.atrrB = tableC.atrrB

    where tableC.attrC = (select attrA from tableA inner join tableB on tableA.attrC = tableB.attrA where attrC = 'ABC')

    group by tableC.attrC

  • The whole things looks like it could be solved simply by using "Where In" on the column you want and the sub-query you want. Am I missing something?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I tried your suggestion. After replacing the 'where ... =' with 'where ... in' as GSquared suggested, it worked!!

    Thanks so much.

  • Yep! I got it. Thanks!!

  • Cool biz. Glad we could help.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply