DBAsupport.com Forums - Powered by vBulletin
Results 1 to 5 of 5

Thread: Need SQL Query help

  1. #1
    Join Date
    Apr 2000
    Location
    Bangalore
    Posts
    16
    hi friends,

    I have creted table TBL with structure as below.

    SQL> desc tbl
    Name Null? Type
    ----------------------------------------- -------- ----------------------------
    COL1 VARCHAR2(5)
    COL2 VARCHAR2(5)
    KEY VARCHAR2(5)

    Sample data is:


    COL1 COL2 KEY
    ----- ----- -----
    y val2 ST
    x val1 ST

    I need query to return output.

    select col1,col2 from tbl where key='ST';

    If col1 has only x , print corresponding row. If col1 has only y , print corresponding row. If col1 has two values both x and y, print first row only.

    Thanks,
    Bhaskara

  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    What does "print first row only" mean? How do you determine the "first row"?
    Jeff Hunter

  3. #3
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    The first part is:
    select t.* from tbl t, (select key, count(*) keycount from tbl group by key) t2
    where t2.keycount = 1
    and t.key = t2.key
    Jeff Hunter

  4. #4
    Join Date
    Dec 2000
    Location
    Ljubljana, Slovenia
    Posts
    4,439
    If you realy want a result for a *single* key, then the sollution is trivial -simply add the "WHERE ROWNUM=1":

    select col1,col2 from tbl where key='ST' and rownum=1;

    But if you want a result set for many keys (single row per key), then use this:

    select col1, col2, key from tbl
    where rowid in (select min(rowid) from tbl group by key);

    HTH,
    Jurij Modic
    ASCII a stupid question, get a stupid ANSI
    24 hours in a day .... 24 beer in a case .... coincidence?

  5. #5
    Join Date
    Nov 2000
    Posts
    48
    Is it that if the KEY column contains X and Y you want to print the row with the X value in COL1? and pl let me know how the COL1 can have value X and Y ? Is it like X Y or X,Y etc?

    Thanks,

    Rajesh

Posting Permissions

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


Click Here to Expand Forum to Full Width