Technical Article

Get  only the ODD record values from a set

,

Using a Having clause I eliminate getting the EVEN rows
and getting ovly the odd primary keys

By using Having (count(*) %2) = 0 I will get the even and not the odd.

SET NOCOUNT ON
CREATE TABLE #set1 (k1 int identity)

INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 
INSERT #set1 DEFAULT VALUES 

SELECT s1.k1 
FROM #set1 s1 JOIN #set1 s2 ON (s1.k1 >= s2.k1)
GROUP BY s1.k1
HAVING (COUNT(*) % 2) = 1

GO
DROP TABLE #set1

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating