“崩溃”表消除空值
我有一个允许在 GUI 中创建自定义字段的产品。我创建了三个字段:系统发货日期、系统保修结束日期和系统保修说明。遗憾的是,系统将它们填充为行,而不是列。
示例:
Machine_GroupID | agentGuid | fieldName | fieldValue
-----------------------------------------------------------------------------
systemABC.companyABC | 57537921 | System Ship Date | 1/1/1900
systemABC.companyABC | 57537921 | System Warranty End Date | 1/1/1950
systemABC.companyABC | 57537921 | System Warranty Notes | Test notes
system123.companyABC | 48394844 | System Ship Date | 1/1/1900
system123.companyABC | 48394844 | System Warranty End Date | 1/1/1950
system123.companyABC | 48394844 | System Warranty Notes | Test notes
systemXYZ.companyABC | 99948403 | System Ship Date | 1/1/1900
systemXYZ.companyABC | 99948403 | System Warranty End Date | 1/1/1950
systemXYZ.companyABC | 99948403 | System Warranty Notes | Test notes
我需要将系统发货日期作为一列返回,将系统保修结束日期作为一列返回,将系统保修注释作为一列返回。如果我使用 CASE WHEN 语句:
CASE WHEN sim.fieldName = 'System Ship Date'
THEN sim.fieldValue END AS 'System Ship Date',
CASE WHEN sim.fieldName = 'System Warranty End Date'
THEN sim.fieldValue END AS 'System Warranty End Date',
CASE WHEN sim.fieldName = 'System Warranty Notes'
THEN sim.fieldValue END AS 'System Warranty Notes'
数据将按每台计算机显示:
header1 header2 header3
数据1 NULL NULL
NULL 数据2 NULL
NULL NULL data3
我需要它看起来像这样:
header1 header2 header3
data1 data2 data3
我以为我可以以某种方式使用 MAX() 但我是新手,这超出了我的能力范围。或者,我知道我可以创建一个临时表,但我也不知道。我在网上和堆栈上进行了广泛的搜索,但据我所知,这是 CASE WHEN 语句。
I have a product that allows creation of custom fields in the GUI. I created three fields: System Ship Date, System Warranty End Date, and System Warranty Notes. Sadly, the system populates these as rows, not columns.
Example:
Machine_GroupID | agentGuid | fieldName | fieldValue
-----------------------------------------------------------------------------
systemABC.companyABC | 57537921 | System Ship Date | 1/1/1900
systemABC.companyABC | 57537921 | System Warranty End Date | 1/1/1950
systemABC.companyABC | 57537921 | System Warranty Notes | Test notes
system123.companyABC | 48394844 | System Ship Date | 1/1/1900
system123.companyABC | 48394844 | System Warranty End Date | 1/1/1950
system123.companyABC | 48394844 | System Warranty Notes | Test notes
systemXYZ.companyABC | 99948403 | System Ship Date | 1/1/1900
systemXYZ.companyABC | 99948403 | System Warranty End Date | 1/1/1950
systemXYZ.companyABC | 99948403 | System Warranty Notes | Test notes
I need to return System Ship Date as a column, System Warranty End Date as a column, and System Warranty Notes as a column. If I use a CASE WHEN statement:
CASE WHEN sim.fieldName = 'System Ship Date'
THEN sim.fieldValue END AS 'System Ship Date',
CASE WHEN sim.fieldName = 'System Warranty End Date'
THEN sim.fieldValue END AS 'System Warranty End Date',
CASE WHEN sim.fieldName = 'System Warranty Notes'
THEN sim.fieldValue END AS 'System Warranty Notes'
The data appears thusly on a per-computer basis:
header1 header2 header3
data1 NULL NULL
NULL data2 NULL
NULL NULL data3
I need it to appear like this:
header1 header2 header3
data1 data2 data3
I thought I could somehow use MAX() but I'm a novice and it's beyond me. Alternatively I know I could create a temporary table but I don't know that either. I've searched extensively online and on stack, but that's as far as I could get is the CASE WHEN statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不自加入?还是简单点吧...
Why not SELF JOIN? Keep it simpler still...
在 MS SQL 中:
在您的情况下,
GROUP BY
子句并不是绝对必要的,但如果没有它,PIVOT
有时会返回无关的行,就像您的第一个表一样。In MS SQL:
The
GROUP BY
clause isn't strictly necessary in your case, but without itPIVOT
sometimes returns extraneous rows, rather like your first table.