[英]if count of a select sql is > 0 then union the result with another sql's result
I have a SQL as follows: 我有一个SQL,如下所示:
SELECT
'' AS "something1",
'a pre defined text' AS "something2",
'' AS "something3"
FROM dual
UNION ALL
SELECT
rownum AS "something1",
resultset.*
FROM (
SELECT
t1.abcd AS "something2",
t2.xyz AS "something3"
FROM table_1 t1, table_2 t2
WHERE t1.mnp = t2.mnp
ORDER BY "something2"
) resultset;
This SQL produce the result like: 该SQL产生如下结果:
something1 | something2 | something3
-------------------------------------------------------------
a pre defined text
1 value of abcd from t1 value of xyz from t2
2 value of abcd from t1 value of xyz from t2
and so on... 等等...
The first line in the result comes from the select part of dual (line 1 to 5) and the remaining lines are comes from the subsql of the UNION clause (line 7 to 17). 结果的第一行来自对偶的选择部分(行1至5),其余行来自UNION子句的子SQL(行7至17)。
Now the challenge is the result from the dual (line 1 to 5) should be present if any result found from the subsql (line 7 to 17). 现在的挑战是,如果从子SQL(第7至17行)中找到任何结果,则应该出现双(第1至5行)的结果。
For example if any result found from the subsql then the final output will be same as above, otherwise as follows: 例如,如果从子SQL中找到任何结果,则最终输出将与上面相同,否则如下所示:
something1 | something2 | something3
-------------------------------------------------------------
Is there any way to achieve it? 有什么办法可以实现? I have tried some approaches but not succeeded.
我尝试了一些方法,但没有成功。
You could try adding a condition on the standard hard-coded row to only be displayed if the second selects returns any row, like the example below: 您可以尝试在标准的硬编码行上添加条件,仅在第二个选择返回任何行时才显示,例如以下示例:
select
'' as 'something1',
'a pre defined text' as 'something2',
'' as 'something3'
from
dual
where exists (select 1 from table_1 t1, table_2 t2 where t1.mnp = t2.mnp)
union all
select
rownum as 'something1',
resultset.*
from (
select
t1.abcd as 'something2',
t2.xyz as 'something3'
from
table_1 t1, table_2 t2
where
t1.mnp = t2.mnp
order by
'something2'
) resultset;
First of all, please fix all obvious problems in your code before posting: 首先,请在发布代码之前解决代码中所有明显的问题:
Your problem per se can be solved by using the analytical function COUNT()
to count the number of rows whose something1 is NOT NULL: 您本身的问题可以通过使用解析函数
COUNT()
来计算something1不为NULL的行数来解决:
count(something1) over (partition by 1) as cnt
After computing this count, you can then apply a WHERE clause to filter out rows whose cnt equals 0. Your overall query (slightly re-written, with example data): 计算完此计数后,您可以应用WHERE子句以过滤出cnt等于0的行。您的总体查询(用示例数据略微重写):
with table_1 as
(select 'abcd from t1' as abcd,
1 as mnp
from dual),
table_2 as
(select 'xyz from t2' as xyz,
1 as mnp
from dual),
resultset as
(select t1.abcd as something2,
t2.xyz as something3
from table_1 t1
join table_2 t2
on t1.mnp = t2.mnp
order by something2),
resultset_plus_dummy as
(select null as something1,
'a pre defined text' as something2,
'' as something3
from dual
union all
select rownum as something1,
resultset.*
from resultset),
resultset_with_cnt as
(select r.*,
count(something1) over(partition by 1) as cnt
from resultset_plus_dummy r)
select *
from resultset_with_cnt
where cnt > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.