【问题标题】:Oracle Convert Rows to ColumnsOracle 将行转换为列
【发布时间】:2012-01-28 05:23:42
【问题描述】:

我已经从我的查询中输入了数据输出

**Date**    **HIGH**  **LOW**       **IMAGE**      **TYPE**
1/28/2012     69         42          1.jpg           SUN
1/29/2012     70         42          2.jpg           MSUN

我想将此输出转换为

**1/28/2012**       **1/29/2012**
1.jpg                    2.jpg    
Sun                       MSUN
69                         72  
42                         42

这是我的查询

SELECT 
   W_DATE,HIGH, LOW, 
   W_TYPE, IMAGE
FROM WEATHER
ORDER BY W_DATE ASC

而且我在行中有多个日期,我只想显示 4 个日期,并且它们应该在系统日期更改时更改

【问题讨论】:

  • 您是在问如何使 Oracle 的实际结果看起来像这样?或者如何从 Oracle 获取结果集并在您自己的程序输出(例如 HTML 页面)中以不同方式显示?
  • 是的,我想在 Gridview 控件的 aspx 页面上显示图像
  • 您应该在问题中提及这一点,以便清楚您在问什么。 (我在刚才的问题中加了一个asp.net标签。)
  • 对不起,我该怎么做

标签: asp.net sql oracle


【解决方案1】:

关于如何在 oracle 中从行到列的所有可能性,您可以在此处阅读:

http://www.dba-oracle.com/t_converting_rows_columns.htm

从数据库的角度来看,我没有看到直接的解决方案 - 建议在应用程序端进行格式化,否则它可能看起来像这样蹩脚:

SELECT
   to_char(w1.w_Date,'MM/DD/YYYY'), to_char(w2.w_Date,'MM/DD/YYYY'), 
   to_char(w3.w_Date,'MM/DD/YYYY'), to_char(w4.w_Date,'MM/DD/YYYY')
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   w1.image,  w2.image, w3.image , w4.image
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   w1.w_type,  w2.w_type, w3.w_type , w4.w_type
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   to_char(w1.high),  to_char(w2.high), to_char(w3.high) , to_char(w4.high)
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4
UNION ALL
SELECT
   to_char(w1.low),  to_char(w2.low), to_char(w3.low) , to_char(w4.low)
FROM 
 (select * from weather where w_date = trunc(sysdate)) w1,
 (select * from weather where w_date = trunc(sysdate) + 1) w2,
 (select * from weather where w_date = trunc(sysdate) + 2) w3,
 (select * from weather where w_date = trunc(sysdate) + 3) w4;
/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 2019-10-14
  • 1970-01-01
相关资源
最近更新 更多