Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have data in the below format:-

Col1 	Col2	Col3
Path1	A1	Value1
Path1	A1	Value2
Path1	A1	Value3
Path1	A1	Value4
Path1	A1	Value5
Path1	A2	Value6
Path1	A2	Value7
Path1	A2	Value8
Path1	A2	Value9


I want the output in the below format:-

Col1 	Col2	Col3
Path1	A1	Value1
Path1	A1	Value2
Path1	A2	Value6
Path1	A2	Value7


In short for every path and for every unique Col2 value (A1,A2) we need to pick any 2 values from col3.

What I have tried:

Have tried using row_number using partition by and ranking function but no luck
Posted
Updated 9-Dec-19 3:15am
v2
Comments
Herman<T>.Instance 9-Dec-19 5:17am    
Show code to see what you did

 
Share this answer
 
Comments
chints786 9-Dec-19 5:11am    
Not working
Herman<T>.Instance 9-Dec-19 5:17am    
Why not?
phil.o 9-Dec-19 5:18am    
Please show what you actually tried, instead of a vague description, and you may get some more detailed answers. Use the green Improve question widget which appears on hovering your question for that matter.
Richard MacCutchan 9-Dec-19 5:18am    
Then your code must be wrong.
Try this:

SQL
DECLARE @tmp TABLE(Col1 NVARCHAR(100), Col2 NVARCHAR(100), Col3 NVARCHAR(100))

INSERT INTO @tmp(Col1, Col2, Col3)
VALUES('Path1', 'A1', 'Value1'),
('Path1', 'A1', 'Value2'),
('Path1', 'A1', 'Value3'),
('Path1', 'A1', 'Value4'),
('Path1', 'A1', 'Value5'),
('Path1', 'A2', 'Value6'),
('Path1', 'A2', 'Value7'),
('Path1', 'A2', 'Value8'),
('Path1', 'A2', 'Value9')


SELECT Col1, Col2, Col3
FROM (
	SELECT Col1, Col2, Col3, ROW_NUMBER() OVER(PARTITION BY COl2 ORDER BY Col1, Col2) AS RowNo  
	FROM @tmp) AS T
WHERE T.RowNo IN (1, 2)

Result:
Col1	Col2	Col3
Path1	A1	Value1
Path1	A1	Value2
Path1	A2	Value6
Path1	A2	Value7


Note: there's few other ways to resolve your issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900