如何在Oracle SQL中转置和枢转

发布于 2025-02-11 11:20:58 字数 189 浏览 2 评论 0原文

我正在尝试从表中翻转数据,这是一个屏幕截图,可以解释它的外观。 在左边,我们有数据库表,以及我想推出它的正确性

尝试使用枢轴和案例语句,但无法获得我需要的视图,请指导

i am trying to flip the data from a table , here is a screenshot to explain how it looks.
on the left we have database table and on right how i want to derive it

i have so far tried using pivot, and case statements but was not able to get the view i need, please guide

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

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

发布评论

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

评论(1

桃扇骨 2025-02-18 11:20:58

这通常是在演示层中执行的任务,而不是在数据库中执行的任务。


如果必须在SQL中进行操作,则可以使用:

SELECT *
FROM   shift_t
PIVOT (
  MAX(shift)
  FOR shift_date IN (
    DATE '2022-07-01' AS "07/01/2022",
    DATE '2022-07-02' AS "07/02/2022",
    DATE '2022-07-03' AS "07/03/2022",
    DATE '2022-07-04' AS "07/04/2022",
    DATE '2022-07-05' AS "07/05/2022",
    DATE '2022-07-06' AS "07/06/2022",
    DATE '2022-07-07' AS "07/07/2022",
    DATE '2022-07-08' AS "07/08/2022",
    DATE '2022-07-09' AS "07/09/2022"
  )
)

对于示例数据,输出:哪个:

位置name07/01/202207/02/202207/03/202207/04/202207/05/202207/06/202207/07/202207/08/202207/09/2022
hydrosecwwcccccw

注意:您必须对日期值进行核对编码。无法使用Pivot语句动态提供它们。如果需要动态枢轴,请查看这个问题(然后忽略它并在演示文稿中执行任务,不在sql)

。 >

This is typically a task that is performed in the presentation-layer and not in the database.


If you must do it in SQL then you can use:

SELECT *
FROM   shift_t
PIVOT (
  MAX(shift)
  FOR shift_date IN (
    DATE '2022-07-01' AS "07/01/2022",
    DATE '2022-07-02' AS "07/02/2022",
    DATE '2022-07-03' AS "07/03/2022",
    DATE '2022-07-04' AS "07/04/2022",
    DATE '2022-07-05' AS "07/05/2022",
    DATE '2022-07-06' AS "07/06/2022",
    DATE '2022-07-07' AS "07/07/2022",
    DATE '2022-07-08' AS "07/08/2022",
    DATE '2022-07-09' AS "07/09/2022"
  )
)

Which, for the sample data, outputs:

LOCATIONNAME07/01/202207/02/202207/03/202207/04/202207/05/202207/06/202207/07/202207/08/202207/09/2022
HYDRoseCWWCCCCCW

Note: You must hard-code the date values. They cannot be provided dynamically using a PIVOT statement. If you want a dynamic pivot then look at this question (and then ignore it and perform the task in the presentation-layer and not in SQL).

db<>fiddle here

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