有人可以告诉我下面的oracle查询有什么问题吗? 我试图将基于块编号和日期作为常见条件的单列数据导入5个不同的列
提前致谢
select WBD_DATE, WBD_BLOCK_NO, t1.WBD_VALUE, t2.WBD_VALUE, t3.WBD_VALUE,
t4.WBD_VALUE, t5.WBD_VALUE
from WB_BLOCK_DATA,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO
from WB_BLOCK_DATA
where WBD_ATTRIBUTE_ID = '2692583'
and trim(to_char(WBD_DATE, 'Month')) = 'January'
and to_char(WBD_DATE, 'YYYY')='2017') as t1,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO
from WB_BLOCK_DATA
where WBD_ATTRIBUTE_ID = '2692599'
and trim(to_char(WBD_DATE, 'Month')) = 'January'
and to_char(WBD_DATE, 'YYYY')='2017') as t2,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO
from WB_BLOCK_DATA
where WBD_ATTRIBUTE_ID = '2692607'
and trim(to_char(WBD_DATE, 'Month')) = 'January'
and to_char(WBD_DATE, 'YYYY')='2017') as t3,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO
from WB_BLOCK_DATA
where WBD_ATTRIBUTE_ID = '2692615'
and trim(to_char(WBD_DATE, 'Month')) = 'January'
and to_char(WBD_DATE, 'YYYY')='2017') as t4,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO
from WB_BLOCK_DATA
where WBD_ATTRIBUTE_ID = '2692623'
and trim(to_char(WBD_DATE, 'Month')) = 'January'
and to_char(WBD_DATE, 'YYYY')='2017') as t5
where WBD_DATE = t1.WBD_DATE
and WBD_DATE = t2.WBD_DATE
and WBD_DATE = t3.WBD_DATE
and WBD_DATE = t4.WBD_DATE
and WBD_DATE = t5.WBD_DATE
and WBD_BLOCK_NO = t1.WBD_BLOCK_NO
and WBD_BLOCK_NO = t2.WBD_BLOCK_NO
and WBD_BLOCK_NO = t3.WBD_BLOCK_NO
and WBD_BLOCK_NO = t4.WBD_BLOCK_NO
and WBD_BLOCK_NO = t5.WBD_BLOCK_NO
答案 0 :(得分:1)
AS
,但仅适用于列;此外,您甚至需要为WB_BLOCK_DATA
使用别名,或将其列引用为WB_BLOCK_DATA.columnName
。
select t0.WBD_DATE, t0.WBD_BLOCK_NO, t1.WBD_VALUE, t2.WBD_VALUE, t3.WBD_VALUE, t4.WBD_VALUE, t5.WBD_VALUE
from WB_BLOCK_DATA t0,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO from WB_BLOCK_DATA where WBD_ATTRIBUTE_ID = '2692583' and trim(to_char(WBD_DATE, 'Month'))='January' and to_char(WBD_DATE, 'YYYY')='2017') t1,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO from WB_BLOCK_DATA where WBD_ATTRIBUTE_ID = '2692599' and trim(to_char(WBD_DATE, 'Month'))='January' and to_char(WBD_DATE, 'YYYY')='2017') t2,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO from WB_BLOCK_DATA where WBD_ATTRIBUTE_ID = '2692607' and trim(to_char(WBD_DATE, 'Month'))='January' and to_char(WBD_DATE, 'YYYY')='2017') t3,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO from WB_BLOCK_DATA where WBD_ATTRIBUTE_ID = '2692615' and trim(to_char(WBD_DATE, 'Month'))='January' and to_char(WBD_DATE, 'YYYY')='2017') t4,
(select WBD_VALUE, WBD_DATE, WBD_BLOCK_NO from WB_BLOCK_DATA where WBD_ATTRIBUTE_ID = '2692623' and trim(to_char(WBD_DATE, 'Month'))='January' and to_char(WBD_DATE, 'YYYY')='2017') t5
where t0.WBD_DATE = t1.WBD_DATE
and t0.WBD_DATE = t2.WBD_DATE
and t0.WBD_DATE = t3.WBD_DATE
and t0.WBD_DATE = t4.WBD_DATE
and t0.WBD_DATE = t5.WBD_DATE
and t0.WBD_BLOCK_NO = t1.WBD_BLOCK_NO
and t0.WBD_BLOCK_NO = t2.WBD_BLOCK_NO
and t0.WBD_BLOCK_NO = t3.WBD_BLOCK_NO
and t0.WBD_BLOCK_NO = t4.WBD_BLOCK_NO
and t0.WBD_BLOCK_NO = t5.WBD_BLOCK_NO
示例:
SQL> select dummy from dual x;
D
-
X
SQL> select dummy from dual as x;
select dummy from dual as x
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
SQL> select dummy as d from dual x;
D
-
X
SQL>
答案 1 :(得分:1)
作为一个旁边(这实际上太长了,不能作为评论,这就是它的真实情况),你要做的就是称为枢轴操作。如果您使用的是Oracle 11.1或更高版本,则可以使用PIVOT运算符执行此操作:
WITH wb_block_data AS (SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692583' wbd_attribute_id, 10 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692599' wbd_attribute_id, 20 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692607' wbd_attribute_id, 30 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692615' wbd_attribute_id, 40 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692623' wbd_attribute_id, 50 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692671' wbd_attribute_id, 60 wbd_value FROM dual)
-- end of mimicking your table with some sample data in it. See SQL below:
SELECT *
FROM (SELECT *
FROM wb_block_data
WHERE wbd_attribute_id IN ('2692583', '2692599', '2692607', '2692615', '2692623')
and TRUNC(wbd_date, 'mm') = to_date('01/01/2017', 'dd/mm/yyyy'))
PIVOT (MAX(wbd_value) FOR (wbd_attribute_id) IN ('2692583' AS attrib_2692583,
'2692599' AS attrib_2692599,
'2692607' AS attrib_2692607,
'2692615' AS attrib_2692615,
'2692623' AS attrib_2692623));
WBD_DATE WBD_BLOCK_NO ATTRIB_2692583 ATTRIB_2692599 ATTRIB_2692607 ATTRIB_2692615 ATTRIB_2692623
----------- ------------ -------------- -------------- -------------- -------------- --------------
12/01/2017 1 10 20 30 40 50
如果您使用的是早期版本,则可以使用以下方法手动执行数据透视:
WITH wb_block_data AS (SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692583' wbd_attribute_id, 10 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692599' wbd_attribute_id, 20 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692607' wbd_attribute_id, 30 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692615' wbd_attribute_id, 40 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692623' wbd_attribute_id, 50 wbd_value FROM dual UNION ALL
SELECT to_date('12/01/2017', 'dd/mm/yyyy') wbd_date, 1 wbd_block_no, '2692671' wbd_attribute_id, 60 wbd_value FROM dual)
-- end of mimicking your table with some sample data in it. See SQL below:
SELECT wbd_date,
wbd_block_no,
MAX(CASE WHEN wbd_attribute_id = '2692583' THEN wbd_value END) attrib_2692583,
MAX(CASE WHEN wbd_attribute_id = '2692599' THEN wbd_value END) attrib_2692599,
MAX(CASE WHEN wbd_attribute_id = '2692607' THEN wbd_value END) attrib_2692607,
MAX(CASE WHEN wbd_attribute_id = '2692615' THEN wbd_value END) attrib_2692615,
MAX(CASE WHEN wbd_attribute_id = '2692623' THEN wbd_value END) attrib_2692623
FROM wb_block_data
WHERE wbd_attribute_id IN ('2692583', '2692599', '2692607', '2692615', '2692623')
and TRUNC(wbd_date, 'mm') = to_date('01/01/2017', 'dd/mm/yyyy')
GROUP BY wbd_date, wbd_block_no;
WBD_DATE WBD_BLOCK_NO ATTRIB_2692583 ATTRIB_2692599 ATTRIB_2692607 ATTRIB_2692615 ATTRIB_2692623
----------- ------------ -------------- -------------- -------------- -------------- --------------
12/01/2017 1 10 20 30 40 50
请注意,在这两种情况下,我都修改了谓词以将日期与日期进行比较,而不是将日期分成两个字符串然后进行比较。另请注意,我将wbd_date截断为月份级别(自动表示该月的第一天)。这也意味着如果你想使用一个索引,你只需要在一个元素上定义它:trunc(wbd_date, 'mm')
而不是两个元素:trim(to_char(WBD_DATE, 'Month'))
和to_char(WBD_DATE, 'YYYY')