SQL Syntax - Edit a column record - sql

I have a table called User. In the User table, there's a column called country. How do I write an SQL syntax to change all the user records whose country is "a" to country "b"?

This is a classic update statement:
UPDATE user
SET country = 'b' -- what to change
WHERE country = 'a' -- on which records

update "User" set "Country" = 'b' where "Country" = 'a'
PS: SQL is compatible with many backends. You didn't specify which one you have. That matters, because User maybe a keyword in some backends and/or casing might be significant.

UPDATE [User]
SET [country] = 'b' -- new value
WHERE [country]='a' -- previous value

Related

Query data from one column depending on other values on the table

So, I have a table table with three columns I am interested in : value, entity and field_entity.
There are other columns that are not important for this question. There are many different types of entity, and some of them can have the same field_entity, but those two columns determine what the column value refers to (if it is an id number for a person or the address or some other thing)
If I need the name of a person I would do something like this:
select value from table where entity = 'person' and field_entity = 'person_name';
My problem is I need to get a lot of different values (names, last names, address, documents, etc.), so the way I am doing it now is using a left join like this:
select
doc_type.value as doc_type,
doc.value as doc,
status.value as status
from
table doc
-- Get doc type
left join table doc_type
on doc.entity = doc_type.entity
and doc.transaction_id = doc_type.transaction_id
and doc_type.field_entity = 'document_type'
-- Get Status
left join table status
on doc.entity = status.entity
and doc.transaction_id = status.transaction_id
and status.field_entity = 'status'
where doc.entity = 'users' and doc.field_entity = 'document' and doc.transaction_id = 11111;
There are 16 values or more, and this can get a bit bulky and difficult to maintain, so I was wondering if some of you can point out a better way to do this?
Thanks!
I assume that you are not in position to alter the table structure, but can you add views to the database? If so, you can create views based on the different types of entities in your table.
For example:
CREATE VIEW view_person AS
SELECT value AS name
FROM doc
WHERE doc.entity = 'person'
AND doc.field_entity = 'name';
Then you can write clearer queries:
SELECT view_person.name FROM view_person;

finding int_ids that have more than one row with a flag set to Y

I have a table that can store multiple descriptions for each code. However there is a flag in that table that is to indicate which of those is the main or primary description. In some instances, we have codes that have more than one with this flag set to Y which is not correct.
I am having trouble coming up with the SQL to get all the rows in that table that have more than one description set to Y.
I've used this SQL to identify rows that do not have ANY dsp_fg = 'Y'
select *
from table A
where dsp_fg = 'N'
and not exists (select 1 FROM table where cod_int_id = A.cod_int_id AND dsp_fg = 'Y')
But I am having trouble writing the SQL to get me the cod_int_ids that have more than one Y record, can someone help?
SELECT int_id FROM A
WHERE dsp_fg = 'Y'
GROUP BY int_id
HAVING count(1) > 1
This is not perfect, but it identifies what I need.

How to update column based on filter of another column?

I'm trying to basically update a subset of rows in a table using a filter but currently my query is updating all values instead of the intended subset. I'm not really sure what I'm missing.
Here's what the current (non working) update looks like.
UPDATE account_type
SET type_id = 3
FROM my_filter;
I have been able to successfully select the data I want, I just can't figure out how to get the update to work. I am able to access the fields correctly with the following select statement.
SELECT account_type FROM my_filter;
But when I attempt to update, it updates every field. Here's what the (working) join I have looks like, wrapped in a CTE.
WITH my_filter AS
(
SELECT account_type.type_id, username
FROM account_type
INNER JOIN user ON user.account_id = account_type.account_id
WHERE username LIKE 'filter%'
)
You need a join condition. I would guess:
UPDATE account_type
SET type_id = 3
FROM my_filter
WHERE account_type.type_id = my_filter.type_id;
Alternative, you can write this as:
UPDATE account_type
SET type_id = 3
FROM user
WHERE user.account_id = account_type.account_id AND
user.username LIKE 'filter%';

update column only when it is null else dont update

Is there a way to update a column only when it is null and leave it as it is when it is not null in one multiple-columns-update query?
something like below. (like how we use case in select statements)
UPDATE users SET users.city = 'Dallas',
CASE
WHEN users.Global_id IS NULL
THEN
users.Global_id = '123'
END WHERE userid = '12312312'
The above update statement throws ORA-00927: missing equal sign error.
Reason why I am looking for this?
I have a schedule job that runs a similar query like above.
And there also exists a trigger in users table that will raise error if you try to update a 'not null' global_id. So my job fails when it encounters this trigger.
I have one option... to split this update query to two.. one to update city and one to update global_id where global_id is null.
But wondering if this can be achieved by any other way... Any idea would be appreciated.
There's a couple of options, but the closest one to your code is this (reformatted and with redundant content removed):
UPDATE users SET
city = 'Dallas',
Global_id =
CASE
WHEN Global_id IS NULL THEN '123'
ELSE Global_id
END
WHERE userid = '12312312';
However, this is equivalent to the more concise use of the coalesce() function:
...
Global_id = COALESCE(Global_id, '123')
...
You can use coalesce function:
UPDATE users
SET
users.city = 'Dallas',
users.Global_id = coalesce( users.Global_id, '123')
Also, you can split your update in two sentences, remember than you can enclose it in a single transaction:
UPDATE users
SET
users.city = 'Dallas';
UPDATE users
SET
users.Global_id = '123'
WHERE
users.Global_id is null
let say you have this column name is not updated city
(UPDATE users SET city = 'Dallas' WHERE Global_id = your_variable_of_user
AND city = "" ) // it means it null here.
the second sql of global id
(UPDATE users SET city = 'Dallas' WHERE Global_id = 0
AND city = "" ) // it means it null here.
user_id is your column name of the user id you use
do you mean global_id will be updated? because if global_id is 0 it means its guest its not logged in so how you will update it ?
but i advice you to use PDO .
hope it helps u

Updating a table by referencing another table

I have a table CustPurchase (name, purchase) and another table CustID (id, name).
I altered the CustPurchase table to have an id field. Now, I want to populate this newly created field by referencing the customer ids from the CustID table, using:
UPDATE CustPurchase
SET CustPurchase.id = CustID.id
WHERE CustPurchase.name = CustID.name;
I keep getting syntax errors!
I believe you are after the useful UPDATE FROM syntax.
UPDATE CustPurchase SET id = CI.id
FROM
CustPurchase CP
inner join CustID CI on (CI.name = CP.name)
This might have to be the following:
UPDATE CustPurchase SET id = CI.id
FROM
CustID CI
WHERE
CI.name = CustPurchase.name
Sorry, I'm away from my Postgres machine; however, based upon the reference, it looks like this is allowable. The trouble is whether or not to include the source table in the from_list.
Joining by name is not an ideal choice, but this should work:
UPDATE custpurchase
SET id = (SELECT c.id
FROM CUSTID c
WHERE c.name = custpurchase.name)
The caveat is that if there's no match, the value attempting to be inserted would be NULL. Assuming the id column won't allow NULL but will allow duplicate values:
UPDATE custpurchase
SET id = (SELECT COALESCE(c.id, -99)
FROM CUSTID c
WHERE c.name = custpurchase.name)
COALESCE will return the first non-NULL value. Making this a value outside of what you'd normally expect will make it easier to isolate such records & deal with appropriately.
Otherwise, you'll have to do the updating "by hand", on a name by name basis, to correct instances that SQL could not.