Link to home
Start Free TrialLog in
Avatar of Southern_Gentleman
Southern_GentlemanFlag for United States of America

asked on

2 Select Distinct

I have a query
SELECT key, value
FROM tableX
    ( SELECT key, value,
             ROW_NUMBER() OVER (PARTITION BY key 
                                ORDER BY whatever)     --- ORDER BY NULL
               AS rn                                   --- for example
      FROM tableX
    ) tmp 
WHERE rn = 1 ;

Open in new window


That has this table
key     value
===     =====
one     test
one     another
one     value
two     goes
two     here
two     also
three   example

Open in new window


resulting to this:
key     value
===     =====
one     test
two     goes
three   example

Open in new window


But i want it to result in the bottom result of the value column instead:
key     value
===     =====
one     value
two     also
three   example

Open in new window

Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Hi,
pls try this..

;WITH CTE AS
(
	SELECT key, value
	FROM tableX
		( SELECT key, value,
				 ROW_NUMBER() OVER (PARTITION BY key 
									ORDER BY whatever)     --- ORDER BY NULL
				   AS rn                                   --- for example
		  FROM tableX
		) tmp 
	WHERE rn = 1
)
SELECT c.Key, p.Value FROM CTE c
CROSS APPLY
(
	SELECT TOP 1 b.value FROM 
	CTE b WHERE b.key = c.key	
)p

Open in new window


Hope it helps!
Avatar of PortletPaul
You indicate that the "natural order" of the rows will produce the wanted result. But tables HAVE NO ORDER so there is no guarantee the result will match you expectation UNLESS there is something else we can definitely use to order by, perhaps an auto-increment ID?


SELECT key, value
FROM tableX
    ( SELECT key, value,
             ROW_NUMBER() OVER (PARTITION BY key 
                                ORDER BY ID DESC) --- ORDER BY reverse sequence of insert
               AS rn                              --- although this isn't perfect
      FROM tableX
    ) tmp 
WHERE rn = 1 ;

Open in new window

NB:
   using TOP requires an ORDER BY or the results are unpredictable
Hi Author,
What is the logic by which you will pick the Value column.? Check out the below Query. I just need the Order by logic.

Try below to see if it works for you-

;WITH CTE AS
(
      SELECT key, value
      FROM tableX
            ( SELECT key, value,
                         ROW_NUMBER() OVER (PARTITION BY key
                                                      ORDER BY whatever)     --- ORDER BY NULL
                           AS rn                                   --- for example
              FROM tableX
            ) tmp
      WHERE rn = 1
)
SELECT c.Key, p.Value FROM (SELECT DISTINCT Key FROM CTE) c
CROSS APPLY
(
      SELECT TOP 1 b.value FROM
      CTE b WHERE b.key = c.key
       ORDER BY b.Value DESC      
)p
Avatar of Southern_Gentleman

ASKER

something isn't jiving with the syntax

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
never mind, it works pretty good.
Excellent !! Glad to help !

Thank You!
Pawan