Updating two values, with a second value having a where clause - sql

This may be pointless but I want in ONE query to only the second value when Eligible equals 1, but always update the first value. So if the eligible is already 0 (or something else), don't update eligible. Can I do this in one query?
---Looping through this
UPDATE myTable p
SET p.first= 'C', p.eligible = 0
WHERE id = l_modifier_row_a.id
Desired Results
BEFORE
ID First Eligible
1 A 1
2 B 2
AFTER
ID First Eligible
1 C 0
2 C 2

In Oracle, you can use exists:
UPDATE myTable p
SET p.first = 'C',
p.eligible = 0
WHERE EXISTS (SELECT 1
FROM l_modifier_row_a l
WHERE p.id = l.id AND p.person_id = l.person_id
);

You cannot set a multiple column values sometime but not in others, in a single statement the columns are ALWAYS the same. However, you can conditionally set the value of a column to the existing value or change that value.
update mytable p
set first= 'C'
, eligible = case when p.eligible = 1
then 0
else p.eligible
end
where id = l_modifier_row_a.id ;
This might be doable in a single statement without a loop. But you did not post the loop control so I cannot look further.

Related

SQL CASE statement only updates one value froom join table

I am trying to do a case statement to update multiple columns, but only the first value from my join table AS 'b' is getting picked up. Below I have EDLCode = 1 and this value gets picked up. If I set the EDLCode = 2, it does not update anything. I can separate the updates into it's own blocks and those update correctly, but I was hoping to combine and make this code a little more condense. Any ideas?
UPDATE mainTable
SET
col_one = CASE WHEN EDLCode = 1 THEN b.Amount END,
col_two = CASE WHEN EDLCode = 2 THEN b.Amount END
FROM #mainDataTable AS a
INNER JOIN #deductLiabData AS b
ON a.Employee = b.Employee
WHERE b.EDLType = 'D'
Result would be:
EDLCode Amount
1 100
2 200
col_one col_two
100 null

how to properly merge these 2 query into one update?

This currently work but I would like to change the update statement to include the action of the insert below it, is it posssible?
UPDATE cas
SET [Locked] = CASE WHEN cas.Locked <> #TargetState AND cas.LastChanged = filter.SourceDateTime THEN #TargetState ELSE cas.[Locked] end,
OUTPUT inserted.Id, inserted.Locked, CASE WHEN inserted.Locked = #TargetState AND
inserted.LastChanged = filter.SourceDateTime THEN 1
WHEN inserted.LastChanged <> filter.SourceDateTime THEN -1 -- out of sync
WHEN deleted.Locked = #TargetState THEN -2 -- was not in a good state
ELSE 0 END --generic failure
INTO #OUTPUT
FROM dbo.Target cas WITH(READPAST, UPDLOCK, ROWLOCK) INNER JOIN #table filter ON cas.Id = filter.Id
INSERT INTO #OUTPUT
SELECT filter.id, NULL, when cas.id is not null -3 -- row was/is locked
else -4 end --not found
FROM #table filter left join dbo.target cas with(nolock) on filter.id = cas.id
WHERE NOT EXISTS (SELECT 1 FROM #OUTPUT result WHERE filter.id = result.UpdatedId)
I do not think what you want is possible.
You start with a table to be updated. Let’s say this table contains a set of IDs, say, 1 to 6
You join onto a temp table containing a different set of IDs that may partially overlap (say, 4 to 9)
You issue the update using an inner join. Only rows 4 to 6 are updated
The output clause picks up data only for modified rows, so you only get data for rows 4 to 6
If you flipped this to an outer join (such that all temp table rows are selected), you still only update rows 4 to 6, and the output clause still only kicks out data for rows 4 to 6
So, no, I see no way of achieving this goal in a single SQL statement.

T-SQL Query to check if related col is true and update another table col

I have this 2 tables SupplierOrder and SupplierOrderDetails which are linked by SupplierOrder PK. Now I have this col called isComplete in the SupplierOrder table which I want o update to true once all the values in the SupplierORderDetails table's isComplete are all true for that supplierOrder ID. Please see the attachment for the tables. I have tried myself with this query but I think it could be a better way or more efficient.
SELECT 1
FROM supplierOrder so
inner JOIN supplierOrderdetails sod
ON so.id = sod.supplierOrderID
WHERE so.id = 1
AND sod.isComplete= 1
This should work, I maynot have correct table names but this should work if u change it
UPDATE suplierorder
SET iscomplete = 'true'
WHERE id IN (SELECT suplierorderid
FROM (SELECT suplierorderid,
--case statement to set to 0 if complete and 1 if not complete (i.e any other value null or false)
Sum(CASE
WHEN iscomplete = 'true' THEN 0
ELSE 1
END) AS complete
FROM suplierorderdetails
--make sure we only update the new ones and makes sure that your select records are limited to Just not complete records, so if your tables grow this will make your update statement doesn't take a lot of time
WHERE suplierorderid IN (SELECT id
FROM suplierorder
WHERE iscomplete IS NULL)
--I am grouping on suplierorderid so that we can add all the iscomplete status of each suplierorderid column
GROUP BY suplierorderid) A
--now that the inner query outputs suplierorderid and complete status which will be 0 if everything is complete we are writing below condition
WHERE complete = 0)
All we need is to find supplierOrderID where MIN(isComplete)=1. So it means that ALL isComplete=TRUE
UPDATE supplierOrder SET isComplete=1
WHERE id in
(
SELECT supplierOrderID
FROM supplierOrderdetails
GROUP BY supplierOrderID
HAVING MIN(CAST(isComplete as Int))=1
)
AND
(
(isComplete is NULL ) OR (isComplete = 0)
)
SQLFiddle demo
PS: Since isComplete is a BIT type field you can't use MIN(isComplete) but you can use MIN(CAST(isComplete as Int))

Selective update in SQL Server

I've created a junction table like this one:
http://imageshack.us/scaled/landing/822/kantotype.png
I was trying to figure out a query that could able to select some rows - based on the PokémonID - and then updating only the first or second row after the major "filtering".
For example:
Let's suppose that I would like to change the value of the TypeID from the second row containing PokémonID = 2. I cannot simply use UPDATE KantoType SET TypeID = x WHERE PokémonID = 2, because it will change both rows!
I've already tried to use subqueries containing IN,EXISTS and LIMIT, but with no success.
Its unclear what are your trying to do. However, you can UPDATE with JOIN like so:
UPDATE
SET k1.TypeID = 'somethng' -- or some value from k2
FROM KantoType k1
INNER JOIN
(
Some filtering and selecting
) k2 ON k1.PokémonID = k2.PokémonID
WHERE k1.PokémonID = 2;
Or: if you want to UPDATE only the two rows that have PokémonID = 2 you can do this:
WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY TypeID) rownum
FROM KantoType
WHERE PokemonID = 2
)
UPDATE c
SET c.TypeID = 5
FROM CTE c
WHERE c.rownum = 1;
SQL Fiddle Demo
I can suggest something like this if you just need to update a single line in your table:
UPDATE kantotype
SET
type = 2
WHERE pokemon = 2
AND NOT EXISTS (SELECT * FROM kantotype k2
WHERE kantotype.type > k2.type
AND kantotype.pokemon = k2.pokemon)
It would be easier to get the first or last item of the table if you had unique identifier field in your table.
Not sure even if you are trying to update the row with PokemenID =2 by doing a major filtering on TypeID... So just out of assumptiong (big one), you can give a try on Case
UPDATE yourtable a
LEFT JOIN youtable b on a.pokeid = b.pokeid
SET a.typeid = (CASE
WHEN a.typeid < b.typeid THEN yourupdatevalue
WHEN a.typeid > b.typeid THEN someothervalue
ELSE a.typeid END);
If you know the pokemon ID and the type id then just add both to the where clause of your query.
UPDATE KantoType
SET TypeID = x
WHERE PokémonID = 2
AND TypeID=1
If you don't know the type ID, then you need to provide more information about what you're trying to accomplish. It's not clear why you don't have this information.
Perhaps think about what is the unique identifier in your data set.

Update query for a specfic columns in all rows

I'm searching for check a columns value in all the rows and update with a where clause as a condition. My case is as follows:
SubscriptionID ChannelURI StudentID
1 XXXX 4
2 yyyy 4
3 XXXX 3
4 XXXX 4
5 XXXX 2
I want to check the column channel uri value for a specfic student and for all matched results to set it to null.
So in this case row 3 and 5 should be set to null.
I've tried this, but it set all channeluri of other rows than studnetid = 4 to null
UPDATE SubscriptionCourse
Set ChannelURI = 1
, DeviceId = null
FROM SubscriptionCourse as t1
INNER JOIN SubscriptionCourse as t2
on t1.ChannelURI = t2.ChannelURI
WHERE StudentId! = 4
Reference the table to be updated by it's alias given in the FROM clause, rather than by name (since the same table name is referenced twice. Also qualify the reference on StudentId in the WHERE clause with the table alias as well.
UPDATE t1
SET t1.ChannelURI = 1
, t1.DeviceId = NULL
FROM SubscriptionCourse t1
JOIN SubscriptionCourse t2
ON t1.ChannelURI = t2.ChannelURI
WHERE t1.StudentId != 4
You say you want to set ChannelURI to NULL, but your statement is setting to a literal value of 1. I've left the assignment as you specified in your statement, but qualified the columns with the table alias.
I don't think this is your problem, but I never include a space in the "not equals" comparison operator symbol (!=). I've just never seen that before. I prefer to use the <> symbol for the "not equals" comparison operator.
From your description of the problem and your example, it's not at all clear why you need to join the table to itself.
I recommend you FIRST write a SELECT statement that returns the rows you want to update, by replacing the UPDATE and SET clauses with a SELECT <expression_list> clause, with the expression_list including the value of the primary key column(s), the column you want to update, and any other columns you want to check. Once that SELECT is returning the rows you want to update, then convert it into an UPDATE statement.
update [table_name] set channelURI = "null" where SubscriptionID = 3 or SubscriptionID =5
in this case,SubscriptionID must be a primary key.
Update table set channeluri = null where studentid <> 4
That what you want? Or you want to find all the ones that have the same uri as student 4 and null those?
Update table set channeluri = null from table inner join table t2 on table.channeluri =t2.channeluri where table.studentid <> 4 and t2.studentid =4
Something like that, im on my phone, the wife has stolen the pc