MsAccess - How to update a column conditionally with different values - sql

I have a database in that a need to set different values in a column dependent upon values in a second column.
I wish to do this (descibed in not working code but I think you'll understand):
UPDATE new
(SET Domain = 'INFO'
WHERE new.node = 'ABC' or new.node = 'DEF')
or
(SET Domain = 'FOO'
WHERE new.node = 'GHI' or new.node = 'JKL')
but I can't figure out how. Can someone help me?
Regards // PS

Use the Switch statement.
UPDATE [new]
SET [Domain] = Switch(
[new].node = 'ABC' OR [new].node = 'DEF', 'INFO',
[new].node = 'GHI' OR [new].node = 'JKL', 'FOO',
);
Related link: Microsoft Access - Case Query

Related

Multi-value object search in CrateDB when this one is within an array of objects

I'm trying to migrate our current ES to CrateDB and one of the issues I'm facing is searching for two specific values within the same object when this object is part of an array of objects.
CREATE TABLE test.artefact (
id INTEGER,
metadata ARRAY(OBJECT(STATIC) AS (
key_id INTEGER,
value TEXT
))
);
insert into test.artefact(id, metadata) values (
1,
[
{
"key_id" = 1,
"value" = 'TEST1'
},
{
"key_id" = 2,
"value" = 'TEST2'
}
]
);
So basically, I'm trying to search metadata providing key_id and value.
A select like this one finds artefact 1 as a match, even when key and value are in different objects:
select * from test.artefact where 1 = ANY(metadata['key_id']) AND 'TEST2' = ANY(metadata['value'])
I have tried other functions, like UNNEST, with no luck.
Copy from CrateDB Community:
One way that should work is
SELECT *
FROM test.artefact
WHERE {key_id = 1, value = 'TEST2'} = ANY(metadata)
however this is probably not the most performant way.
together with the queries on the fields it might be quick enough.
SELECT *
FROM test.artefact
WHERE
1 = ANY(metadata['key_id'])
AND 'TEST2' = ANY(metadata['value'])
AND {key_id = 1, value = 'TEST2'} = ANY(metadata)

Display a formatted version of a returned query value

I am trying to figure out the best way to go about a problem I'm having with our Database. I don't have the option to go back and change the architecture, and so I am stuck trying to figure out a workaround.
Table 1:
User => Column MailingCountry = Either (en-Country(en-US), Country (US), or Null)
Table 3:
SMSCode => Column CountryCode = en-Country (en-US), Column SMSCode = Code (1 for US).
I'd like to know if there is a way that I could:
Get the User.MailingCountry and check if it is in the format of 'en-US', 'US' or 'NULL'
If it is in 'en-US' we are good, if it is in 'US', I'd like to replace it in the returned results to 'en-US', and if it is 'NULL', id like it to default to 'en-US'.
Inner JOIN with SMS.CountryCode where User.MailingCountry = SMS.CountryCode and then retrieve the SMSCode.
I guess the problem is, I am not sure how I can do something like
Select m.MailingCountry, a.SMSCode FROM User m->
IF (m.MailingCountry = 'US' or m.MailingCountry is null) Then m.MailingCountry = 'en-US'
Inner Join SMSCode a on a.CountryCode = m.MailingCountry
You can go for below query:
SELECT SMS.SMSCode
FROM
(SELECT CASE WHEN MailingCountry like '%US%' OR MailingCountry IS NULL THEN 'en-US'
ELSE MailingCountry END AS MailingCountry
FROM User) AS u
INNER JOIN SMSCode AS SMS
ON SMS.CountryCode = u.MailingCountry

Multiple Syntax Use

Please correct my SQL query for use in Oracle:
SELECT *
FROM GNGRB.PRBOOK
WHERE (SERIES = 'I' AND BOOKNO = '180922')
AND (SERIES = 'J' AND BOOKNO = '138446')
AND (SERIES = 'K' AND BOOKNO = '196491')
I got you. You want all records which have either of mentioned combinations.
As mentioned in other answer, use OR or you can use the IN as follows:
SELECT * FROM GNGRB.PRBOOK
WHERE (SERIES,BOOKNO) IN (('I','180922'), ('J', '138446'), ('K', '196491'))
Should you use the OR between the brackets? I am not sure about the use case, but looks like there will never be result from the query, as you are expecting multiple value in same column at same time.
SELECT *
FROM GNGRB.PRBOOK
WHERE (SERIES = 'I' AND BOOKNO = '180922')
OR (SERIES = 'J' AND BOOKNO = '138446')
OR (SERIES = 'K' AND BOOKNO = '196491')

Update two or more column to the same value SQL in a DRY manner

I want to do something like this to set foo and bar to the same value and I don't like to repeat the value, and not working.
update t1 set foo = bar = 10;
For now, I must do that in this way, this is working, but need a DRY way to do this:
update t1 set foo = 10, bar = 10;
Also tried but bar getting pervious value of foo:
update t1 set foo = 10, bar = foo;
Is this achievable in SQL (Also Postgres would be nice)?
I don't think there is anything in standard SQL that could do this.
But in Postgres, you can use an update from a VALUES clause:
update foo
set bar = t.value,
foo = t.value
from (
values (10)
) as t(value);
Online example
This logic:
update t1
set foo = bar = 10;
Does not do what you expect. It is parsed as:
update t1
set foo = (bar = 10);
The second part (bar = 10) is interpreted as a boolean expression, which returns true, false, or null.
Your second method is the correct way to do what you want.
You can also express this as:
update t1
set (foo, bar) = (10, 10)
However, I don't see any particular advantage to using this form.
Or, if you wanted to specify the value only once, you could use from:
update t1
set foo = v.val, bar = v.val
from (values (10)) v(val);

MS Access IF IIF statement TRUE set two fields to values

I am writing SQL query in MS Access. I came across a need for the following IF statement
IF (branch= 'TEST123') THEN (branch = '123' and subbranch='456')
ELSE branch = 'TEST0'
Looking in google, I only see the IIF statementmki
I am not sure if I can write my IF statement in IIF, correct? is there another way to do this?
I don't think I can write
iif( branch= 'TEST123', branch = '123' AND subbranch='456', 'TEST0')
Any help is appreciated.
Two ways:
UPDATE MyTable SET Branch = IIF([Branch] = 'TEST123','123',[Branch]),
SubBranch = IIF([Branch] = 'TEST123','456',[SubBranch ])
Or with just a simple WHERE
UPDATE MyTable SET Branch = '123', SubBranch = '456' WHERE Branch = 'TEST123'
2nd option is better.
try the below query
Update test set test.branch=iif(branch= "TEST123", "123", branch = "TEST0"), test.subbranch=iif(branch= "TEST123", "567", branch = "TEST0")