I am trying to select the earlier date/time from a two given columns. However, I am running into issues if one of the two columns have a null value.
my thought is
select case when dateTime1 < datetime2 then column1 else column2
end as EarlierDate
from table
However, using the above method will always return null values regardless how I change the greater or smaller sign.
You can have:
Select Case When Column1 is null then Column2 when Column2 is Null then Column1 When Column1 > Column2 Then Column2 When Column1 < Column2 Then Column1 End As EarlierDate From TableName
Related
I'm writing a query to update a table. The table has many fields among which there are 3 fields: column1, column2, column3 each of which can be null.
The data is loaded every day. On day 1 a row can come in which
column1 is null,
column2 is not null
column3 is null.
The next day there may be a row where
column1 is not null,
column2 is null,
column3 is null.
The idea is to insert new rows, and those rows which are identical to previously uploaded ones, but which column1, column2, column3 change are not inserted, but updated.
I tried to write a query to update all three fields at once. But in case described above, I will change not null to null.
Does anyone know a way to update any of the fields in a single update query without writing null there?
for example:
update table2 upt
set column1 = n.column1,
column2 = n.column2,
column3 = n.column3
from (select new.column1, new.column2, new.column3, new.column4, new.column5
from table1 new
where new.column4 in (select old.column4 from table2 old
where old.column5 = new.column5
and (old.column1 is null or old.column2 is null or old.column3 is null)
and (new.column1 is not null or new.column2 is not null and new.column3 is not null) n
where upt.date_fielad = '2022-01-01'
and upt.column4 = n.column4
and upt.column5 = n.column5
and (upt.column1 <> n.column1
or upt.column2 <> n.column2
or upt.column3 <> n.column3)
How I can change this query to not update not null fields null?
I'm just starting with SQL with no training but the job suddenly requires it. So thank you in advance for any help.
Let's say I have a query that returns 3 columns. Some of the cells in column 3 are empty and I would like to fill them in with values based on column1.
example:
CASE column1 = 'Individual' then Column3 should show 'Individual' not empty, but if column1 = 'group' them column3 needs to show "group" else no change.
SELECT column1, column2, column3,
CASE
WHEN column1 = 'Individual' THEN Column3 = 'Individual'
WHEN column1 = "Group' THEN comlumn3 = 'Group'
END
FROM tablename
If you only want to select data and not change the table values you can use case as you tried:
SELECT column1, column2,
CASE
WHEN column3 IS NULL THEN Column1
ELSE column3
END as column3
FROM tablename
I want to count some columns from a table in PostgreSQL.
For each column count I have some conditions and I want to have all in one query. My problem is the fact that I don't get the expected results in counting because I tried to apply all the conditions for the entire data set.
The table:
column1
column2
column3
UUID10
UUID20
UUID30
NULL
UUID21
NULL
NULL
UUID22
UUID31
UUID11
UUID20
UUID30
This is what I tried so far:
SELECT
COUNT(DISTINCT column1) AS column1_count,
COUNT(DISTINCT column2) AS column2_count,
COUNT(DISTINCT column3) AS column3_count
FROM TABLE
WHERE
column2 IN ('UUID20', 'UUID21', 'UUID22')
AND column1 = 'UUID10' -> this condition should be removed from this where clause
OR column3 IN ('UUID30', 'UUID31')
Result:
column1_count
column2_count
coumn3_count
2
3
2
The result in not correct because I should have column1_count = 1. I mean, this is what the query does, but is not what I intended. So I thought to have some constrains for column2 and column3 in a subquery, and having a another condition just for column1.
A second try:
SELECT *
FROM
(
SELECT
column1
column2,
column3
FROM TABLE
WHERE
column2 IN ('UUID20', 'UUID21', 'UUID22')
OR column3 IN ('UUID30', 'UUID31')
) x
WHERE
column1 = 'UUID10'
Result:
column1_count
column2_count
coumn3_count
1
1
1
Because the last condition on column1 is restricting my result, I end up having 1 for all the counts.
How can I apply different conditions for counting each column?
I would try not to use UNION if is possible. Maybe there can be made some subqueries in another way than what I tried so far. I just have to find a way for the constraint for the column1, to not be on the same WHEN clause as for the column2 and column3.
I think you want conditional aggregation:
SELECT COUNT(DISTINCT CASE WHEN column1 = 'UUID10' THEN column1 END) AS column1_count,
COUNT(DISTINCT column2) AS column2_count,
COUNT(DISTINCT column3) AS coumn3_count
FROM TABLE
WHERE column2 IN ('UUID20', 'UUID21', 'UUID22') OR
column3 IN ('UUID30', 'UUID31');
I assume that you are aware that COUNT(DISTINCT CASE WHEN column1 = 'UUID10' THEN column1 END) is not particularly useful code. It returns 1 or 0 depending on whether the value is present. I assume your code is actually more interesting.
I have two columns, each with identification numbers that have been brought in from different datasheets.
I want to combine this into one column with both identification numbers if they are different, but only one of the identification numbers if they are the same.
I'm using SELECT DISTINCT CONCAT(column 1, column 2) AS column 3 to combine the columns, but can not filter out UNIQUE combinations.
When I try WHERE column 1 <> column 2, I get an error message.
Any suggestions?
You can use CASE WHEN to test for conditions:
SELECT DISTINCT CASE WHEN column1 = column2 THEN column1
ELSE CONCAT(column1, column2)
END AS column3
FROM table1
try this using IIF or CASE and CONCAT
select
distinct
iif(col1<>col2,concat(col1,col2),col1) [myid]
from mytable
or
select
distinct
case when col1<>col2 then
concat(col1,col2)
else col1 end [myid]
from mytable
You should do something like:
SELECT DISTINCT CASE WHEN column1 = column2 THEN column1
ELSE column1 + '|' + column2
END AS combinedColumn
FROM table1
Consider the following chart:
column1 column2 column1+column2 column1+'|'+column2
12 34 1234 12|34
123 4 1234 123|4
1234 1234 1234 1234
Also, column1+column2 loses some information - what the original parts were.
I'm trying to use the BETWEEN with column names instead of direct values, something like this:
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3;
This is returning something like 17 rows, but if i write:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
i get around 600 rows..
In both cases i only get rows where column1 value is actually the middle value, but 2nd method gives me much more results, so 1st method has something wrong with it.
I suspect the problem might be on using BETWEEN clause with column names, instead of pure values, and somehow SQL is converting the column names to actual values..its strange, but can someone enlighten me please?
Thanks
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3; # gives 17 rows
is same as
SELECT * FROM table WHERE (column1 >= column2 AND column1 <= column3) # gives 17 rows
Because of your addition check of
(column1 <= column2 AND column1 >= column3)
which is ORed, you get additional rows.
Between A And B assumes that A<B, i.e., that the first expression in the Between, (A), is less than the second expression, (B) it does not check or execute with the opposite option.
e.g., if you put Where 3 Between 4 And 2 no rows will be returned:
or, if you write
Select Case When 3 Between 4 and 2 then 'true' else 'false' end
it will return false
Your logic for the two statements is not the same:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
Has two clauses. Remove the first and you should have the same results as your between statement.
SELECT * FROM table WHERE (column1 >= column2 AND column1 <= column3)