我有SQL查询,我想在结果中添加插入空行,这样很容易看到结果。 我想在ORDER BY之后插入它。不知道是否可以做到。
这是我的选择陈述。
SELECT TableName.CREWACTIONFACTID
,TableName.CREWKEY as CrewKey
,TableName.EVENTKEY as EventID
,TableName.ACTIONSEQUENCE
,case TableName.ACTIONTYPE
when 'DISPATCHED' then '2-Dispatched'
when 'ASSIGNED' then '1-Assigned'
when 'ENROUTE' then '3-Entoute'
when 'ARRIVED' then '4-Arrived'
else 'unknown'
end as Type
,TableName.STARTDATETIME as StartTime
,TableName.ENDDATETIME as EndTIme
,TableName.DURATION as Duration
FROM DatabaseName.TableName TableName
where
To_Date(to_char(TableName.STARTDATETIME, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY')
AND To_Date(to_char(TableName.ENDDATETIME, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
ORDER BY TableName.EVENTKEY, TableName.STARTDATETIME,TableName.ACTIONSEQUENCE
答案 0 :(得分:5)
你可以像迈克尔和戈登那样,只用union all
添加空行,但你需要在order by
之前得到它:
...
and to_date(to_char(t.enddatetime, 'DD-MON-YYYY')) <=
to_date('?DATE2::?','MM/DD/YYYY')
union all
select null, null, null, null, null, null, null, null
from dual
order by eventid, starttime, actionsequence;
...而且你不能使用Gordon直接在case
中拥有的order by
,因为它不是一个选定值 - 你会得到一个ORA-07185。 (请注意,order by
中的列名是您在select
中分配的别名,而不是表中的那些;并且您不包含表名/别名;并且没有必要为联合部分中的null
列添加别名,但为了清楚起见,您可能希望这样做。
但这取决于null
在任何实际值之后被排序,这可能并非总是如此(不确定,但可能受NLS参数影响),并且不知道是否真实{{1}无论如何,1}}可以eventkey
。因此,在查询的两个部分中引入虚拟列并将其用于排序可能更安全,但是通过嵌套查询将其从结果中排除:
null
日期处理虽然奇怪,尤其是select crewactionfactid, crewkey, eventid, actionsequence, type,
starttime, endtime, duration
from (
select 0 as dummy_order_field,
t.crewactionfactid,
t.crewkey,
t.eventkey as eventid,
t.actionsequence,
case t.actiontype
when 'DISPATCHED' then '2-Dispatched'
when 'ASSIGNED' then '1-Assigned'
when 'ENROUTE' then '3-Enroute'
when 'ARRIVED' then '4-Arrived'
else 'unknown'
end as type,
t.startdatetime as starttime,
t.enddatetime as endtime,
t.duration
from schema_name.table_name t
where to_date(to_char(t.startdatetime, 'DD-MON-YYYY')) >=
to_date('?DATE1::?','MM/DD/YYYY')
and to_date(to_char(t.enddatetime, 'DD-MON-YYYY')) <=
to_date('?DATE2::?','MM/DD/YYYY')
union all
select 1, null, null, null, null, null, null, null, null
from dual
)
order by dummy_order_field, eventid, starttime, action sequence;
部分。您似乎只是试图丢失时间部分,在这种情况下,您可以使用to_date(to_char(...))
代替:
trunk
但是将任何函数应用于日期列会阻止使用它的任何索引,因此最好不要单独使用它并使变量部分处于正确状态以进行比较:
where trunc(t.startdatetime) >= to_date('?DATE1::?','MM/DD/YYYY')
and trunc(t.enddatetime) <= to_date('?DATE2::?','MM/DD/YYYY')
where t.startdatetime >= to_date('?DATE1::?','MM/DD/YYYY')
and t.enddatetime < to_date('?DATE2::?','MM/DD/YYYY') + 1
添加一天,因此ID + 1
为DATE2
,过滤器为07/12/2012
,与< 2012-07-13 00:00:00
相同。
答案 1 :(得分:2)
确定的奇怪请求,但是可以通过对一行文字空值进行UNION
来完成。为了确保将order-by应用于真实查询,请将整个事物包含在()
中,然后将其与空行联合起来。
SELECT * FROM
(SELECT TableName.CREWACTIONFACTID
,TableName.CREWKEY as CrewKey
,TableName.EVENTKEY as EventID
,TableName.ACTIONSEQUENCE
,case TableName.ACTIONTYPE
when 'DISPATCHED' then '2-Dispatched'
when 'ASSIGNED' then '1-Assigned'
when 'ENROUTE' then '3-Entoute'
when 'ARRIVED' then '4-Arrived'
else 'unknown'
end as Type
,TableName.STARTDATETIME as StartTime
,TableName.ENDDATETIME as EndTIme
,TableName.DURATION as Duration
FROM DatabaseName.TableName TableName
where
To_Date(to_char(TableName.STARTDATETIME, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY')
AND To_Date(to_char(TableName.ENDDATETIME, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
ORDER BY TableName.EVENTKEY, TableName.STARTDATETIME,TableName.ACTIONSEQUENCE
)
UNION ALL
SELECT
'' AS CREWACTIONFACTID,
'' AS CrewKey,
'' AS EventID,
'' AS ACTIONSEQUENCE,
'' AS Type,
'' AS StartTime,
'' AS EndTime,
'' AS Duration
FROM dual
最后,根据你如何呈现这个结果,我会研究其他方法来区分结果。为查询目的而在查询中附加空白行会面临业务和表示逻辑的分离。
答案 2 :(得分:2)
你的问题相当复杂。 SQL只保证结果的排序,通过顺序。它不保证事后会发生什么。因此,您必须放入空白行,然后在后面添加信息:
<your select query minus the order by>
union all
select NULL as CrewActionFatId, . . .
order by (case when CrewActionFactId is NULL then 1 else 0 end),
TableName.EVENTKEY, TableName.STARTDATETIME,TableName.ACTIONSEQUENCE
在实践中,@ Michael的解决方案通常会起作用。但这并不能保证。
此外,您应该决定是否需要空格或NULL。我猜第一个id是一个数字,所以我把它设置为NULL。
通常,这种演示细节由调用应用程序处理。也许您需要一个更好的SQL查询工具来更清晰地查看数据。
以下是完整查询的外观(所有字段都设置为NULL,如果您愿意,可以更改为空白):
SELECT TableName.CREWACTIONFACTID, TableName.CREWKEY as CrewKey,
TableName.EVENTKEY as EventID, TableName.ACTIONSEQUENCE,
(case TableName.ACTIONTYPE
when 'DISPATCHED' then '2-Dispatched'
when 'ASSIGNED' then '1-Assigned'
when 'ENROUTE' then '3-Entoute'
when 'ARRIVED' then '4-Arrived'
else 'unknown'
end) as Type,
TableName.STARTDATETIME as StartTime,
TableName.ENDDATETIME as EndTIme,
TableName.DURATION as Duration
FROM DatabaseName.TableName TableName
where To_Date(to_char(TableName.STARTDATETIME, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY') AND
To_Date(to_char(TableName.ENDDATETIME, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
union all
SELECT NULL AS CREWACTIONFACTID, NULL AS CrewKey, NULL AS EventID,
NULL AS ACTIONSEQUENCE, NULL AS Type, NULL AS StartTime, NULL AS EndTime,
NULL AS Duration
from dual
ORDER BY (case when CrewActionFactId is NULL then 1 else 0 end),
TableName.EVENTKEY, TableName.STARTDATETIME, TableName.ACTIONSEQUENCE
答案 3 :(得分:1)
结果将在HTML页面中显示。
因此,使用SQL提取数据,而不是格式化输出。 根据页面结构和布局,有很多解决方案。 看看here。