afeef
afeef

Reputation: 4706

cannot group by the data

Input

A   B    date
--------------------
00  12   22-01-2019
00  12   null
00  25   22-01-2019
00  24   22-01-2019 

From the above data I need the following output:

  A   B    date
--------------------
 00  12    null
 00  25    22-01-2019
 00  24   22-01-2019

I need to get open,closed records from the table.

if i used date is null then other records existing will not appear.

i need to apply condition in Column B.

If Column there is null value in date for column B then null will show otherwise if there both null and not null value null then date null will be output.

Select a,b,date 
from table1,table2
where ...
group by a,b,date

Upvotes: 1

Views: 70

Answers (2)

MT0
MT0

Reputation: 167981

Oracle Setup:

CREATE TABLE table_name ( A, B, DT ) AS
SELECT 0, 12, DATE '2019-01-22' FROM DUAL UNION ALL
SELECT 0, 12, NULL FROM DUAL UNION ALL
SELECT 0, 25, DATE '2019-01-22' FROM DUAL UNION ALL
SELECT 0, 24, DATE '2019-01-22' FROM DUAL;

Query:

SELECT A,
       B,
       CASE
       WHEN COUNT( CASE WHEN DT IS NULL THEN 1 END ) > 0
       THEN NULL
       ELSE MAX( DT )
       END AS DT
FROM   table_name
GROUP BY A, B;

Output:

 A |  B | DT       
-: | -: | :--------
 0 | 12 | null     
 0 | 24 | 22-JAN-19
 0 | 25 | 22-JAN-19

db<>fiddle here

Upvotes: 2

Ponder Stibbons
Ponder Stibbons

Reputation: 14848

Aggregate functions ignores nulls so you need anaytic version:

select a, b, min(date_) keep (dense_rank first order by date_ nulls first) 
  from input group by a, b

demo

Upvotes: 3

Related Questions