Home » SQL & PL/SQL » SQL & PL/SQL » How to filter records from UNION ALL query
How to filter records from UNION ALL query [message #643891] Tue, 20 October 2015 14:14 Go to next message
toitdoctor
Messages: 6
Registered: September 2015
Location: USA
Junior Member
Hi All,


I have one requirement to filter out records from a UNION ALL query . If we do not have any matching records in any of union all query.


create table test_1(n1 number,n2 number,n3 number)

insert into test_1 values(1,2,3);

insert into test_1 values(11,22,33);

insert into test_1 values(55,66,77);


Case-1 :


SELECT N1,N2,N3
FROM TEST_1
WHERE N2=2
UNION ALL
SELECT N1,N2,N3
FROM TEST_1
WHERE N3=3

Output is : This is desired output(so we are good)

1,2,3
1,2,3


Case 2 :
SELECT N1,N2,N3
FROM TEST_1
WHERE N2=22
UNION ALL
SELECT N1,N2,N3
FROM TEST_1
WHERE N3=44


Output : (Here is the problem: If we don't have matching records for anyone union all query we don't want any records)


Output is coming : 11,22,33


Out put : No Records (We want this output.)


I hope I have given the clear example of my requirement.


Thanks.


[Updated on: Tue, 20 October 2015 14:16]

Report message to a moderator

Re: How to filter records from UNION ALL query [message #643892 is a reply to message #643891] Tue, 20 October 2015 14:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
SQL> select n1,n2,n3
  2  from (
  3  select n1, n2, n3, count(*) over() cnt
  4  from (
  5  SELECT N1,N2,N3
  6  FROM TEST_1
  7  WHERE N2=&n2
  8  UNION ALL
  9  SELECT N1,N2,N3
 10  FROM TEST_1
 11  WHERE N3=&n3
 12  )
 13  )
 14  where cnt = 2
 15  /
Enter value for n2: 2
Enter value for n3: 3

        N1         N2         N3
---------- ---------- ----------
         1          2          3
         1          2          3

2 rows selected.

SQL> /
Enter value for n2: 22
Enter value for n3: 44

no rows selected

icon14.gif  Re: How to filter records from UNION ALL query [message #643907 is a reply to message #643892] Wed, 21 October 2015 03:45 Go to previous messageGo to next message
live4learn
Messages: 41
Registered: September 2013
Location: Bangalore, India
Member
@Michel : superb ,But not generic

SQL>select * from test_1
o/p:
N1 N2 N3
1 2 3
11 22 33
55 66 77
1 2 3
11 22 33
55 66 77

SQL> select n1,n2,n3
2 from (
3 select n1, n2, n3, count(*) over() cnt
4 from (
5 SELECT N1,N2,N3
6 FROM TEST_1
7 WHERE N2=&n2
8 UNION ALL
9 SELECT N1,N2,N3
10 FROM TEST_1
11 WHERE N3=&n3
12 )
13 )
14 where cnt = 2
15 /

Enter value for n2: 22
Enter value for n3: 44

N1 N2 N3
11 22 33
11 22 33
Re: How to filter records from UNION ALL query [message #643910 is a reply to message #643907] Wed, 21 October 2015 03:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Adapt it as you want, we have ONLY the information you gave, so we give you the query according to this information.
So what does "generic" mean here? What are ALL the possible cases for your data?
What are the exact and specific requirements? Those that can't lead to any defect in the query as soon as it works for the test case you provide (and provide this one).

Re: How to filter records from UNION ALL query [message #643915 is a reply to message #643910] Wed, 21 October 2015 08:11 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
SQL> insert into test_1 select * from test_1;

3 rows created.

SQL> select n1,n2,n3
  2  from (
  3  select n1,n2,n3, count(distinct fl) over () cnt
  4  from (
  5  SELECT N1,N2,N3, 1 fl
  6  FROM TEST_1
  7  WHERE N2=&n2
  8  UNION ALL
  9  SELECT N1,N2,N3, 2 fl
 10  FROM TEST_1
 11  WHERE N3=&n3
 12  )
 13  )
 14  where cnt = 2   --<-- Nb UNION ALL in the subquery
 15  /
Enter value for n2: 2
Enter value for n3: 3
        N1         N2         N3
---------- ---------- ----------
         1          2          3
         1          2          3
         1          2          3
         1          2          3

4 rows selected.

SQL> /
Enter value for n2: 22
Enter value for n3: 44

no rows selected
Previous Topic: Parallel Procedure Run
Next Topic: interview question asked (merged 3)
Goto Forum:
  


Current Time: Mon Jul 06 13:00:28 CDT 2026