SELECT Statement Question

  • How can I create a select statement that uses a sub query to generate its values as well as the initial sub query link value? Probably best shown in an example:

    SELECT     ID

    FROM         TableA

    WHERE     (ID IN

                              (SELECT     ID

                                FROM          TableA

                                WHERE      ID = x ))

    GROUP BY ID

    So what I want is to produce a list (as in the sub query) for all records linked to value x but I also want value x to be returned in the recordset. Hope this makes sense!

     

  • Is this what you need?

    SELECT  A.ID

    FROM    TableA A

    WHERE  ID IN (  SELECT     ID

                          FROM          TableA B

                          WHERE      B.ID = A.ID ))

    GROUP BY A.ID

    If not, would you submit an example on how data would be?

    Nicolas Donadio

  • Also...

    submit a real example... this one would always return a single value ( your "x" )

    and it's the same as

    SELECT ID FROM TableA WHERE ID = x

    no need to use subqueries...

    Nicolas Donadio

  • My table has two ID columns, ID and PID. What I want to do is to return all PIDs where the ID is lets say 10. This will produce a list of all matching PIDs. In addition, I want the recordset to include the ID value too. So for example if the following is a list of the matching PIDs - 1, 2, 3 and 4 I want the results to produce a list such as 1, 2, 3, 4 and 10.

    Thanks

  • I don't really follow what you want. If you have two columns

    all you need is:

    Select ID, PID from TableA where ID =x -- This give you all PID matching that ID

    but ID will always be equal to x, why do you need to return that ?

     

     


    * Noel

  • The value of ID also needs to be included because the result set is used in another statement and ID needs to be processed too. Is there any way that I could get the list of PIDs and then append the value of ID to that list?

  •  

    Sure!

     

    Select  PID as ID from TableA where ID =x

    union all

    select x

     


    * Noel

  • That simple! Thank you Noel!

  • Happy to Help


    * Noel

Viewing 9 posts - 1 through 9 (of 9 total)

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