Home All Groups Group Topic Archive Search About

SQL Query to get the result

Author
8 Jun 2006 3:24 PM
NAT
I have the below table :
               Column1     Column2    Column3     Column4   
Row1           T1               T2           12               12
Row2           T3               T4           12               12
Row3           T5               T6           13               13
Row4           T7               T8           14               14

I need a query to get the following result:

My condition is to select the records for which Column3 = Column4. But when
i put this condition in sql I get all the Rows. But I want only Row3,Row4 and
ANY ONE row out of Row1 and Row2.Bcoz AS PER the Business rules Row1 and Row2
are identical(for me).Using "GroupBy" could b difficult bcoz I have more than
30 columns in my original table.
Thanks in advance

Author
8 Jun 2006 3:57 PM
Roy Harvey
>ANY ONE row out of Row1 and Row2

There isn't any function in SQL to pick "any one".  What you must do
is provide a way to select a specific one from the group.  Often this
is done by picking the row with the lowest, or highest, value of a
datetime, or of some ID number.  Without familiarity with the details
of your table all I can give you is a generic example.

SELECT *
  FROM Whatever as W
WHERE NOT EXISTS
       (select * from Whatever as X
         where W.Column3 = X.Column3
           and W.Column4 = X.Column4
           and (W.Column1 > X.Column1
            or  (W.Column1 = X.Column1
           and   W.Column2 > X.Column2)))

This would work if the key to the table were the four columns given.
In general, all the columns of the key would have to be involved.

Hope that helps.

Roy Harvey
Beacon Falls, CT

On Thu, 8 Jun 2006 08:24:01 -0700, NAT <N**@discussions.microsoft.com>
wrote:

Show quote
>I have the below table :
>               Column1     Column2    Column3     Column4   
>Row1           T1               T2           12               12
>Row2           T3               T4           12               12
>Row3           T5               T6           13               13
>Row4           T7               T8           14               14
>
>I need a query to get the following result:
>
>My condition is to select the records for which Column3 = Column4. But when
>i put this condition in sql I get all the Rows. But I want only Row3,Row4 and
>ANY ONE row out of Row1 and Row2.Bcoz AS PER the Business rules Row1 and Row2
>are identical(for me).Using "GroupBy" could b difficult bcoz I have more than
>30 columns in my original table.
>Thanks in advance

AddThis Social Bookmark Button