Up front, I'm in a DB class and could use a hint to get closer to the correct answer.
In the ticket_old table there is the first and last name of technicians. Only two unique names.
In the new ticket table, I've got a tech_id column which needs the int matching the last_name of the tech found in the ticket_old table.
I've been trying to do this using the code below, which executes successfully and updates 0 rows.
UPDATE ticket,ticket_old
SET tech_id = (CASE WHEN ticket_old.techLast = 'name1' THEN 1
WHEN ticket_old.techLast = 'name2' THEN 2
END)
;
-edit, I also tried the following which runs and updates 0 rows.
UPDATE ticket,
(SELECT techLast FROM ticket_old WHERE techLast = 'name1') as src
SET ticket.tech_id = 1;
When Comparing two values,
Always use Double Equal marks:
SET tech_id = (CASE WHEN ticket_old.techLast == 'name1' THEN 1
WHEN ticket_old.techLast == 'name2' THEN 2
END)
I'm not sure if it is 'name' or "name". Try it.
Related
Im programming a favourite function.
For example we have multiple adresses and can choose one as favourite.
At the moment i got 2 querys to do this job:
UPDATE userdata
SET maindata = 0
WHERE
cid = :id;
UPDATE userdata
SET maindata = 1
WHERE
cid = :id AND id = :id2
LIMIT 1
In the first query i make all adresses as "no favourite" and in the second one i make the new choosen adress the favourite one.
Is there any way to imrpove this query or rewrite both into 1 ? Or even a better solution ?
If you want a single query you could use a case when (or an if)
update userdata
set maindate = case when id = :id2 then 1
else 0 end
where cid = :id;
for performance Be sure you have a proper index on userdata columns (cid, id)
and the number of rows scanned should be the same for the first wuary ..but in this way you avoid the second ..
eventually try create a composite index
create index myidx1 on userdata(cid, id)
UPDATE userdata SET maindata = (case when cid = id AND id = id2 then 1 else 0 end);
This will help. I am not sure of your query but this will help. let me know if you looking something different...
UPDATE campaigns.list_name_counter SET counter = CASE WHEN name = 'occupant' THEN '2' WHEN name = 'occupant' THEN '3' WHEN name = 'Resident' THEN '3' WHEN name = 'Resident' THEN '4' WHEN name = 'Resident' THEN '5' END WHERE name IN ('occupant', 'occupant', 'Resident', 'Resident', 'Resident');
This table only has 3 columns. id, name, and counter. When it's done updating with the statement above, it only did the first occupant and the first Resident. It skipped the rest. If anything, if it only can do once per unique name, I would prefer it do the last of each, not the first of each, but anyway, should it update on each one, even if a name is repeated, or is that not allowed? and how can i make it update on the biggest counter number, not the smallest? Is my syntax messed up somewhere?
CASE statements do not "fall through" like they do in C; only the first match will be used; they are equivalent to "if...else if....else if...else if...."
I had a table with two thousands columns. i need to modify few columns data based on flag column.
tableSchemaRDD.registerAsTable("customer")
var results = sqlContext.sql("select *,case when flag1 = 'A' then null else charges end as charges, flag2 = 'B' then null then else stax end as stax from customer")
flag1,flag2, charges,stax are the columns from my table. the above code will give extra two coumns along with original columns. How can i get all columns with modified columns (charges,stax) based on flag columns.
Don't use asterisk ( * ), actually asterisk tells you have to bring all columns, after that you are using the two cases which are responsible for two new columns. You just have to remove asterisk ( * ) and place the column names comma separated without those column names which you are going to modify. In this way those two old columns will not be shown.
If you are using Spark 1.3 then its very easy for DataFrame, like
val columsNames = df.schema.fieldNames
.filter(fieldName => {
!fieldName.equals("charges") && !fieldName.equals("stax")
}).mkString(",")
Don't exactly remember that there are methods/properties in SchemaRDD or not.
EDITED:
Just understand the issue, asterisk tells bring all old columns, then you are using two new cases (two new columns.) also, where in your scenario you have to specify columns with their names without charges and stax, as these are your new columns, those will be populated by cases.
Suppose you have a table customer which has 4 columns, id name charges stax and you write the query like you are writing
select *,case when flag1 = 'A' then null else charges end as charges, flag2 = 'B' then null then else stax end as stax from customer
This will give you 6 columns, 4 for asterisk ( * ) as there are four columns in the table. and 2 for your cases. Instead of asterisk ( * ), you have to query like
select id , name ,case when flag1 = 'A' then null else charges end as charges, flag2 = 'B' then null then else stax end as stax from customer
This will result in 4 columns, id, name as they are (OLD). Stax and Charges (new) result of your cases.
Hopefully this will help.
So I have a table containing two boolean columns called "master" and "edition". I did not make this table, somebody else made it, the table is already filled with a lot of data and I cannot change it so this is what I have to work with.
Now this is needed:
I need to do a SELECT statement, where I combine the master and edition columns into a new column. The new column should contain either the letter 'm' (when master is 1), 'e' (when edition is 1) or 'u' (when both are 0). Now I have no clue how to do that. Could anybody help me with this?
Try this:
select
CASE
WHEN master = 0 and edition = 0 THEN 'u'
WHEN master = 1 and edition = 0 THEN 'm'
WHEN master = 0 and edition = 1 THEN 'e'
ELSE '???' -- when either are one???
END
from myTable
If you need to update the value of a new column, you could use this query:
UPDATE
tablename
SET
new = CASE WHEN NOT (master OR edition) THEN 'u'
ELSE
CONCAT_WS('',
CASE WHEN master THEN 'm' END,
CASE WHEN edition THEN 'e' END)
END
Please see fiddle here.
This will return u if both values are false, m if master is true, e if edition is true, me if they are both true.
There are multiple ways to do this. Using CASE is the simplest, there is another one:
if there are no rows with master=1 and edition=1, this will work:
select *, 'm' from table where master=1
union
select *, 'e' from table where edition=1
union
select *, 'u' from table where edition=0 and master=0
I'm trying to update two fields of several rows at once but I can't determine the right syntax to do so, except for doing so with one field update.
Each row is identified by an id, and therefore I'm using a CASE statement.
I have this table:
tbl_accounts(id_account, nation_id,
group_id)
Now, the following query works for updating only one field:
UPDATE tbl_accounts SET nation_id = CASE id_account
WHEN 3 THEN 333
WHEN 5 THEN 555
ELSE nation_id END
The above will update the nation_id field of each corresponding row identified by its id_account.
And the following query doesn't work for updating two fields - please suggest a fix to the syntax. I'm trying to avoid using any SELECT/JOIN/etc':
UPDATE tbl_accounts SET nation_id = CASE id_account, group_id = CASE id_account
WHEN 3 THEN 3331, 3332
WHEN 5 THEN 5551, 5552
ELSE nation_id, group_id END
I could run this as two separate statements but I'm sure there's a way to combine the two into one.
Any help is highly appriciated!
It sounds like you are looking for something like this:
UPDATE tbl_accounts
SET nation_id =
CASE id_account
WHEN 3 THEN 3331
WHEN 5 THEN 5551
ELSE nation_id
END,
group_id =
CASE id_account
WHEN 3 THEN 3332
WHEN 5 THEN 5552
ELSE group_id
END
But doing separate updates is a sensible solution in this situation. The above query will require checking every row in the table to see if it matches the condition. If you have an index on id_account (and presumably you do as it appears to be the primary key) then it will be very fast to update a single row.
UPDATE tbl_accounts SET nation_id = 3331, groupid = 3332 WHERE id_account = 3
UPDATE tbl_accounts SET nation_id = 5551, groupid = 5552 WHERE id_account = 5