类似于 rows-to-col(EAV 到关系型) - 请使用 SQL 查询

发布于 2024-12-21 02:54:50 字数 392 浏览 1 评论 0原文

我的数据集并不完全像 EAV 格式,但有些相似;这是数据:

original data

我需要的格式如下:

original data sstatic.net/Pq92P.png" alt="需要数据格式">

对于每个 EN_NO 组,我需要上述格式的数据。如果组为 EN_NO > 1 那么相应的产品密钥应该进入相应的产品列,否则不会(例如 EN_NO 4 和 5)。

我希望我说清楚了。数据位于 Qracle 表中,请建议查询以获取我需要的格式的数据。

谢谢, 普拉卡什

My dataset is not exactly like EAV format, but it's somewhat similar; here's the data:

original data

In the format I need is as follows:

data format needed

For every EN_NO group I need the data in above format. If group on EN_NO > 1 then respestive product key should go to respestive product column otherwise not (for e.g. EN_NO 4 and 5).

I hope I am clear. Data is in a Qracle table, please suggest a query to get the data in the format I need.

Thanks,
Prakash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

兔姬 2024-12-28 02:54:50

我强烈建议更改您的表结构。目前,您有两条信息绑定到一个字段中。这是一种 SQL 反模式,会破坏 Oracle 使用某些优化的能力。

相反,请考虑将“PROD_KEY”拆分为两个字段(PRODUCT_TYPE = Prod_A 等)(SUB_PRODUCT_ID = 1、2、3 等)。或者,为了减少数据库中的潜在更改,只需将 PRODUCT_TYPE 添加到当前表中即可。

也就是说,使用您当前的结构...

SELECT
  EN_NO,
  PROD_KEY,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_A') THEN PROD_KEY ELSE NULL END AS Prod_A,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_B') THEN PROD_KEY ELSE NULL END AS Prod_B,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_C') THEN PROD_KEY ELSE NULL END AS Prod_C,
  PROD_QTY
FROM
  yourTable

当您明确知道需要哪些列作为输出时,这才有效。如果你需要代码适应有Prod_D等,那么你需要编写写代码的代码(动态SQL)。

I would highly recommend altering your table structure. At present you have two pieces of information tied into a single field. This is a SQL Anti-Pattern and destroys the ability of Oracle to user certain optimisations.

Instead, please consider splitting "PROD_KEY" into two fields (PRODUCT_TYPE = Prod_A, etc) (SUB_PRODUCT_ID = 1, 2, 3, etc). Or, to cause less potential change across the database, simply add the PRODUCT_TYPE to your current table.

That said, using your current structure...

SELECT
  EN_NO,
  PROD_KEY,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_A') THEN PROD_KEY ELSE NULL END AS Prod_A,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_B') THEN PROD_KEY ELSE NULL END AS Prod_B,
  CASE WHEN (EN_NO < 4) AND (LEFT(PROD_KEY, 6) = 'Prod_C') THEN PROD_KEY ELSE NULL END AS Prod_C,
  PROD_QTY
FROM
  yourTable

This works when you know specifically what columns you need as output. If you need the code to adapt to having Prod_D, etc, then you need to write code that writes code (Dynamic SQL).

与他有关 2024-12-28 02:54:50

您显示的是一个数据透视表。如果您希望查询能够自动添加与表中不同数据值相对应的列,那么您就不走运了。 SQL不支持这个;在准备查询时,在读取表中的任何数据值之前,必须知道并固定列。

为此,您需要获取不同 prod_key 值的列表(或 : 字符之前的子字符串):

SELECT DISTINCT LEFT(PROD_KEY, 6) FROM yourTable;

然后编写应用程序代码以将此值列表转换为一系列列表达式正如 @Dems 提到的,动态 SQL 语句。

另一种选择是获取表中存在的原始数据,然后编写应用程序代码对其进行迭代,并将其分组到表格报告中,一次一个数据值。

无论哪种方式,您都需要编写应用程序代码,除非您已经知道要获取的不同 prod_key 类型的列表。

What you're showing is a pivot table. If you want a query that will automatically add columns corresponding to the distinct data values in your table, then you're out of luck. SQL does not support this; columns must be known and fixed at the time you prepare the query -- before it reads any data values in the table.

To do this, you need to get a list of distinct prod_key values (or the substring up to the : character):

SELECT DISTINCT LEFT(PROD_KEY, 6) FROM yourTable;

And then write application code to turn this list of values into a series of column expressions in a dynamic SQL statement, as @Dems mentions.

The other option is to fetch the raw data as it exists in the table, then write application code that iterates over it and groups it into a tabular report one data value at a time.

Either way, you need to write application code unless you already know the list of distinct prod_key types you want to fetch.

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