SQL 服务器 Row_number 按个案排序
SQL Server Row_number in ORDER BY CASE
编辑了整个主题。
我需要创建一个按类型对文章进行排序的视图。
- 如果我只有类型:*VALUE -> 我只需要显示这一行。
- 如果我有以下类型:*VALUE & 2 -> 仍然显示与 *VALUE 类型相应的行。
- 如果我只有类型 : 2 -> 显示这个。
我已经做了这样的事情:
VALUE* 是一个值,应该来自另一个 table 和一个 Join。
SELECT Id_item ,Name_item , Type_item , Id_type_item 来自项目
WHERE Name_item = 'Gillette' AND (Id_Type_item = VALUE* OR Id_Type_item ='10')<br>
按情况订购
WHEN row_number() OVER(ORDER BY Id_item DESC , Id_Type_Item DESC) <= 1 THEN 0
其他 1
结束;
但如果我们有两行类型 (*VALUE & 10):
Id_item / Name_item / Type_item / Id_Type_Item
1 Gillette 45 30 (*VALUE)
1 Gillette 2 10
所以我认为 Over() 上的顺序对于始终按 *VALUE 排序很有用(实际上是另一个 table 的另一列)
我一直想要select只有1行数据! :)
我猜,您想要的是每个 SELECT
返回的 "first" 行?无需为同一 table 上的每个变量使用单独的 SELECT
语句,您可以使用 window 函数来执行此操作。我相信这就是您想要的。
WITH CTE AS(
--The following assumes table A and B have the same DDL (which begs the question, why are they different tables?)
SELECT *,
ROW_NUMBER() OVER (PARTITION BY var
ORDER BY (SELECT NULL)) AS RN --Replace SELECT(NULL) with your actual ordering criteria
FROM A
WHERE var IN (1,2)
UNION --ALL(?)
SELECT *
ROW_NUMBER() OVER (PARTITION BY var
ORDER BY (SELECT NULL)) AS RN --Replace SELECT(NULL) with your actual ordering criteria
FROM B
WHERE var IN (3))
SELECT *
FROM CTE
WHERE RN = 1;
这是一个可能的解决方案。在这种情况下 ROW_NUMBER、RANK 和 DENSE_RANK 都可以。但是,ROW_COUNT 在 sql 服务器中不是有效的 window 函数。
DECLARE @A TABLE(ID INT, Value INT)
DECLARE @B TABLE(ID INT,Value INT)
INSERT INTO @A VALUES (1,1),(2,1),(3,2),(4,3),(5,2),(6,1),(7,3)
INSERT INTO @B VALUES (1,1),(2,1),(3,1),(4,2),(5,3),(6,2),(7,1),(8,3)
;WITH D AS
(
SELECT ID,Value FROM @A WHERE Value IN(1,2)
UNION ALL
SELECT ID,Value FROM @B WHERE Value IN (3)
)
SELECT * FROM
(
SELECT
ID, Value,
ValueRankInSet = DENSE_RANK() OVER(PARTITION BY VALUE ORDER BY ID) -- <-- If you do not have an ID field you can subst ID with NEWID() as order is not important
FROM D
)AS X
WHERE ValueRankInSet = 1
在您的 Select(s) 中分配优先级,然后在 row_number 中按优先级排序:
with cte as
(
SELECT *,
row_number()
over (-- partition by ???
order by prio) as Position
FROM
(
SELECT 1 as prio, * FROM A WHERE var = 1
UNION -- probably a more efficient UNION ALL
SELECT 2 as prio, * FROM A WHERE var = 2
UNION -- probably a more efficient UNION ALL
SELECT 3 as prio, * FROM B WHERE var = 3
)
)
select *
from cte
WHERE Position = 1
编辑了整个主题。
我需要创建一个按类型对文章进行排序的视图。
- 如果我只有类型:*VALUE -> 我只需要显示这一行。
- 如果我有以下类型:*VALUE & 2 -> 仍然显示与 *VALUE 类型相应的行。
- 如果我只有类型 : 2 -> 显示这个。
我已经做了这样的事情:
VALUE* 是一个值,应该来自另一个 table 和一个 Join。
SELECT Id_item ,Name_item , Type_item , Id_type_item 来自项目
WHERE Name_item = 'Gillette' AND (Id_Type_item = VALUE* OR Id_Type_item ='10')<br>
按情况订购
WHEN row_number() OVER(ORDER BY Id_item DESC , Id_Type_Item DESC) <= 1 THEN 0
其他 1
结束;
但如果我们有两行类型 (*VALUE & 10):
Id_item / Name_item / Type_item / Id_Type_Item
1 Gillette 45 30 (*VALUE)
1 Gillette 2 10
所以我认为 Over() 上的顺序对于始终按 *VALUE 排序很有用(实际上是另一个 table 的另一列)
我一直想要select只有1行数据! :)
我猜,您想要的是每个 SELECT
返回的 "first" 行?无需为同一 table 上的每个变量使用单独的 SELECT
语句,您可以使用 window 函数来执行此操作。我相信这就是您想要的。
WITH CTE AS(
--The following assumes table A and B have the same DDL (which begs the question, why are they different tables?)
SELECT *,
ROW_NUMBER() OVER (PARTITION BY var
ORDER BY (SELECT NULL)) AS RN --Replace SELECT(NULL) with your actual ordering criteria
FROM A
WHERE var IN (1,2)
UNION --ALL(?)
SELECT *
ROW_NUMBER() OVER (PARTITION BY var
ORDER BY (SELECT NULL)) AS RN --Replace SELECT(NULL) with your actual ordering criteria
FROM B
WHERE var IN (3))
SELECT *
FROM CTE
WHERE RN = 1;
这是一个可能的解决方案。在这种情况下 ROW_NUMBER、RANK 和 DENSE_RANK 都可以。但是,ROW_COUNT 在 sql 服务器中不是有效的 window 函数。
DECLARE @A TABLE(ID INT, Value INT)
DECLARE @B TABLE(ID INT,Value INT)
INSERT INTO @A VALUES (1,1),(2,1),(3,2),(4,3),(5,2),(6,1),(7,3)
INSERT INTO @B VALUES (1,1),(2,1),(3,1),(4,2),(5,3),(6,2),(7,1),(8,3)
;WITH D AS
(
SELECT ID,Value FROM @A WHERE Value IN(1,2)
UNION ALL
SELECT ID,Value FROM @B WHERE Value IN (3)
)
SELECT * FROM
(
SELECT
ID, Value,
ValueRankInSet = DENSE_RANK() OVER(PARTITION BY VALUE ORDER BY ID) -- <-- If you do not have an ID field you can subst ID with NEWID() as order is not important
FROM D
)AS X
WHERE ValueRankInSet = 1
在您的 Select(s) 中分配优先级,然后在 row_number 中按优先级排序:
with cte as
(
SELECT *,
row_number()
over (-- partition by ???
order by prio) as Position
FROM
(
SELECT 1 as prio, * FROM A WHERE var = 1
UNION -- probably a more efficient UNION ALL
SELECT 2 as prio, * FROM A WHERE var = 2
UNION -- probably a more efficient UNION ALL
SELECT 3 as prio, * FROM B WHERE var = 3
)
)
select *
from cte
WHERE Position = 1