- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有下表(输出已经排序并分开以便理解):
| PK | FK1 | FK2 | ActionCode | CreationTS | SomeAttributeValue |
+----+-----+-----+--------------+---------------------+--------------------+
| 6 | 100 | 500 | Create | 2011-01-02 00:00:00 | H |
----------------------------------------------------------------------------
| 3 | 100 | 500 | Change | 2011-01-01 02:00:00 | Z |
| 2 | 100 | 500 | Change | 2011-01-01 01:00:00 | X |
| 1 | 100 | 500 | Create | 2011-01-01 00:00:00 | Y |
----------------------------------------------------------------------------
| 4 | 100 | 510 | Create | 2011-01-01 00:30:00 | T |
----------------------------------------------------------------------------
| 5 | 100 | 520 | CreateSystem | 2011-01-01 00:30:00 | A |
----------------------------------------------------------------------------
什么是ActionCode
?我们在 c#
中使用它,它代表一个枚举值
我想达到什么目的?
好吧,我需要以下输出:
| FK1 | FK2 | ActionCode | SomeAttributeValue |
+-----+-----+--------------+--------------------+
| 100 | 500 | Create | H |
| 100 | 500 | Create | Z |
| 100 | 510 | Create | T |
| 100 | 520 | CreateSystem | A |
-------------------------------------------------
那么,实际的逻辑是什么?我们有一些复合键(FK1 + FK2)的逻辑组。这些组中的每一个都可以分成多个分区,这些分区以 Create
或 CreateSystem
开头。每个分区都以 Create
、CreateSystem
或 Change
结尾。每个分区的 SomeAttributeValue
的实际值应该是分区最后一行的值。
不可能有以下数据池:
| PK | FK1 | FK2 | ActionCode | CreationTS | SomeAttributeValue |
+----+-----+-----+--------------+---------------------+--------------------+
| 7 | 100 | 500 | Change | 2011-01-02 02:00:00 | Z |
| 6 | 100 | 500 | Create | 2011-01-02 00:00:00 | H |
| 2 | 100 | 500 | Change | 2011-01-01 01:00:00 | X |
| 1 | 100 | 500 | Create | 2011-01-01 00:00:00 | Y |
----------------------------------------------------------------------------
然后期望 PK 7 影响 PK 2 或 PK 6 影响 PK 1。
我什至不知道如何/从哪里开始……我怎样才能做到这一点?我们在 mssql 2005+ 上运行
编辑:
有 a dump可用:
Create
,5是Change
,3必须忽略)Create
(分组分区的第一行)Create
(这将是一个“one-line-partition-in-group”/与上一个相同) 或 Change
(组中分区的最后一行)最佳答案
我假设每个分区只能包含一个单个 Create 或CreateSystem,否则您的要求定义不明确。以下是未经测试的,因为我没有示例表,也没有易于使用的格式的示例数据:
;With Partitions as (
Select
t1.FK1,
t1.FK2,
t1.CreationTS as StartTS,
t2.CreationTS as EndTS
From
Table t1
left join
Table t2
on
t1.FK1 = t2.FK1 and
t1.FK2 = t2.FK2 and
t1.CreationTS < t2.CreationTS and
t2.ActionCode in ('Create','CreateSystem')
left join
Table t3
on
t1.FK1 = t3.FK1 and
t1.FK2 = t3.FK2 and
t1.CreationTS < t3.CreationTS and
t3.CreationTS < t2.CreationTS and
t3.ActionCode in ('Create','CreateSystem')
where
t1.ActionCode in ('Create','CreateSystem') and
t3.FK1 is null
), PartitionRows as (
SELECT
t1.FK1,
t1.FK2,
t1.ActionCode,
t2.SomeAttributeValue,
ROW_NUMBER() OVER (PARTITION_FRAGMENT_ID BY t1.FK1,T1.FK2,t1.StartTS ORDER BY t2.CreationTS desc) as rn
from
Partitions t1
inner join
Table t2
on
t1.FK1 = t2.FK1 and
t1.FK2 = t2.FK2 and
t1.StartTS <= t2.CreationTS and
(t2.CreationTS < t1.EndTS or t1.EndTS is null)
)
select * from PartitionRows where rn = 1
(请注意,我在这里使用了各种保留名称)
基本逻辑是:Partitions CTE用于根据FK1、FK2、包含开始时间戳和排他结束时间戳来定义每个分区。它通过对基表的三重连接来实现这一点。来自 t2
的行被选择出现在来自 t1
的行之后,然后是 t3
中的行被选择出现在来自 t1
的匹配行之间和 t2
.然后,在 WHERE 子句中,我们从结果集中排除来自 t3
的匹配项的任何行。 - 结果是来自 t1
的行和来自 t2
的行代表两个相邻分区的开始。
然后第二个 CTE 从 Table
中检索所有行对于每个分区,但分配一个 ROW_NUMBER()
每个分区内的分数,基于 CreationTS
, 降序排列,结果为 ROW_NUMBER()
每个分区中的 1 是最后一行。
最后,在选择中,我们选择在各自分区中最后出现的那些行。
这都假设 CreationTS
每个分区中的值都是不同的。如果该假设不成立,我也可以使用 PK 对其进行重新处理。
关于sql-server - 如何为我的数据池正确使用 Row_Number()(分区),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277511/
我正在使用 postgres 9.1 构建分页记分牌。 用户可以根据多个标准对记分板进行排序,他们可以按升序或降序排序。有一个功能可以让用户在记分牌的多个页面中找到“他们的行”,它必须反射(refle
我在练习中遇到了这个问题,我无法修复它,这让我发疯了......基本上我使用的是 SQLLite,所以我只能使用任何窗口函数来获得结果并且更痛苦的部分是已经创建的架构上没有唯一的 id。参见示例: *
语法: ROW_NUMBER() OVER([ <partition_by_clause>] <order_by_clause>) 备注: 
更新:事实上,请留下以下复杂的查询,请检查此查询。它说 Fetch 为 98%,而 Row_Number 中为 2%? Fetch 是 SQL Server 2012 的另一个营销关键字吗? ----
我需要使用以下查询向用户事件添加计数器: select PERSON_ID, TIMESTAMP, row_number() over (partition by PERSON_ID order
我有一张 table : Trip Stop Time ----------------- 1 A 1:10 1 B 1:16 1 B 1:20 1
我有以下存储过程返回 A , B , 并按降序计数。我正在尝试使用 ROW_NUMBER ,所以我可以分页记录,但我想要第一行号 1成为计数最高的记录,所以基本上,如果我返回一个包含 3 条记录的表并
在我的 sql 案例中,我在计算 row_number 的最大值时遇到了麻烦。 我将直接在 SQL Fiddle 示例上进行解释,因为我认为理解起来会更快:SQL Fiddle “OrderNumbe
我有这个运行良好的 SQL。 希望我的过滤器返回具有最高 UserSessionSequenceID 的最新唯一 SessionGuid。 问题是性能很差——即使我有很好的索引。 我怎样才能重写这个
希望有人可以在这里为我指明正确的方向。 请参阅下面的查询。 SELECT day_id,month_id,time_id, row_number() over (partition BY month_
请问函数row_number()总是以相同的方式对相同的数据进行排序? 最佳答案 不可以。SQL 中的排序不稳定,这意味着不会保留原始排序顺序。不能保证解析函数或 order by将为相同的键值以相同
我有一个用例,我需要在 PARTITION 上使用 ROW_NUMBER(): 就像是: SELECT Column1 , Column 2 ROW_NUMBER() OVER ( P
UPD:谢谢大家,话题结束, sleep 后我明白了一切=) 我在理解 OVER 子句和 ROW_NUMBER 函数时遇到问题。简单的表 - 名称和标记。我想计算每个名字的平均分数。 SELECT t
我有一个包含零的列和一个保留顺序的列,其中零表示数据片段之间的中断。这是例子 A Ord 1 1 1 2 0 3 0 4 0 5 1 6 1 7 1 8 0 9 1 10 我想要获得的是同一列,其中零
我有这个简单的查询,希望它是不言自明的。 SELECT ROW_NUMBER() OVER (PARTITION BY Price_Id ORDER BY date DESC) r, * FROM
请考虑此查询: SELECT num, * FROM ( SELECT OrderID, CustomerID, EmployeeID, OrderDate, Required
首先,我想提一下,我已经检查了所有其他提出的问题,没有一个与我的相似,所以我不认为它是重复的。 我有两个表格,“Article_tbl”到目前为止已超过 300,000 行,“ArticleZone_
我有一个非常简单的问题,但我正在使用 appInsights,并且似乎无法弄清楚如何在 aiql 或任何函数中复制 row_number() 函数(没有分区意义)。 我有综合浏览量表,我按 sessi
在 SQL 2005/2008 数据库中,我们有表 BatchMaster。列:RecordId bigint - 自动增量 id,BatchNumber bigint - 唯一非聚集索引,Batch
我正在搜索删除表中的重复条目,我看到了如下示例: CREATE TABLE Suppliers ( Id int identity (1,1), CompanyTitle nvarchar(1
我是一名优秀的程序员,十分优秀!