我有一个SQL查询,它带来了有关表空间及其大小的信息。我正在将此查询用于oracle数据库。
QUERY
SELECT
df.tablespace_name tablespace,
(df.total_space_mb - fs.free_space_mb) kullanilan_alan,
fs.free_space_mb bos_alan,
ROUND(100 * ((df.total_space - fs.free_space) / df.total_space), 2) doluluk_orani
FROM (
SELECT
tablespace_name,
SUM(bytes) total_space,
ROUND(SUM(bytes) / 1048576) total_space_mb
FROM dba_data_files
GROUP BY tablespace_name) df,
(
SELECT
tablespace_name,
SUM(bytes) free_space,
ROUND(SUM(bytes) / 1048576) free_space_mb
FROM dba_free_space
GROUP BY tablespace_name) fs
WHERE df.tablespace_name = fs.tablespace_name (+)
ORDER BY fs.tablespace_name;
这给我一个这样的结果:
system - 322 - 23 - 92.32
上面的查询显示了4列。我创建了一个有5列的新表,它有一个日期字段。所以我需要复制上面的查询结果,加上日期。这是一个例子:
system - 322 - 23 - 92.32 - 01/09/2014
我在sql方面不擅长,所以我尝试了太多但是失败了。
答案 0 :(得分:1)
您总是可以在选择中使用“常量”值(因此在插入...中选择)
所以这完全有效:
insert into table2 (id, f1, f2, f3, f4, fdate, farbitrary)
select id, field1, field2, field3, field4, SYSDATE, 'I put what I want here')
from table1
在这种情况下,id,field1,field2,field3,field4是table1的字段,而SYSDATE
是......当前日期和时间('I put what I want here'
是“常量”字符串)
答案 1 :(得分:0)
您可以按SYSDATE
获取今天的日期,并附加到选择列表以便插入新表格。
INSERT INTO newTable(colA,colB,colC,colD,DateColumn)
SELECT df.tablespace_name TABLESPACE,
(df.total_space_mb - fs.free_space_mb) KULLANILAN_ALAN,
fs.free_space_mb BOS_ALAN,
ROUND(100 * ((df.total_space - fs.free_space) / df.total_space),2) DOLULUK_ORANI ,
SYSDATE
FROM (SELECT tablespace_name, SUM(bytes) TOTAL_SPACE,
ROUND(SUM(bytes) / 1048576) TOTAL_SPACE_MB
FROM dba_data_files
GROUP BY tablespace_name) df,
(SELECT tablespace_name, SUM(bytes) FREE_SPACE,
ROUND(SUM(bytes) / 1048576) FREE_SPACE_MB
FROM dba_free_space
GROUP BY tablespace_name) fs
WHERE df.tablespace_name = fs.tablespace_name(+)
ORDER BY fs.tablespace_name;
答案 2 :(得分:0)
您可以通过以下方式获取当天:
select ..., trunc(sysdate) from ...
回答你的问题。我还想推荐两件事:1。使用新的连接语法(左外连接而不是(+)运算符),2。尽可能晚地除以避免舍入错误。将2.4舍入到3和1.4舍入为1会给你3-1 = 2而不是舍入(2.6-1.4)= 1。
select
tablespace_name as tablespace,
round((df.total_space_mb - fs.free_space_mb) / 1048576) as kullanilan_alan,
round(fs.free_space / 1048576) as bos_alan,
round((df.total_space - fs.free_space) / df.total_space * 100, 2) as doluluk_orani
-- , trunc(sysdate) as current_day
from
(
select tablespace_name, sum(bytes) as total_space
from dba_data_files
group by tablespace_name
) df
left outer join
(
select tablespace_name, sum(bytes) as free_space
from dba_free_space
group by tablespace_name
) fs
using (tablespace_name = tablespace_name)
order by tablespace_name;