Results 1 to 5 of 5

Thread: subqueries

  1. #1
    Join Date
    May 2003
    Posts
    47

    Question Unanswered: subqueries

    table
    -----

    c1 c2
    --------

    a 1
    b 2
    c 1
    d 3
    e 4
    c 5
    a 2


    distinct c2 values r 1,2,3,4,5
    distinct c1 values r a,b,c,d,e

    all c1 does not have all c2's.

    so what should b the query to insert into a new table values
    a 1
    a 2
    b 2
    c 1
    c 5
    d 3
    e 4


    should i use nested subquery, joins,exists clause etc..
    suggestions highly appreciated...

    thanks

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    Here is a free clue, the order of records within a table is indeterminate.

    INSERT INTO NEW_TABLE (SELECT * FROM OLD_TABLE ORDER BY C1);
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    May 2003
    Posts
    47

    required query is different...

    sorry,
    i think my question was unclear...

    i have 2 cursors defined for each select..

    outerloop

    fetch c1

    inner loop

    fetch c2

    insert into tablename...(will insert distinct c1 * c2)

    for example it will insert a value for a 3 as 0 as there r no matching records

    i want to avoid this situation...

  4. #4
    Join Date
    Mar 2002
    Location
    Reading, UK
    Posts
    1,137
    I'm still not absolutely clear what you want but if you want to get all distinct combinations of c1 and c2 then this will do it.

    insert into table x
    select distinct c1,c2 from y

    Alan

  5. #5
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    Without knowing what the querys are that make up your two cursor loops, there is no way that we can generate a query to fix your problem.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •