How to find largest sequence of consecutive null values in a column - sql

I want to get the maximum consecutive occurrence of null in a column as shown in the screenshot. As there is ID column also there in this table, ordering could be done based on it.
Like in this case, the output would be 4 as maximum 4 nulls are together.
SELECT * INTO #tmp FROM (Select 1 AS ID, 1 as lvl union all
Select 2, 1 union all
Select 3, Null union all
Select 4, Null union all
Select 5, 1 union all
Select 6, 1 union all
Select 7, Null union all
Select 8, Null union all
Select 9, Null union all
Select 10, Null union all
Select 11, 1 union all
Select 12, 1 union all
Select 13, 1 union all
Select 14, 1)x
SELECT * FROM #tmp
DROP TABLE #tmp
I just dont know from where to start, so any help would be appreciable.

Following the post at this link, I found the solution of my problem. The query giving correct result as 4 is:
SELECT TOP 1 COUNT(*)AS MaxNull
FROM
#tmp t
JOIN #tmp t2 ON t2.id <= t.id
WHERE NOT EXISTS(
SELECT 1
FROM #tmp t3
WHERE t3.id BETWEEN t2.id AND t.id
AND NOT t3.lvl IS NULL)
GROUP BY t.id
ORDER BY COUNT(*)DESC;

Related

Group and exclude rows that had empty values aggregated - oracle sql

I have a oracle sql table that looks like so
"STUDENT_ID","FULL_NAME","SEMESTER_ID","STIP_ID"
"1","Liam Bottrill","1","1"
"1","Liam Bottrill","2","3"
"1","Liam Bottrill","3","2"
"1","Liam Bottrill","4","5"
"2","Maurits Smitham","1","6"
"2","Maurits Smitham","2",""
"2","Maurits Smitham","3","2"
"2","Maurits Smitham","4","6"
"43","Jackie Cotton","1",""
"43","Jackie Cotton","2",""
"43","Jackie Cotton","3",""
"43","Jackie Cotton","4",""
I want to group this table by "STUDENT_ID" and exclude from result any students that have any of "STIP_ID" rows empty
Im aiming for result like this:
"STUDENT_ID","FULL_NAME"
"1","Liam Bottrill"
Liam Bottrill should be displayed while Maurits Smitham and Jackie Cotton should be excluded from result
Can you please help me with such aggregate function?
Here is one way, using aggregation:
SELECT *
FROM yourTable
WHERE STUDENT_ID IN (
SELECT STUDENT_ID
FROM yourTable
GROUP BY STUDENT_ID
HAVING COUNT(CASE WHEN STIP_ID IS NULL THEN 1 END) = 0
);
Another way, using exists logic:
SELECT t1.*
FROM yourTable t1
WHERE NOT EXISTS (
SELECT 1
FROM yourTable t2
WHERE t2.STUDENT_ID = t1.STUDENT_ID AND
t2.STIP_ID IS NULL
);
You can group by the identifier and then use conditional aggregation to find the student where the count when STIP_ID is NULL (which, in Oracle, is the same as an empty string):
SELECT student_id,
MAX(full_name) AS full_name
FROM table_name
GROUP BY student_id
HAVING COUNT(CASE WHEN stip_id IS NULL THEN 1 END) = 0;
Which, for your sample data:
CREATE TABLE table_name (STUDENT_ID, FULL_NAME, SEMESTER_ID, STIP_ID) AS
SELECT 1, 'Liam Bottrill', 1, 1 FROM DUAL UNION ALL
SELECT 1, 'Liam Bottrill', 2, 3 FROM DUAL UNION ALL
SELECT 1, 'Liam Bottrill', 3, 2 FROM DUAL UNION ALL
SELECT 1, 'Liam Bottrill', 4, 5 FROM DUAL UNION ALL
SELECT 2, 'Maurits Smitham', 1, 6 FROM DUAL UNION ALL
SELECT 2, 'Maurits Smitham', 2, NULL FROM DUAL UNION ALL
SELECT 2, 'Maurits Smitham', 3, 2 FROM DUAL UNION ALL
SELECT 2, 'Maurits Smitham', 4, 6 FROM DUAL UNION ALL
SELECT 43, 'Jackie Cotton', 1, NULL FROM DUAL UNION ALL
SELECT 43, 'Jackie Cotton', 2, NULL FROM DUAL UNION ALL
SELECT 43, 'Jackie Cotton', 3, NULL FROM DUAL UNION ALL
SELECT 43, 'Jackie Cotton', 4, NULL FROM DUAL;
Outputs:
STUDENT_ID
FULL_NAME
1
Liam Bottrill
db<>fiddle here

Getting a specific output from 2 tables

I have 2 tables :
tab_1 :
ID VAL
1 Y
2 N
3 Y
tab_2 :
ID VAL
2 N
3 X
4 Y
I want to get the final output like
ID Operation
1 INSERT
2 EQUAL
3 DIFF
4 DEL
I am in very basic level in joins so need some help in explanation/understanding this type of functionalities.Thanks in advance.
It appears that you want a full outer join between the two tables; and then a case expression to compare the val columns (both their existence and values) in both tables. Maybe something like:
-- CTEs for sample data
with tab_1 (ID, VAL) as (
select 1, 'Y' from dual
union all select 2, 'N' from dual
union all select 3, 'Y' from dual
),
tab_2 (ID, VAL) as (
select 2, 'N' from dual
union all select 3, 'X' from dual
union all select 4, 'Y' from dual
)
-- actual query
select coalesce(t1.id, t2.id) as id,
case
when t1.id is null then 'DEL'
when t2.id is null then 'INSERT'
when t2.val = t1.val then 'EQUAL'
else 'DIFF'
end as operation
from tab_1 t1
full outer join tab_2 t2 on t2.id = t1.id
order by id;
ID OPERATION
---------- ---------
1 INSERT
2 EQUAL
3 DIFF
4 DEL

Oracle SQL compare aggregated lines

Kind of stuck in relatively simple SQL...
Could someone propose some code for retrieving the GroupID for aggregated lines (group by GroupID) whose aValue is different ?
For example in the below table I'd need to get GroupID '4' as the 2 Items with in the same group (4) have different aValue
GroupId ItemID aValue
4 19 Hello
4 20 Hello1
5 78 Hello5
5 86 Hello5
You can use the having clause and look at the count of distinct values:
-- CTE for your sample data
with your_table (groupid, itemid, avalue) as (
select 4, 19, 'Hello' from dual
union all select 4, 20, 'Hello1' from dual
union all select 5, 78, 'Hello5' from dual
union all select 5, 86, 'Hello5' from dual
)
select groupid
from your_table
group by groupid
having count(distinct avalue) > 1;
GROUPID
----------
4
If you actually also want to see the individual values, you can use an analytic count in a subquery and filter that with where instead of having:
-- CTE for your sample data
with your_table (groupid, itemid, avalue) as (
select 4, 19, 'Hello' from dual
union all select 4, 20, 'Hello1' from dual
union all select 5, 78, 'Hello5' from dual
union all select 5, 86, 'Hello5' from dual
)
select groupid, itemid, avalue
from (
select groupid, itemid, avalue,
count(distinct avalue) over (partition by groupid) as value_count
from your_table
)
where value_count > 1;
GROUPID ITEMID AVALUE
---------- ---------- ------
4 19 Hello
4 20 Hello1
I would do this as :
select GroupId
from table t
group by GroupId
having min(aValue) <> max(aValue);
However, if you want all columns/expression then you can use EXISTS
select t.*
from table t
where exists (select 1
from table t1
where t1.GroupId = t.GroupId and
t1.avalue <> t.avalue
);

join 2 tables with SQL

I have to join 2 tables with SQL in a special way:
TABLE1 has the fields GROUP and MEMBER, TABLE2 has the fields GROUP and MASTER.
I have to build a new TABLE3 with the fields GROUP and ID by copying TABLE1 to TABLE3 and search TABLE2 if there is a GROUP from TABLE1 and if, copy GROUP and MASTER to TABLE3.
Example:
table1:
group member
1 a
1 b
1 c
2 x
3 y
table2:
group master
3 n
3 z
1 k
9 v
2 m
7 o
8 p
Expected result, table3:
group id
1 a from table1
1 b from table1
1 c from table1
1 k from table2
2 x from table1
2 m from table2
3 y from table1
3 z from table2
3 n from table2
I hope everything's clear.
So what is the SQL query?
Thanks, Hein
The first part (copy members) should be easy:
INSERT INTO table3 (group, id) SELECT group, member FROM table1;
Then You just copy the masters, that are in groups, that are already present in table1:
INSERT INTO table3 (group, id) SELECT group, master FROM table2 WHERE group IN (SELECT DISTINCT group FROM table1);
Try this out. Of course you need to INSERT the whole selection to your new table named Table3.
WITH TABLE1(GRP,MMBR) AS
(SELECT 1, 'a' FROM DUAL UNION ALL
SELECT 1, 'b' FROM DUAL UNION ALL
SELECT 1, 'c' FROM DUAL UNION ALL
SELECT 2, 'x' FROM DUAL UNION ALL
SELECT 3, 'y' FROM DUAL),
TABLE2(GRP,MSTR) AS
(SELECT 3, 'n' FROM DUAL UNION ALL
SELECT 3, 'z' FROM DUAL UNION ALL
SELECT 1, 'k' FROM DUAL UNION ALL
SELECT 9, 'v' FROM DUAL UNION ALL
SELECT 2, 'm' FROM DUAL UNION ALL
SELECT 7, 'o' FROM DUAL UNION ALL
SELECT 8, 'p' FROM DUAL)
SELECT * FROM (
SELECT GRP, MMBR ID FROM TABLE1
UNION --UNION ALL if you need duplicates
SELECT GRP, MSTR ID FROM TABLE2
WHERE TABLE2.GRP IN (SELECT GRP FROM TABLE1)
)
ORDER BY GRP, ID
You can do it using UNION ALL and 2 simple SELECT in an INSERT as follows:
INSERT INTO table3(group,id)
SELECT group,id FROM table1
UNION ALL
SELECT group,id FROM table2
SELECT * FROM table3;
And if you don't want duplicate values,try this using UNION instead of UNION ALL:
INSERT INTO table3(group,id)
SELECT group,id FROM table1
UNION
SELECT group,id FROM table2
SELECT * FROM table3;

Cannot figure out logic to update a table

I have below table.
TABLE: ABCD
B column have value 1 whenever there is a change in A column. Now I have to update the table like below. How can I do that?
You can do this using a correlated subquery:
update t
set b = (select sum(t2.b) from t t2 where t2.A <= t.A);
This is standard SQL and should work in either Oracle or Teradata.
Lets have a slightly more complicated example (where the changes in B are not correlated to the changes in A):
Oracle Setup:
CREATE TABLE ABCD( A, B ) AS
SELECT 1, 0 FROM DUAL UNION ALL
SELECT 1, 0 FROM DUAL UNION ALL
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 2, 0 FROM DUAL UNION ALL
SELECT 3, 0 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 3, 0 FROM DUAL;
Update:
UPDATE ABCD t1
SET b = (
SELECT sm
FROM (
SELECT ROWID AS id,
SUM( b ) OVER ( ORDER BY a, ROWNUM ) AS sm
FROM ABCD
) t2
WHERE t1.ROWID = t2.ID
);
Output:
SELECT * FROM ABCD;
A B
- -
1 0
1 0
1 1
2 2
2 2
3 2
3 3
3 3
(Note: This is an Oracle solution; I have no idea if it will or won't work in Teradata.)