Select rows where all values within group are NULL - sql

How to:
Select rows where all rows within group are NULL. Below group is defined by id. I
wish to select rows where all values are NULL.
Id Var1
1 NULL
1 NULL
1 NULL
2 10
2 20
2 30
3 NULL
3 30
What I have tried:
select id
from table
where all var1 is null
group by id
Desired result:
id var1
1 NULL
1 NULL
1 NULL

Use having instead of where. It filters after aggregation:
select id
from table
group by id
having max(var1) is null;
A similar method uses:
having count(var1) = 0

You can try with this query:
select id,Var1
from table1 as a
where not exists (select id
from table1 as b
where a.id=b.id and b.Var1 is not null)
The subquery get the id that have values not null, so you don get them in the main query

Related

SQL - How to find out that a status field has no record with a value equal to 3

I have a table that can look like this:
user_id
status
1
3
1
5
2
3
2
1
3
5
table has user_id, and status column,
the status column is integer has 1,2,3,4,5 state, each user can has one than one record
but only can has one record that status is 3.
How to find out that a status field has no record with a value equal to 3
Please help!
Thanks
Rob
You could use an aggregation approach:
SELECT user_id
FROM yourTable
GROUP BY user_id
HAVING COUNT(CASE WHEN status = 3 THEN 1 END) = 0;
Using exists logic we can try:
SELECT DISTINCT user_id
FROM yourTable t1
WHERE NOT EXISTS (
SELECT 1
FROM yourTable t2
WHERE t2.user_id = t1.user_id AND
t2.status = 3
);

Order by statement - two columns

I have a SQL select query from table x. In this query, I get BpName from table x and BpName2 from scalar function. I want to order by BpName2 if BpName is null and by BpName if is not null. Is it possible?
Example:
These are my rows:
Id BpName BpName2
------------------------
1 NULL 'C'
2 'A' NULL
3 NULL 'B'
I want to order them like this:
Id BpName BpName2
------------------------
2 'A' NULL
3 NULL 'B'
1 NULL 'C'
You could order by the coalesced result of the columns:
SELECT *
FROM mytable
ORDER BY COALESCE(BpName, BpName2)

Return count of ids where occurrence is equal to 1 in SQL

I have a dataset with a column of ids (e.g.)
ID
1
2
3
3
3
4
4
5
I want to return the count of the rows where an id only appears once. For example, from the above table the query would return count = 3 as only ID 1, 2 and 5 appear once.
This query:
SELECT id
FROM tablename
GROUP BY id
HAVING COUNT(*) = 1
returns all the ids that you want to count, so count them like this:
SELECT COUNT(*)
FROM (
SELECT id
FROM tablename
GROUP BY id
HAVING COUNT(*) = 1
) t
See the demo.
One method is two levels of aggregation
select count(*)
from (select id, count(*) as cnt
from t
group by id
) i
where cnt = 1;

TSQL - excluding the rows that matches with temp table

I need a help with SQL SELECT query.
I have [Details] sql table below. I want to select rows that have matching group id and that does not exist in given temp table
DetailID GroupID TemplateID DocumentID
------------------------------------------------------
1 A 2 NULL
2 A NULL 33
3 A 10 NULL *
4 B NULL 33
5 B 4 NULL *
6 C 2 NULL
7 C 4 NULL *
8 C NULL 55 *
#tmpDetails - A Templarary table that has TemplateID and DocumentID
TemplateID DocumentID
---------------------------
2 NULL
NULL 33
I want to select rows that are in group A, B and C and does not have matching TemplateID and DocumentID from #tmpDetail table
so the select query should return the rows with detail ID 3,5,7,8
The query below does not return anything
SELECT * from Details D
WHERE D.GroupID IN ('A','B','C') AND NOT EXISTS
(SELECT d2.DetailID FROM Details d2
JOIN #tmpDetails t ON d2.TemplateID IS NOT NULL
AND d2.TemplateID = t.TemplateID
OR d2.DocumentID IS NOT NULL AND d2.DocumentID = t.DocumentID)
whatever the condition have in JOIN it should be the where condition but i am sure how do compose that SQL
You did not connect your nested query with the main table (D).
You can get rid of the join and do this:
SELECT * FROM Details D
WHERE D.GroupID IN ('A','B','C') AND NOT EXISTS
(SELECT 1 FROM #tmpDetails t
WHERE (D.TemplateID IS NOT NULL AND D.TemplateID = t.TemplateID) OR
(D.DocumentID IS NOT NULL AND D.DocumentID = t.DocumentID))
SELECT D.*
FROM
Details D
OUTER APPLY
(
SELECT TOP 1 1 Match
FROM #tmpDetails T
WHERE
D.TemplateID = T.TemplateID
OR D.DocumentID = T.DocumentID
) T
WHERE
D.GroupID IN ('A','B','C')
AND T.Match IS NULL
You could use NOT EXISTS and subquery with INTERSECT to handle NULL values:
SELECT * FROM Details D
WHERE D.GroupID IN ('A', 'B', 'C')
AND NOT EXISTS (SELECT TemplateID, DocumentID FROM #tmpDetails INTERSECT
SELECT D.TemplateID, D.DocumentID)

Oracle SQL - Keep null value if it's the only one, else avoid null value

I have this table :
Table1
-----------------
ID VAL
-----------------
1 A
1 B
1 null
2 null
3 B
3 null
4 B
4 C
5 null
I want to get this result :
If the only value is null then return null, else return non null
values
Table1
-----------------
ID VAL
-----------------
1 A
1 B
2 null
3 B
4 B
4 C
5 null
I tried using Over Partition By with a Case but my statement became too complicated with several subqueries, if think there should be a simple way to do this.
The COUNT( value ) OVER ( PARTITION BY id ) analytic function will count the rows where value is not-NULL for each id. You can use it to filter the rows like this:
SELECT id, value
FROM (
SELECT t.*,
COUNT( value ) OVER ( PARTITION BY id ) AS num_values
FROM yourtable t
)
WHERE value IS NOT NULL
OR num_values = 0;
If the only value is null then return null, else return non null values
If you want to ensure that NULL is only returned if there is exactly one row for the id then:
SELECT id, value
FROM (
SELECT t.*,
COUNT( * ) OVER ( PARTITION BY id ) AS num_rows
FROM yourtable t
)
WHERE value IS NOT NULL
OR num_rows = 1;
Try this:
SELECT *
FROM yourtable t1
WHERE val IS NOT NULL OR
NOT EXISTS (SELECT 'VALUED'
FROM yourtable t2
WHERE t2.id = t1.id
AND t2.val IS NOT NULL)