我有什么
来自以下#MyTable
我只有Name
和Number
列。
我的目标是使用渐进数字填充Number = NULL的值,并将我写入的值写入Desidered_col
列。
+------+--------+---------------+
| Name | Number | Desidered_col |
+------+--------+---------------+
| John | 1 | 1 |
| John | 2 | 2 |
| John | 3 | 3 |
| John | NULL | 4 |
| John | NULL | 5 |
| John | 6 | 6 |
| Mike | 1 | 1 |
| Mike | 2 | 2 |
| Mike | NULL | 3 |
| Mike | 4 | 4 |
| Mike | 5 | 5 |
| Mike | 6 | 6 |
+------+--------+---------------+
我尝试了什么
我尝试过以下查询
SELECT Name, Number, row_number() OVER(PARTITION BY [Name] ORDER BY Number ASC) AS rn
FROM #MyTable
但它首先放置所有NULL值,然后计算行数。 我怎样才能填空值?
为什么我认为这不是一个重复的问题
我已阅读this question和this question,但我不认为它是重复的,因为他们不考虑PARTITION BY
结构。
这是创建和填充表格的脚本
SELECT *
INTO #MyTable
FROM (
SELECT 'John' AS [Name], 1 AS [Number], 1 AS [Desidered_col] UNION ALL
SELECT 'John' AS [Name], 2 AS [Number], 2 AS [Desidered_col] UNION ALL
SELECT 'John' AS [Name], 3 AS [Number], 3 AS [Desidered_col] UNION ALL
SELECT 'John' AS [Name], NULL AS [Number], 4 AS [Desidered_col] UNION ALL
SELECT 'John' AS [Name], NULL AS [Number], 5 AS [Desidered_col] UNION ALL
SELECT 'John' AS [Name], 6 AS [Number], 6 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], 1 AS [Number], 1 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], 2 AS [Number], 2 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], NULL AS [Number], 3 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], 4 AS [Number], 4 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], 5 AS [Number], 5 AS [Desidered_col] UNION ALL
SELECT 'Mike' AS [Name], 6 AS [Number], 6 AS [Desidered_col]
) A
答案 0 :(得分:1)
您还可以使用ORDER BY
函数和select 1 or select null
子句(select *,
row_number() over (partition by Name order by (select 1)) New_Desidered_col
from #MyTable
)
row[1]
答案 1 :(得分:1)
为此,您需要一个指定表中行的顺序的列。您可以使用identity()
功能执行此操作:
SELECT identity(int, 1, 1) as MyTableId, a.*
INTO #MyTable
. . .
我非常确定SQL Server会遵循values()
语句的顺序,并且在实践中将遵循union all
的顺序。如果您愿意,可以在每一行中明确地放置此列。
然后你可以用它来分配你的价值:
select t.*,
row_number() over (partition by name order by mytableid) as desired_col
from #MyTable
答案 2 :(得分:1)
此查询有点复杂但似乎返回了您的预期结果。唯一可能是错误的情况是有人没有Number = 1
。
我们的想法是你必须找到数字之间的间隙,并计算可以使用多少空值来填充它们。
示例数据
create table #myTable (
[Name] varchar(20)
, [Number] int
)
insert into #myTable
insert into #myTable
SELECT 'John' AS [Name], 1 AS [Number] UNION ALL
SELECT 'John' AS [Name], 2 AS [Number]UNION ALL
SELECT 'John' AS [Name], 3 AS [Number] UNION ALL
SELECT 'John' AS [Name], NULL AS [Number] UNION ALL
SELECT 'John' AS [Name], NULL AS [Number] UNION ALL
SELECT 'John' AS [Name], 6 AS [Number] UNION ALL
SELECT 'Mike' AS [Name], 1 AS [Number] UNION ALL
SELECT 'Mike' AS [Name], 2 AS [Number] UNION ALL
SELECT 'Mike' AS [Name], NULL AS [Number] UNION ALL
SELECT 'Mike' AS [Name], 4 AS [Number] UNION ALL
SELECT 'Mike' AS [Name], 5 AS [Number] UNION ALL
SELECT 'Mike' AS [Name], 6 AS [Number]
<强>查询强>
;with gaps_between_numbers as (
select
t.Name, cnt = t.nextNum - t.Number - 1, dr = dense_rank() over (partition by t.Name order by t.Number)
, rn = row_number() over (partition by t.Name order by t.Number)
from (
select
Name, Number, nextNum = isnull(lead(Number) over (partition by Name order by number), Number + 1)
from
#myTable
where
Number is not null
) t
join master.dbo.spt_values v on t.nextNum - t.Number - 1 > v.number
where
t.nextNum - t.Number > 1
and v.type = 'P'
)
, ordering_nulls as (
select
t.Name, dr = isnull(q.dr, 2147483647)
from (
select
Name, rn = row_number() over (partition by Name order by (select 1))
from
#myTable
where
Number is null
) t
left join gaps_between_numbers q on t.Name = q.Name and t.rn = q.rn
)
, ordering_not_null_numbers as (
select
Name, Number, rn = dense_rank() over (partition by Name order by gr)
from (
select
Name, Number, gr = sum(lg) over (partition by Name order by Number)
from (
select
Name, Number, lg = iif(Number - lag(Number) over (partition by Name order by Number) = 1, 0, 1)
from
#myTable
where
Number is not null
) t
) t
)
select
Name, Number
, Desidered_col = row_number() over (partition by Name order by rn, isnull(Number, 2147483647))
from (
select * from ordering_not_null_numbers
union all
select Name, null, dr from ordering_nulls
) t
CTE gaps_between_numbers
正在寻找不连续的数字。当前行和下一行之间的Number
差异显示可以使用多少NULL值来填补空白。然后master.dbo.spt_values
用于将每一行乘以该数量。在gaps_between_numbers
dr
列中是间隙编号,cnt
是需要使用的NULL值的数量。
ordering_nulls
仅对NULL值进行排序,并与CTE gaps_between_numbers
联接,以了解每行应显示的位置。
ordering_not_null_numbers
命令非NULL值。连续数字将具有相同的行号
最后一步是联合CTE的ordering_not_null_numbers
和ordering_nulls
并进行所需的排序