如何在SQL Server中通过以下查询进行排序 [英] How can I order by in SQL server in the following query
问题描述
我的表格比Group_ID,Product_ID,Is_Prime_Product
和记录如下
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假
3 3真实
3 1假
我尝试了什么:
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
推荐答案
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
这篇关于如何在SQL Server中通过以下查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!