Oracle query: if column A is null then condition on column B - sql

I have a table
num1 num2 info1 info2 status1 status2 flag
14 11111 affsd sdf 10 - 1
135 22222 fds - 20 10 0
1513 33333 - fds 10 30 1
21 44444 dd a - 20 1
- 55555 ddwd - 10 - 0
Goal: to find most fitting record based on parameters and return flag.
Conditions:
if num is not null then match status1 to num1 and status2 to num2. If either of status is null then treat this null as "Match". i both are null then return f.e. 2;
if num is null:
match info1 to my_param_1 and info2 to my_param_2. status1 and status2 - if they exists in table then have to match to params, if they don't exists - they are null and they match.
if info1 and info2 is null then return f.e. 2
My actual solution is list of if-else and diffrent select in every if section, but now i wonder if this could be done in single query? Like when this column is null then condition on another. I tried something like
WHERE case when ... then...
but no luck.
I hope i explained right and everything is understood :)
Thanks!

Related

How to pivot/merge rows based on condition in BigQuery?

I have a table that looks like this:
record
name1
name2
to_merge
value1
value2
1
STEVE
null
false
30
null
2
JOHN
null
true
43
null
3
null
LAURA
true
null
66
4
JEN
null
false
18
null
I want this to be the output:
record
name1
name2
value1
value2
1
STEVE
null
30
null
2
JOHN
LAURA
43
66
3
JEN
null
18
null
This means I want to merge the rows with a TRUE value in the to_merge field. Any help is much appreciated!
Consider below
select * except(to_merge)
from your_table
where not to_merge
union all
select max(name1), max(name2),
max(value1), max(value2)
from your_table
where to_merge
if applied to data in your question - output is

Find values which are present in all columns in a Cable

I would like a SQL Server query which finds the Values in a cell which fills multiple columns. For example, if I have table
ID Value1 Value2 Value3
1 2 NULL NULL
1 NULL 3 NULL
1 NULL NULL 4
1 3.4 NULL NULL
2 NULL 3 NULL
2 NULL NULL NULL
3 NULL NULL 91
As in the table above, only 2 of the columns can be filled at a time(First is ID and 2nd is either of Value1, 2 or 3) and ID can be repeated multiple times.
I want to return the ID as only 1 because 1 is the only ID that populates all the three other columns. 2 fills only Value2 and all the other values of 2nd iteration of 2 are NULL where as 3 is present only in Column Value3. Is there someway that I can find the Id's which fill all the other columns.
I would love to do this preferably without a cursor but I can go for cursor if it's compulsory. Thanks
EDIT
Desired Table:
ID
1
The Statement should return only the filtered IDs which populate all the other columns.
Try this
SELECT id,
FROM TableName
GROUP BY id
HAVING MAX(value1) IS NOT NULL AND
MAX(value2) IS NOT NULL AND
MAX(value3) IS NOT NULL
Something for you try if you want some less lines of code:
select ID from dbo.Table_1 group by ID having count(Value1) > 0 AND count(Value2) > 0 AND count(Value3) > 0

Where (or even any kind of join) doesn't work with Temp table in SQLite

I have to collect data from many sources into a temp table before performing another SELECT on this table and another table. The temp table has a column called RoomID while the table (which I want to join with my temp table) also has a column called RoomID (I join these 2 tables to get more info about specified Room via its RoomID), but the returned rows are not all rows (all which I think should meet the WHERE condition).
Here is my temp table (after creating and perform a SELECT on it like this: SELECT * FROM MyTempTable):
ID1 | Name1 | ID2 | Name2 | RoomID
NULL NULL 2 A 2
2 A NULL NULL 2
1 B NULL NULL 3
3 C NULL NULL 4
4 D 5 E 8
All the values in RoomID are populated and all these RoomIDs are also present in the table I want to join with MyTempTable, here is that table (call it Room):
RoomID | Name | Note
1 Dining-room NULL
2 Bathroom NULL
3 Pantry NULL
4 Living room NULL
5 Sitting room NULL
6 Music room NULL
7 Office room NULL
8 Library NULL
With those tables above, the following query should return all the matched rows as you (and I expect):
SELECT ID1, Name1, ID2, Name2, MyTempTable.RoomID as [RoomID], Name as [Room Name], Note
FROM MyTempTable, Room
WHERE MyTempTable.RoomID=Room.RoomID
But it doesn't. I want to say more about MyTempTable, in fact it is created from 3 UNIONs, and all the values in its ZoomIDs are collected from other tables (which take part in the UNION clauses). I don't know if it matters. However the SELECT performs on the last MyTempTable shows that MyTempTable does have a column of RoomID populated with some rows. Here is what it should be after performing the SELECT above:
ID1 | Name1 | ID2 | Name2 | RoomID | Room Name | Note
NULL NULL 2 A 2 Bathroom NULL
2 A NULL NULL 2 Bathroom NULL
1 B NULL NULL 3 Pantry NULL
3 C NULL NULL 4 Living room NULL
4 D 5 E 8 Library NULL
But there isn't such a full result, the result contains only 2 rows and I even don't know why they are always those 2 rows not of any other 2 rows in the set of rows above:
ID1 | Name1 | ID2 | Name2 | RoomID | Room Name | Note
2 A NULL NULL 2 Bathroom NULL
1 B NULL NULL 3 Pantry NULL
I guess, the MyTempTable.RoomID refers to the original table in the UNION which I used to create MyTempTable, because I can see that all the returned rows seem to only belong to that table.
This is really strange, and if it's not a bug, I think the things we can do with Temp table in SQLite are so limited.
Update
You can see that, there is no row with ZoomID equal to 4 returned, but if I change the SELECT to this, it's returned - why is that?
SELECT ID1, Name1, ID2, Name2, MyTempTable.RoomID as [RoomID], Name as [Room Name], Note
FROM MyTempTable, Room
WHERE MyTempTable.RoomID=4 and Room.RoomID=4
I'm sorry to say that, all the fault belongs to me. I made a mistake in creating my tables where in this the ZoomID has type of varchar and in that it has type of int. The difference in types makes the Where Condition unable to be met. And it seems that the auto-conversion is applied only when there is at least 1 constant (like 4 above) in the comparison expression.
Sorry!

SQL Ignore null value if a row with the same ID exists

I have a Sybase table with the following data (example):
Users:
ID Type PlayerID
-------- ----------- -------------------
65823 1 (null)
65823 2 187
91817 1 (null)
37950 1 773
37950 1 (null)
37968 1 (null)
37968 1 773
72576 1 (null)
72576 1 (null)
I want to return all combination of the users and type's but if there is more than one example of a particular user/type combination only show the record that isn't null.
E.g. the above table should return the following
ID Type PlayerID
-------- ----------- -------------------
65823 1 (null) - although this is null the type/id is unique
65823 2 187
91817 1 (null) - this is null but is a unique type/id
37950 1 773
37968 1 773 - this is included and the id/type that has a null player id isn't
72576 1 (null) - this is a unique type/id
So far I've looked into queries using group by, having and inner joins but have been unable to find a way to match the results I'm looking for.
I've also looked at things like group by, and then using max on the PlayerID, but aggregate functions ignore null values.
How can I return the unique id/type pairs with their player id?
--
Question from Paul:
ID Type PlayerID
-------- ----------- -------------------
65823 2 187
37950 1 773
37968 1 773
From SQL side you can do like:
select * from mytable t1 where playerId is not null
union
select * from mytable t2 where playerid is null
and not exists (
select * from mytable t3 where t2.id=t2.id and t2.type=t3.type
)
I don't know how effective it will be, but it will give you the result you want.
What does
select ID, type, max(PlayerID)
from Users
group by ID, type
return? (I have never used sybase)

Count sequential field values based on date in seperate table using SQL

I have been trying to devise an SQL query in Access 2010 to count the number of sequential field values which are based over 3 tables using fields with unique ID's
Example
Table1: Course
CorID Date
1 01/01/2012
2 01/03/2012
3 01/02/2012
Table 2: Delegate
DelID StaffID CorID Value CounterField
1 17263 2 99 1
2 17263 1 99 2
3 17263 3 99 3
4 17263 65 4 1
5 17263 44 5 1
6 17263 78 5 2
Table 3: Staff
StaffID Surname
1 Test
2 Smith
17263 Jones
The CounterField increases by 1 where the Value field in Table 2 is the same as the previous Value field. There would be a requirement to ensure that the count only uses the the order based on the course table Date field. The delegate table would also contain more DelID fields than listed and will contain different StaffID values as well.
The CounterField in the above table is an example of what I want the query to be able to do.
Is this possible? Thanks in advance.
If you want to count how many rows you have in Delegate that are not duplicate regarding the Value field:
SELECT count(*)
FROM Delegate
WHERE CounterField = 1
Try running a subquery:
SELECT a.DelID, a.StaffID, a.CorID, a.[Value],
(SELECT Count(*) FROM Delegate b
WHERE b.DelID <= a.DelID AND a.[Value]=b.[Value]) As CounterField
FROM Delegate a;