Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in than Group_ID,Product_ID,Is_Prime_Product
and records are as follows

Group_ID Product_ID Is_Prime_Product

1 2 false
1 1 true
1 3 false
1 4 false

2 3 false
2 1 true
2 2 false


3 2 false
3 3 true
3 1 false

What I have tried:

i want to order by such as result should be as order by of each group and top row of each group shoul be Is_prime_Product 



Group_ID Product_ID Is_Prime_Product 
1            1             true------
1            2             false
1            3             false
1            4             false
2            1             true------
2            3             false
2            2             false
3            3             true------
3            2             false
3            1             false

Thanks in advance

Thanks in Advance
Posted
Updated 22-Nov-17 20:54pm
Comments
Mehdi Gholam 23-Nov-17 2:18am    
What is your query string?

1 solution

CREATE TABLE #TEMP(Group_ID int, Product_ID int, Is_Prime_Product char(10));
INSERT INTO #TEMP VALUES
(1,2,'false'),
(1,1,'true '),
(1,3,'false'),
(1,4,'false'),
(2,3,'false'),
(2,1,'true'),
(2,2,'false'),
(3,2,'false'),
(3,3,'true'),
(3,1,'false');

SELECT Group_ID,Product_ID,Is_Prime_Product 
    FROM #TEMP 
      GROUP BY Group_ID,Product_ID,Is_Prime_Product 
       ORDER BY Group_ID,Is_Prime_Product DESC
--------------------------------------------------
Group_ID	Product_ID	Is_Prime_Product
--------------------------------------------------
1	1	true      
1	2	false     
1	3	false     
1	4	false     
2	1	true      
2	2	false     
2	3	false     
3	3	true      
3	1	false     
3	2	false     
 
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