简体   繁体   English

在查询标头中设置DATE Oracle SQL(SQL Developer)

[英]set DATE in query headers oracle sql (sql developer)

I am trying to display specific data that will be using DATE in headers. 我正在尝试显示将在标题中使用DATE的特定数据。

Data should be displayed in similar format to the shown below. 数据的显示格式应类似于以下所示。

Master_Column|Todays_date|Today-1|Today-2|Today-3... Master_Column | Todays_date |今日-1 |今日-2 |今日-3 ...

select  
   sysdate,
   SYSDATE "&&_DATE",
   (sysdate-1) "&&_DATE-1" --this header is incorrectly named
from 
  dual;

This however will not display the last header correctly. 但是,这将无法正确显示最后一个标题。

Is there any way to set dates in headers that will change dynamically? 有什么方法可以在标题中设置日期,这些日期会动态变化吗?

I need to have a range of 6 days. 我需要安排6天的时间。 Each day as separate column. 每天作为单独的列。

The _DATE substituion variable is a string, which tracks your NLS_DATE_FORMAT: _DATE替代变量是一个字符串,它跟踪您的NLS_DATE_FORMAT:

show defines

DEFINE _DATE =  "2018-10-31" (CHAR)

You're sort of trying to subtract a number from a string; 您正在尝试从字符串中减去数字。 except you aren't even really doing that, because no calculation would be done inside the double-quotes anyway. 除非您甚至没有真正这样做,因为无论如何在双引号内不会进行任何计算。

It's a bit messy but you could achieve the effect I think you are after using multiple substituion variables and the column ... new_value ... syntax supported by SQL Developer, with a query to set those values - which you can hide with set termout off (at least in recent versions): 有点混乱,但是您可以达到以下效果:我认为您是在使用多个替换变量和SQL Developer支持的column ... new_value ...语法之后,通过查询来设置这些值的-您可以使用set termout off隐藏该值set termout off (至少在最新版本中):

column date_0 new_value date_0
column date_1 new_value date_1
column date_2 new_value date_2
-- ...

set termout off
select to_char(sysdate, 'YYYY-MM-DD') as date_0,
  to_char(sysdate - 1, 'YYYY-MM-DD') as date_1
  -- ...
from dual;
set termout on

set verify off

select  
   sysdate,
   SYSDATE "&date_0",
   (sysdate-1) "&date_1"
from 
  dual;

SYSDATE    2018-10-31 2018-10-30
---------- ---------- ----------
2018-10-31 2018-10-31 2018-10-30

However, you need to put the whole thing, or at least the 'hidden' part, into a file and then run that with @file.sql , as termout ins't honoured for statements run directly from the worksheet. 但是,您需要将整个内容(至少是“隐藏”部分)放入文件中,然后使用@file.sql运行该文件,因为termout不能用于直接从工作表中运行的语句。 ALternatively you could add noprint to the column definitions, which hides them too - but give syou extra blank lines in the output still. 或者,您可以在列定义中添加noprint ,也将它们隐藏起来-但仍然在输出中给您额外的空白行。

You can also use those substituion variables to change the column formatting if you need to, eg with shorter dummy values selected: 您还可以根据需要使用这些替换变量来更改列格式,例如,选择较短的虚拟值:

column master_column format a20
column &date_0 format A10
column &date_1 format A15

select  
   'a' as master_column,
   'b' as "&date_0",
   'c' as "&date_1"
from 
  dual;

MASTER_COLUMN        2018-10-31 2018-10-30     
-------------------- ---------- ---------------
a                    b          c              

Note that in the column &date_0 ... etc. clauses the variable names should not be enclosed in double quotes, unlike the aliases in the main query which do have to be quoted (because of the characters used in and the formatting of the alias name). 请注意,在column &date_0 ... etc.子句中,变量名不应用双引号引起来,这与主查询中的别名必须使用双引号不同(因为别名中使用的字符和格式) )。

You can extend that approach further, and perhaps more neatly, by setting the column headers that way too; 您也可以通过这样设置列标题来进一步扩展该方法,甚至可以更整洁地扩展该方法。 so the query itself doesn't have to worry about them: 因此查询本身不必担心它们:

column master_column format a20
column col2 format A10 heading &date_0
column col3 format A15 heading &date_1

select  
   'a' as master_column,
   'b' as col2,
   'c' as col3
from 
  dual;

MASTER_COLUMN        2018-10-31 2018-10-30     
-------------------- ---------- ---------------
a                    b          c              

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM