“崩溃”表消除空值

发布于 2024-11-09 02:03:30 字数 1702 浏览 0 评论 0原文

我有一个允许在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2024-11-16 02:03:30

为什么不自加入?还是简单点吧...

SELECT
   s1.Machine_GroupID, s1.agentGuid,
   s1.fieldValue, s2.fieldValue, s3.fieldValue
FROM
   sim s1
   JOIN sim s2 ON s1.Machine_GroupID = s2.Machine_GroupID
   JOIN sim s3 ON s1.Machine_GroupID = s3. Machine_GroupID
WHERE
   s1.fieldName = 'System Ship Date' AND
   s2.fieldName = 'System Warranty End Date' AND
   s2.fieldName = 'System Warranty Notes'

Why not SELF JOIN? Keep it simpler still...

SELECT
   s1.Machine_GroupID, s1.agentGuid,
   s1.fieldValue, s2.fieldValue, s3.fieldValue
FROM
   sim s1
   JOIN sim s2 ON s1.Machine_GroupID = s2.Machine_GroupID
   JOIN sim s3 ON s1.Machine_GroupID = s3. Machine_GroupID
WHERE
   s1.fieldName = 'System Ship Date' AND
   s2.fieldName = 'System Warranty End Date' AND
   s2.fieldName = 'System Warranty Notes'
書生途 2024-11-16 02:03:30

在 MS SQL 中:

SELECT
  Machine_GroupID, agentGuid, "System Ship Date", "System Warranty End Date", "System Warranty Notes"
FROM
  (
  SELECT
    Machine_GroupID, agentGuid, fieldName, MAX(fieldValue) AS Value
  FROM
    sim
  GROUP BY
    Machine_GroupID, agentGuid, fieldName
  ) AS X
PIVOT
  (MAX(Value) FOR fieldName IN ("System Ship Date", "System Warranty End Date", "System Warranty Notes")) AS Y

在您的情况下,GROUP BY 子句并不是绝对必要的,但如果没有它,PIVOT 有时会返回无关的行,就像您的第一个表一样。

In MS SQL:

SELECT
  Machine_GroupID, agentGuid, "System Ship Date", "System Warranty End Date", "System Warranty Notes"
FROM
  (
  SELECT
    Machine_GroupID, agentGuid, fieldName, MAX(fieldValue) AS Value
  FROM
    sim
  GROUP BY
    Machine_GroupID, agentGuid, fieldName
  ) AS X
PIVOT
  (MAX(Value) FOR fieldName IN ("System Ship Date", "System Warranty End Date", "System Warranty Notes")) AS Y

The GROUP BY clause isn't strictly necessary in your case, but without it PIVOT sometimes returns extraneous rows, rather like your first table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文