我正在尝试运行以下查询。我从参数
传递日期SELECT to_char(TIMESTAMP, 'HH24') * 1 TIMESTAMP
,sum(TwoG) TwoG
,sum(ThreeG) ThreeG
,sum(FourG) FourG
FROM (
SELECT *
FROM T_CA_Cellsdown
UNION ALL
SELECT *
FROM T_CA_Cellsdown_TFVF
)
WHERE trunc(TIMESTAMP) = trunc(to_date('03-Apr-16', 'dd-Mon-yy'))
AND Type = 'Network'
GROUP BY to_char(TIMESTAMP, 'HH24') * 1
ORDER BY to_char(TIMESTAMP, 'HH24') * 1
查询错误:ORA-01481:数字格式模型无效
以下查询有效。我只是想将sysdate
更改为日期字符串,我通过php中的用户输入作为参数传递。为什么它乘以1我不知道,这是一个有效的查询并提供所需的结果。
SELECT to_char(TIMESTAMP, 'HH24') * 1
,sum(TwoG) TwoG
,sum(ThreeG) ThreeG
,sum(FourG) FourG
FROM (
SELECT *
FROM T_CA_Cellsdown
UNION ALL
SELECT *
FROM T_CA_Cellsdown_TFVF
)
WHERE trunc(TIMESTAMP) = trunc(sysdate-1)
AND Type = 'Network'
GROUP BY to_char(TIMESTAMP, 'HH24') * 1
ORDER BY to_char(TIMESTAMP, 'HH24') * 1
答案 0 :(得分:2)
问题是最后一行:
ORDER BY to_char(TIMESTAMP, 'HH24') * 1
在order-by子句中,它将TIMESTAMP视为选择列表中的列别名,而不是表中的原始TIMESTAMP;所以它包含一个数字,而不是时间戳。
因此,如果你有一个时间戳,其中小时值为13,比如说,你正在做
ORDER BY to_char(13, 'HH24') * 1`
...并且第一个参数是一个数字,您使用的是TO_CHAR()
的数字版本,而HH24
并不是一个有效的格式模型。这是错误信息所说的内容。
你可以这样做:
ORDER BY TIMESTAMP;
要使用原始模式,您需要为内联视图提供表别名,然后在order-by中使用该别名来限定TIMESTAMP。默认情况下,它使用列别名版本。
答案 1 :(得分:0)
order by
最后执行。在执行order by
时,您将在select语句中派生一个新列timestamp
。要克服它,只需按顺序使用timestamp
SELECT to_char(TIMESTAMP, 'HH24') * 1 TIMESTAMP
,sum(TwoG) TwoG
,sum(ThreeG) ThreeG
,sum(FourG) FourG
FROM (
SELECT *
FROM T_CA_Cellsdown
UNION ALL
SELECT *
FROM T_CA_Cellsdown_TFVF
)
WHERE trunc(TIMESTAMP) = trunc(to_date('03-APR-16', 'dd-Mon-yy'))
AND Type = 'Network'
GROUP BY to_char(TIMESTAMP, 'HH24') * 1
ORDER BY TIMESTAMP -- TIMESTAMP here refers to derived column at line 1
或者使用此
SELECT to_char(TIMESTAMP, 'HH24') * 1 TIMESTAMP1 -- Or use different alias
,sum(TwoG) TwoG
,sum(ThreeG) ThreeG
,sum(FourG) FourG
FROM (
SELECT *
FROM T_CA_Cellsdown
UNION ALL
SELECT *
FROM T_CA_Cellsdown_TFVF
)
WHERE trunc(TIMESTAMP) = trunc(to_date('03-APR-16', 'dd-Mon-yy'))
AND Type = 'Network'
GROUP BY to_char(TIMESTAMP, 'HH24') * 1
ORDER BY to_char(TIMESTAMP, 'HH24') * 1
我在这里复制了这个问题。 - 这不起作用。
select to_char(TIMESTAMP, 'HH24') * 1 TIMESTAMP,sum(col1) from
(
select sysdate-1 as TIMESTAMP,1 as col1 from dual
union all
select sysdate-1 as TIMESTAMP,2 as col1 from dual
)
where trunc(sysdate)=trunc(to_date('04-APR-16','Dd-Mon-YY'))
group by to_char(TIMESTAMP, 'HH24') * 1
order by to_char(TIMESTAMP, 'HH24') * 1
但是这句话会奏效。
select to_char(TIMESTAMP, 'HH24') * 1 TIMESTAMP1,sum(col1) from (
select sysdate-1 as TIMESTAMP,1 as col1 from dual
union all
select sysdate-1 as TIMESTAMP,2 as col1 from dual
)
where trunc(sysdate)=trunc(to_date('04-APR-16','Dd-Mon-YY'))
group by to_char(TIMESTAMP, 'HH24') * 1
order by to_char(TIMESTAMP, 'HH24') * 1 --As TIMESTAMP is now reflecting the column from inner table.