将 /转置列转换为行,仅显示修改的值
我有一张名为“ at_order_info”的表名称名称和值,如下所示: -
ID | Orderno | ordertype | Engineername | 评论 | 设施 | 部门 |
---|---|---|---|---|---|---|
101 | OD 1 | 5 | Tom | Fitting | C1P1 | 组件 |
102 | OD 1 | 5 | Jack | Red | C1P1 | 绘画 |
我想显示数据更改,我想显示数据更改2个记录。
需要一个SQL查询,该查询将返回以下输出: -
属性 | OldValue | NewValue |
---|---|---|
ID | 101 | 101 |
Engineername | tom | Jack |
评论 | 拟合 | 红色 |
部门 | 组装 | 绘画 |
要求: -
输出应仅显示两个行中价值变化的列。 就像我的情况下一样(orderno,ordertype,设施)列在两个行中都有相同的值,因此在输出中不需要它。 (ID,Engineername,评论,部门)具有不同的值,因此需要它们在输出中。
现有列名称应像我们看到的那样成为行值(ID,Engineername,评论,部门)。
输出查询应该有3个自定义列名称,例如属性,旧版,newValue。
属性 - 包含列名。
oldValue-相应列的值id = 101。
newValue-相应列的值ID = 102。
基本上,我想显示2个记录之间发生的数据更改。
I have a table named "AT_ORDER_INFO" with columns names and values as shown below :-
ID | OrderNo | OrderType | EngineerName | Comments | Facility | Department |
---|---|---|---|---|---|---|
101 | OD 1 | 5 | Tom | Fitting | C1P1 | Assembly |
102 | OD 1 | 5 | Jack | Red | C1P1 | Painting |
I want to display the data changes occurred between 2 records.
Need an SQL Query which will return the below output :-
Property | OldValue | NewValue |
---|---|---|
ID | 101 | 102 |
EngineerName | Tom | Jack |
Comments | Fitting | Red |
Department | Assembly | Painting |
Requirements :-
Output should display only the columns for which there is change in value in both the rows.
Like in my case (OrderNo,OrderType,Facility) columns have same values in both the rows so dont need this in output. (ID,EngineerName,Comments,Department) have different values so need them in output.Existing Columns names should become the Row Values like we see (ID,EngineerName,Comments,Department).
Output Query Should Have 3 Custom Columns names like Property, OldValue, NewValue.
Property - Contains the Column Names.
OldValue - Value of corresponding columns value of ID = 101.
NewValue - Value of corresponding columns value of ID = 102.
Basically, I want to display the data changes occurred between 2 records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数据
首先应在组中使用
String_agg
在中使用,以确保订单
然后您应该在
subquery 和
undivot
下一步是使用拆分列的方法,我使用了
substring
如果值更多地考虑
创建一个fucntion,将字符串分为列
,则可以通过处理动态ID。dbfiddle
your data
first you should use
String_agg
WITHWITHIN GROUP
to ensure the orderThen you should use this in
Subquery
andUnpivot
The next step is using methods of splitting column, I have used
Substring
if values are much more consider
creating a fucntion that split string into columns
, by doing so it is possible to handle Dynamic IDs.dbfiddle