Ive got a column that is set up like so:
-----Col_2-----
Value 1
Value 2
-----------------
As you can see there are 2 values within the column, but there are also many spaces as well. What I would like to do is run an Update query that would replace the blanks with the 2 values. What I would like is to update the column to look like this:
-------Col_2---------
Value 1
Value 1
Value 1
Value 2
Value 2
value 2
---------------------
Basically I want to run a query that looks at the first not null value and replaces the following null values with that non null value until it hits the next non null value. Once it hits the next non null value, it then proceeds to replace the following nulls with the new value. Currently, I've got this set up.
Update Table1,
(Select TOP 1 Col_1 AS Z FROM Table1 Where Col_1 Is Not Null)
Set Col_1 = Z
Where Col_1 Is Null;
This replaces every null value with whatever the first non null value is, but doesn't stop once it hits the next non null value. This results in me getting something like this:
---------Col_2-----------
Value 1
Value 1
Value 1
Value 2
Value 1
Value 1
-------------------------
Is there a way to add some sort of "do this until" this statement into access? I know you can run if, then statements in PHP and other languages, but not sure how to do this in access or straight SQL.
UPDATE:
There is a second column that contains account numbers, which correlate to the values in COL_1. The original set up looks like this.
-----Col_1-------Col_2------
12345 Value 1
12345
12345
54321 Value 2
54321
54321
------------------------------
Is there a way to use Col_1 in order to get Col_2 to look like:
--------Col_2---------
Value 1
Value 1
Value 1
Value 2
Value 2
Value 2
----------------------
without having to manually enter Col_1's numbers into the query? By this I mean, I don't want to have to manually enter "12345" or "54321" in the query, but rather have things happen automatically.
Assuming I'm understanding your question correctly, you can use an update statement, joining the table back to itself. I think this is the correct syntax in Access:
update Table1
inner join Table1 t2 on Table1.col1 = t2.col1 and t2.col2 is not null
set Table1.col2 = t2.col2
SQL Fiddle Demo
One way of doing it:
UPDATE SomeData SDOuter
SET Col_2 = (SELECT MAX(SDInner.Col_2) FROM SomeData SDInner WHERE SDInner.Col_1 = SDOuter.Col_1 AND Col_2 IS NOT NULL)
WHERE Col_2 IS NULL;
SQL Fiddle
Related
I'm creating a new table and carrying over several columns from a previous table. One of the new fields that I need to create is a flag that will have values 0 or 1 and value needs to be determined based on 6 previous fields in the table.
The 6 previous columns have preexisting values of 0 or 1 stored for each one. This new field needs to check whether any of the 6 columns have 1 and if so set the flag to 0. If there is 0 in all 6 fields then set itself to 1.
Hopefully this makes sense. How can I get this done in oracle? I assume a case statement and some sort of forloop?
You can use greatest() function: GREATEST
create table t_new
as
select
case when greatest(c1,c2,c3,c4,c5,c6)=1 -- at least one of them contains 1
then 0
else 1
end c_new
from t_old;
Or even shorter:
create table t_new
as
select
1-greatest(c1,c2,c3,c4,c5,c6) as c_new
from t_old;
In case of greatest = 1, (1-1)=0, otherwise (1-0)=1
You can use a virtual column with a case expression; something like:
flag number generated always as (
case when val_1 + val_2 + val_3 + val_4 + val_5 + val_6 = 0 then 1 else 0 end
) virtual
db<>fiddle
or the same thing with greatest() as #Sayan suggested.
Using a virtual column means the flag will be right for newly-inserted rows, and if any of the other values are updated; you won't have to recalculate or update the flag column manually.
I've assumed the other six columns can't be null and are constrained to only be 0 or 1, as the question suggests. If they can be null you can add nvl() or coalesce() to each term in the calculation.
id value
1 a
2 b
3 c
How do i add second value 'z' to id=1 (separated by comma)?
id value
1 a,z
2 b
3 c
and how to remove the 'z' now if i have that final table?
You can use update:
update t
set value = concat(value, ',z')
where id = 1;
To answer your secondary question, yes.
If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.
The best and simplest way to do this is the following query according to me :
update table1 set value = concat(value,'z') where id = 1
where : Table1 is the name of your table.
SELECT * FROM hr.NullValueCheck
ID Name
1 abc
2 abc
3 bcd
4 cde
https://oracle-base.com/articles/misc/all-any-some-comparison-conditions-in-sql
Query 1 :
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT NULL FROM DUAL )
Nothing is coming.
But for below quesry. All records are coming while subquesry is returning is NULL same as like above query (SELECT NULL FROM DUAL )
Query 2:
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT id from hr.NullValueCheck where id = 5)
Please explain me why Query 1 is returning No Records but Query 2 is returning all records.
As per my knowledge Query 1 should also return all records.
NULL is different from an empty set.
The first example is saying: "select all rows where the id is greater than all values of NULL". Or more simply, "where id is greater than 'NULL'`.
In SQL, 'NULL' generally has the semantics of "not known". If you don't know the value, then you don't know if a given id is larger. Hence, no rows are returned.
In the second example, instead has an empty set for comparison. An empty set is not NULL. Obviously, any number is greater than all numbers in an empty set. Hence, all rows are returned.
So I found these 2 articles but they don't quite answer my question...
Find max value and show corresponding value from different field in SQL server
Find max value and show corresponding value from different field in MS Access
I have a table like this...
ID Type Date
1 Initial 1/5/15
1 Periodic 3/5/15
2 Initial 2/5/15
3 Initial 1/10/15
3 Periodic 3/6/15
4
5 Initial 3/8/15
I need to get all of the ID numbers that are "Periodic" or NULL and corresponding date. So I want a to get query results that looks like this...
ID Type Date
1 Periodic 3/5/15
3 Periodic 3/6/15
4
I've tried
select id, type, date1
from Table1 as t
where type in (select type
from Table1 as t2
where ((t2.type) Is Null) or "" or ("periodic"));
But this doesn't work... From what I've read about NULL you can't compare null values...
Why in SQL NULL can't match with NULL?
So I tried
SELECT id, type, date1
FROM Table1 AS t
WHERE type in (select type
from Table1 as t2
where ((t.Type)<>"Initial"));
But this doesn't give me the ID of 4...
Any suggestions?
Unless I'm missing something, you just want:
select id, type, date1
from Table1 as t
where (t.type Is Null) or (t.type = "") or (t.type = "periodic");
The or applies to boolean expressions, not to values being compared.
I have a little problem.
I'm SELECTING From a table. I want to see a value what is just used only in one point of my workflow. Before that no value here.
But, if no result, then my "result" is NULL. And thats not good for me.
I mean, if I write
SELECT myValue FROM myTable WHERE asd = 'thisIs'
when myValue is NULL in the table, then my result is NULL. Thats okay.
when no results found, then my result is NULL too. Thats not okay.
If no result I have do different function.
How can I separate these ?
|MyValue|
---|-------|
1 |NULL |
---|-------|
|MyValue|
---|-------|
|MyValue|
---|-------|
1 |Hello |
---|-------|
In the first and third case I have to call an Update function, in the second case I have to call an Insert function.
But the result what my SP is sending back in the first and second case is NULL too.
Thank you for your help in advance.
As I understand you want to do something like this:
IF EXISTS (
SELECT myValue FROM myTable WHERE asd = 'thisIs'
)
BEGIN
SELECT myValue FROM myTable WHERE asd = 'thisIs'
END
ELSE
BEGIN
EXEC SomeOtherCode
END
You can count number of rows found after select records query. if count is greater or equal to one you have to run update query else insert query as selected records are empty.