Select from a Select ?

Discussion in 'Databases' started by guitarbinge, Nov 12, 2009.

  1. #1
    Hello,

    I have a single table and I'm trying to select some data out of a subset of that data.

    For example, something like:

    SELECT *
    FROM (SELECT * FROM Table WHERE ....)
    WHERE.......


    It is being done on a single table and should return several results (not the single result that i believe a subquery returns)

    Does anyone know a way of doing this?
     
    guitarbinge, Nov 12, 2009 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Subquery always return the results that matches to the condition whether a single or more than one.
     
    mwasif, Nov 12, 2009 IP
  3. guitarbinge

    guitarbinge Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok but theres one more complication,

    I have to run a complicated string function check as the condition on the subset

    If this condition is run on the whole table, it results in an "invalid procedure call error"

    That's why I have to run it on just a subset of the main table. If the condition is run on the subset, it will return all the correct records.

    So if i do it this way:

    SELECT *
    FROM (SELECT * FROM table WHERE ..condition to extract the subset..)
    WHERE (string condition)

    it gives that error because its not applying the string condition just to the subset, but to the whole table where the string condition can't apply..

    If i do:

    SELECT *
    FROM (SELECT * FROM Table WHERE...condition to extract the subset....AND string condition)

    it also gives an error because the string condition is still being applied to the whole table.


    I need to extract the subset, keep it in isolation, then perform the string function upon it....:confused:
     
    guitarbinge, Nov 12, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    You could try using a self join.

    You could also try something like:

    SELECT * FROM my_table WHERE id_column IN (
    SELECT id_column FROM my_table WHERE condition...
    )
     
    jestep, Nov 12, 2009 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    why not try self join?
    please share data structure and sample data with desired output and conditions to help us build a query for you..
     
    mastermunj, Nov 15, 2009 IP
  6. ziya

    ziya Well-Known Member

    Messages:
    1,971
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Why twice selecting on one table ? If it is possible Write down the structure and let us know what do you want data do you want to collect ?
     
    ziya, Nov 16, 2009 IP
  7. same

    same Greenhorn

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    i think it is like SELECT * FROM TABLE WHERE EMPID IN (SELECT EMPID FROM TABLE WHERE ) like this
     
    same, Nov 19, 2009 IP