MERGE INTO ONE ROW - oracle12c

i have table like this
HONS S.S.C H.S.C APPLICANT_ID
null null H.S.C-1 1
null S.S.C-1 null 1
B.S.C HONS - 1 null null 1
I want the output like this
HONS S.S.C H.S.C APPLICANT_ID
B.S.C HONS - 1 S.S.C-1 H.S.C-1 1
what will be the query of it?

This works for me. Hope this helps !
SELECT MAX(HONS) HONS, MAX(SSC) SSC, MAX(HSC) HSC, MAX(APPLICANT_ID) APPLICANT_ID FROM
(
select null HONS, null SSC, 'HSC-1' HSC, 1 APPLICANT_ID from dual
union
select null HONS, 'SSC-1' SSC, null HSC, 1 APPLICANT_ID from dual
union
select 'BSC-HONS' HONS, null SSC, 'HSC-1' HSC, 1 APPLICANT_ID from dual
);

Related

Hide some datetimes in a query

I have a query that returns me this result:
-----DATE--------------VALUE1---VALUE2
|2016-09-20 11:15:00| 5653856 | 37580
|2016-09-20 11:16:00| NULL NULL
|2016-09-20 11:18:00| NULL NULL
|2016-09-20 11:20:00| NULL NULL
|2016-09-20 11:30:00| 5653860 37580
|2016-09-20 11:32:00| NULL NULL
|2016-09-20 11:34:00| NULL NULL
In this table, only the records in xx:00, xx:15, xx:30, xx:45, have values, other records are null.
How can I make a condition in my query to get only 00,15,30 and 45 times records and dont show the others?
This is the query:
SELECT t.date,
MAX(CASE WHEN t.id= '924' THEN t.value END) - MAX(CASE WHEN t.id= '925' THEN t.valueEND) as IMA_71,
MAX(CASE WHEN t.id= '930' THEN t.value END) as IMA_73
FROM records t
where office=10
and date between '2016-09-20 11:15:00' and '2016-10-21 11:15:00'
GROUP BY t.office,t.date order by t.date asc;
You could use extract to determine the minute, and filter on that:
where extract('minute' from t.date) in (0, 15, 30, 45)

TSQL - Get latest rows which their title is not null

I have following table:
========================
Id SubCode Title
========================
1 1 test1
1 2 test2
1 3 NULL
1 4 NULL
2 1 k1
2 2 k2
2 3 k3
2 4 NULL
No I want to select latest rows which their title is not null, for example for Id 1 then query must show test2 and for Id 2 it must be k3:
========================
Id SubCode Title
========================
1 2 test2
2 3 k3
I have written this query:
select t.Id, t.SubCode, t.Title from Test t
inner join (
select max(Id) as Id, max(SubCode) as SubCode
from Test
group by Id
) tm on t.Id = tm.Id and t.SubCode = tm.SubCode
But this code gives the wrong result:
========================
Id SubCode Title
========================
1 4 NULL
2 4 NULL
Any idea?
You forgot to exclude NULLs by writing an appropriate WHERE clause (where title is not null).
However such problems (to get a best / last / ... record) are usually best solved with analytic functions (RANK, DENSE_RANK, ROW_NUMBER) anyway, because with them you access the table only once:
select id, subcode, title
from
(
select id, subcode, title, rank() over (partition by id order by subcode desc) as rn
from test
where title is not null
) ranked
where rn = 1;
You need a Title is not null where clause in your inner select:
select t.Id, t.SubCode, t.Title from Test t
inner join (
select max(Id) as Id, max(SubCode) as SubCode
from Test
where Title is not null
group by Id
) tm on t.Id = tm.Id and t.SubCode = tm.SubCode

How to get below SQL output

I am trying to get select from a table and return row based on values of a column. Below is data and desired output. If column EmpRecord has multiple values not null to be returned, if it has only null then it should be returned.
Data Table
EmployeeNo EmpRecord
1 A
1 NULL
2 a
3 NULL
4 NULL
4 A
4 aa
Output
EmployeeNo EmpRecord
1 A
2 a
3 NULL
4 A
4 aa
Any advice on how to go ahead with it would be great?
Regards,
Sid
The first half of the UNION query below simply strips off records for which the EmpRecord be NULL. This almost gets the job done, except that for employees who have only one more NULL records, this would remove them from the result set as well. So the second part of the UNION adds these employees back as a single record with their employee number and NULL placeholder for the record.
SELECT t1.EmployeeNo,
t1.EmpRecord
FROM yourTable t1
WHERE t1.EmpRecord IS NOT NULL
UNION ALL
SELECT t2.EmployeeNo,
NULL AS EmpRecord
FROM yourTable t2.
GROUP BY t2.EmployeeNo
HAVING SUM(CASE WHEN t2.EmpRecord IS NULL THEN 1 ELSE 0 END) = COUNT(*)

GROUP BY for MAX and NULL

I want max startdate but there is a NULL data, it will be null.
Sample data is as follows:
DECLARE #Tbl TABLE (Id INT, StartDate DATETIME)
INSERT INTO #Tbl
VALUES (1, NULL),
(1, '2016.07.30'),
(1, '2016.07.05'),
(1, '2016.07.05'),
(2, '2016.07.07'),
(2, '2016.07.05'),
(3, '2016.07.05'),
(3, NULL)
My Query:
SELECT Id, MAX(StartDate) AS StartDate
FROM #Tbl
GROUP BY Id
Output:
Id StartDate
----------- ----------
1 2016-07-30
2 2016-07-07
3 2016-07-05
Desired Output:
Id StartDate
----------- ----------
1 NULL
2 2016-07-07
3 NULL
To solve this problem we can use a count function that behave different in two cases:
when we use count(*) then all rows are count (also with null value)
when we use count(someFieldName) then only rows with not null value are count
You can see this different behaviour on this example using sample data from the question
select Id, count(*) as count_all, count(StartDate) as count_StartDate
from #Tbl
group by Id;
On the output we can see this
Id count_all count_StartDate
1 4 3
2 2 2
3 2 1
We can use this different behaviour to solve problem from question by this query
select Id, case when count(*) = count(StartDate)
then max(StartDate)
else null
end as StartDate
from #Tbl
group by Id
On the output we can see the desired result
Id StartDate
1 NULL
2 2016-07-07 00:00:00.000
3 NULL
Found the result.
SELECT Id, CASE
WHEN MAX(COALESCE(StartDate, '2099.01.01')) = '2099.01.01' THEN NULL
ELSE MAX(StartDate) END AS StartDate
FROM #Tbl
GROUP BY Id

2 rows presented as 1

This query gives me 2 rows but the result is presented a just 1 row. I dont understand why. I want the result as 2 rows.
SELECT *
FROM table AS S1
INNER JOIN table AS S2
ON S1.code = S2.code
WHERE S1.column1 IS NULL
AND S2.column1 IS NOT NULL
Here is the ouptput that i am expecting:
EXPECTED OUTPUT:
ID login email code column1
--------------------------------------------
96 testid1 test-Email1 XPQR NULL
97 testid1 test-Email1 XPQR P
Just a wild guess:
SELECT *
FROM table AS S1
WHERE EXISTS
( SELECT *
FROM table AS S2
WHERE ( S2.code = S1.code )
AND ( ( S1.column1 IS NULL AND S2.column1 IS NOT NULL )
OR ( S2.column1 IS NULL AND S1.column1 IS NOT NULL )
)
) ;