SQL Server Query (complicated)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murali

    SQL Server Query (complicated)

    Folks,

    I have the following data in a table:

    4 NULL NULL
    2 abc NULL
    2 aaa NULL
    4 xyz NULL
    4 xyz pqr
    4 pyz xqr

    I want to get only one record for each record number. that is, the
    result set should be like this:

    4 NULL NULL
    2 abc NULL

    Please suggest how the query should be built. Thanks for the help.

    Murali
  • Simon Hayes

    #2
    Re: SQL Server Query (complicated)

    Unfortunately, it's not easy to give a good answer without more information.
    From what you have posted below, it's not clear why you want those two those
    records and not one of the others, unless perhaps there is another column
    which you didn't include and that you use to order by.

    I suggest you post a CREATE TABLE for your table, with some INSERT
    statements to add test data, and then a sample result set that you would
    like to see.

    Simon

    "Murali" <murali_pinnint i@hotmail.com> wrote in message
    news:62ad0475.0 306300616.4c514 9@posting.googl e.com...[color=blue]
    > Folks,
    >
    > I have the following data in a table:
    >
    > 4 NULL NULL
    > 2 abc NULL
    > 2 aaa NULL
    > 4 xyz NULL
    > 4 xyz pqr
    > 4 pyz xqr
    >
    > I want to get only one record for each record number. that is, the
    > result set should be like this:
    >
    > 4 NULL NULL
    > 2 abc NULL
    >
    > Please suggest how the query should be built. Thanks for the help.
    >
    > Murali[/color]


    Comment

    • Murali

      #3
      Re: SQL Server Query (complicated)

      Thanks Anith & others.

      I think I confused you. I'm sorry. What I want exactly is, ONLY ONE
      RECORD for each <col1>. I don't care whether it's first or last. But
      it's better if I get the first record.

      Ex: Say I've this data in test table:
      Pno Pname
      1 xxxx
      1 NULL
      1 YYYYY
      2 NULL
      2 abcd

      I want the result to be:

      1 xxxxx
      2 NULL

      Hope, I'm clear this time. Thanks for the help.

      Murali

      "Anith Sen" <anith@bizdatas olutions.com> wrote in message news:<6PXLa.296 57$0v4.2203622@ bgtnsc04-news.ops.worldn et.att.net>...[color=blue]
      > First, add keys to your tables. Then, if possible, try not to use NULLs as
      > logical values. In any case, do:
      >
      > SELECT *
      > FROM tbl
      > WHERE COALESCE(col2, 'aaa') =
      > (SELECT TOP 1 COALESCE(t1.col 2, 'aaa')
      > FROM tbl t1
      > WHERE t1.col1 = tbl.col1
      > ORDER BY t1.col2);[/color]

      Comment

      • Anith Sen

        #4
        Re: SQL Server Query (complicated)

        >> But it's better if I get the first record. <<

        There is no 'first' & 'last' rows in SQL. In fact logically a table is a set
        of rows & by definition of sets the rows have no order. Data is retrieved
        based on the values in the table, not by position of rows.

        For the example in your second post, you can do:

        SELECT Pno, MAX(Pname) --- or MIN
        FROM tbl
        GROUP BY Pno ;

        If you have other columns, here is a generalized soln :

        SELECT *
        FROM tbl
        WHERE keycol = ( SELECT TOP 1 t1.keycol
        FROM tbl t1
        WHERE t1.dupcol = tbl.dupcol
        ORDER BY t1.uniqueCol DESC ) ;

        In the above, uniquecol is the column or set of columns which can uniquely
        identify a row per group (pname in your case) in the table & dupcol is the
        column which is being duplicated (pno in your case )

        --
        - Anith
        ( Please reply to newsgroups only )


        Comment

        Working...