Select distinct current and previous columns from a sql table(从 sql 表中选择不同的当前和以前的列)
问题描述
我有一张这样的桌子
Id |Name |Status | Rate | Method |ModifiedTime |ModifiedBy
-----------------------------------------------------------------------------
1 |Recipe1 | 0 | 30 | xyz | 2016-07-26 14:55:57.977 | A
-------------------------------------------------------------------------------
2 |Recipe1 | 0 | 30 | abc | 2016-07-26 14:56:18.123 | A
--------------------------------------------------------------------------------
3 |Recipe1 | 1 | 30 | xyz | 2016-07-26 14:57:50.180 | b
我想只选择更改,并想显示以前的值是什么以及它当前伴随着什么以及谁更改了它.最终结果如下.我正在使用 SQL Server 2014.
I would like to select only the changes and wanted to show what the value was previously and what it is currently accompanied by who changed it. The final outcome will be as follows. I am using SQL Server 2014.
Item | Before | After |ModifiedTime | ModifiedBy
-----------------------------------------------------------------------------
Method | xyz | Abc | 2016-07-26 14:56:18.123 | A
-------------------------------------------------------------------------------
Status | 0 | 1 | 2016-07-26 14:57:50.180 | b
--------------------------------------------------------------------------------
Method | Abc | xyz | 2016-07-26 14:57:50.180 | b
推荐答案
没有动态 SQL 这是我能做的最好的.我假设您按 Name 对更改进行分组.如果 Name 可以更改,则应将其从分区中删除并添加到项目子查询和 case 中.你可以试试这里
without dynamic SQL this is the best I could do.
I'm assuming that you are grouping changes by Name. If Name can change it should be removed of the partition by and added to the items subquery and to the case. You can try it here
select item,
case item
when 'Status' then cast(prevStatus as varchar)
when 'Rate' then cast(prevRate as varchar)
when 'Method' then prevMethod
end as Before,
case item
when 'Status' then cast(Status as varchar)
when 'Rate' then cast(Rate as varchar)
when 'Method' then Method
end as After,
ModifiedTime,
ModifiedBy
from (
select Status,
lag(Status) over (partition by Name order by id) prevStatus,
Rate,
lag(Rate) over (partition by Name order by id) prevRate,
Method,
lag(Method) over (partition by Name order by id) prevMethod,
ModifiedBy,
ModifiedTime
from t ) as t1 cross join (select 'Status' as item union all
select 'Rate' as item union all
select 'Method' as item) items
where (item = 'Status' and Status <> prevStatus)
or (item = 'Rate' and Rate <> prevRate)
or (item = 'Method' and Method <> prevMethod)
order by ModifiedTime
输出
item Before After ModifiedTime ModifiedBy
------ ------ ----- ----------------------- ----------
Method xyz abc 2016-07-26 14:56:18.123 A
Status 0 1 2016-07-26 14:57:50.180 b
Method abc xyz 2016-07-26 14:57:50.180 b
在第二行,您会看到 ModifiedBy 是 b,而您有 A.我认为是一个错字.
On the second row you'll see that ModifiedBy is b while you have A. I think is a typo.
这篇关于从 sql 表中选择不同的当前和以前的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 sql 表中选择不同的当前和以前的列
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
