Update data from the same table for 235 rows - sql

I need help updating rows to equal to another set of rows for the same table for example:
M005E globalpickesequense = 6627,
globalallocationsequense = 7080,
globalputawaysequence = 4268
so these numbers need to equal the same numbers as the M005D 7607,8068,5256.
M006E same thing needs to equal M007D globals.
and so forth...
I have to do this update for a total of 235 rows but in the image I am just adding part of rows in the database.
So if this possible to do? a query that can update all at the same time without updating row by row individually
I have been using this query where it works but I have do one by one changing the ids so that would take me too much time for all my 235 row I need a query that can do the update all at the same time
UPDATE lc1
SET lc1.globalPickSequence = lc2.globalPickSequence,
lc1.globalAllocationSequence = lc2.globalAllocationSequence,
lc1.globalPutAwaySequence = lc2.globalPutAwaySequence
from mytable lc1
JOIN mytable lc2 ON lc1.id = 27234 AND lc2.id = 16358

Related

SQL Server: Selecting Specific Records From a Table with Duplicate Records (Excluding Stale Data from a Query)

I'm trying to put together a query (select preferably) in SQL server that works with a single table. Said table is derived from two sets of data. Records where SET = OLD represent old data, records where SET = NEW represent new data. My intention is as follows:
If record CODE = A, keep/include the record.
If record CODE = C, keep/include the record but delete/exclude the corresponding record from the old set under the same ACT value.
If record CODE = D, delete/exclude it along with its corresponding record from the old set under the same ACT value.
If CODE = '' (blank/null), keep the record but only if it exists in the OLD set (meaning their isn't a corresponding record from the new set with the same ACT value)
What the table looks like before logic is applied:
ACT|STATUS |CODE|SET|VALUE
222| | |OLD|1
333| | |OLD|2
444| | |OLD|3
111|ADDED |A |NEW|4
222|CHANGED|C |NEW|5
333|DELETED|D |NEW|6
What the table should look like after logic is applied (end result)
ACT|STATUS |CODE|SET|VALUE
444| | |OLD|3
111|ADDED |A |NEW|4
222|CHANGED|C |NEW|5
While I can probably put together a select query to achieve the end result above I doubt it will run efficiently as the table in question has millions of records. What is the best way to do this without taking a long time to obtain the end result?
Something like this. you will have to split your query and union.
--Old Dataset
SELECT O.*
FROM MyTable O
LEFT JOIN Mytable N ON O.ACT = N.ACT AND N.[SET] = 'NEW'
WHERE O.[SET] ='OLD'
AND ISNULL(N.CODE,'A') = 'A'
UNION
-- New records
SELECT N.*
FROM MyTable N
WHERE N.[SET] ='NEW'
AND CODE <> 'D'

increasing speed of sql update postgresql

I'm performing the 2 below queries on my database, and I'm trying to figure out how to make it faster.
The first query takes 208796.8ms. The second one takes 611654.9ms. I'm not sure there is a way to make them faster. I need these updates to be in the same transaction, so I'm also not sure if the update by batches of n records would be faster. I will take any idea !
UPDATE ticket_memberships AS my_table
SET ticket_id = foreign_table.id
FROM tickets AS foreign_table
WHERE my_table.agency_id = 2
AND foreign_table.agency_id = 2
AND my_table.ticket_id IS NOT NULL
AND my_table.ticket_id = foreign_table.old_id
UPDATE ticket_memberships AS my_table
SET person_contact_id = foreign_table.id
FROM person_contacts AS foreign_table
WHERE my_table.agency_id = 2
AND foreign_table.agency_id = 2
AND my_table.person_contact_id IS NOT NULL
AND my_table.person_contact_id = foreign_table.old_id

Updating multiple SQL records from x to x in a single query

I have 30+ records that need to be updated to have some of the same billing information. I don't want to have to type in every single number from 1200-1237. Is there an easy way to update all these records without copy/paste and altering the numbers? Possibly use a count?
EX:
UPDATE LakeEncroachments
SET PERMITEE = 3304, BILLTOPERSON = 3304,
LakeEncroachments.InvoiceBillTo = 1799
WHERE LakeEncroachments.EN_ID = 1200, LakeEncroachments.EN_ID = 1201, LakeEncroachments.EN_ID = 12xx
Provided EN_ID is numeric
UPDATE LakeEncroachments
SET
PERMITEE = 3304, BILLTOPERSON = 3304, LakeEncroachments.InvoiceBillTo = 1799
WHERE
LakeEncroachments.EN_ID BETWEEN 1200 AND 12037

update column with numbers in sqlite table

This should be very simple but I cannot figure out how to do it. I would like to modify the values of two different columns. One from 1 to the total number of rows and the other one from the total of rows to one (basically increasing and decreasing number). I tried:
start = 0
end = number_of_rows + 1
c.execute('SELECT * FROM tablename')
newresult=c.fetchall()
for row in newresult:
start += 1
end -= 1
t = (start,)
u = (end,)
c.execute("UPDATE tablename SET Z_PK = ?", t) ---> this will transform all rows with Z_PK since there is no where statement to limit
c.execute("UPDATE tablename SET Z_OPT = ?", u)
The thing is that I don't know how I can add the "where" statement since I have no values I am sure for rows (like IDs number). A possibility would be to return the current row as the argument for "where" but I don't know how to do it...
Without an INTEGER PRIMARY KEY column, your table has an internal rowid column, which already contains the values you want:
UPDATE MyTable
SET Z_PK = rowid,
Z_OPT = (SELECT COUNT(*) FROM MyTable) + 1 - rowid

Creating filter with SQL queries

I am trying to create a filter with SQL queries but am having trouble with numeric values linking to other tables.
Every time I try to link to another table, it takes the same record and repeats it for every element in the other table.
For example, here is query:
SELECT ELEMENTS.RID,TAXONOMIES.SHORT_DESCRIPTION,[type],ELEMENT_NAME,ELEMENT_ID,SUBSTITUTION_GROUPS.DESCRIPTION,namespace_prefix,datatype_localname
FROM ELEMENTS,SUBSTITUTION_GROUPS,TAXONOMIES,SCHEMAS,DATA_TYPES
WHERE ELEMENTS.TAXONOMY_ID = TAXONOMIES.RID AND ELEMENTS.ELEMENT_SCHEMA_ID = SCHEMAS.RID AND
ELEMENTS.DATA_TYPE_ID = DATA_TYPES.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = 0
The last line is the actual filtering criteria.
Here is an example result:
There should only be ONE result (Item has an RID of 0). But it's repeating a copy of the one record for every result inside the substitution groups table (there's 4).
Here is my database schema for reference. The lines indicate relationships between tables and the circles indicate the values I want:
You're forgot to join between ELEMENTS and SUBSTITUTION_GROUPS in your query.
SELECT
ELEMENTS.RID,TAXONOMIES.SHORT_DESCRIPTION,[type],ELEMENT_NAME,ELEMENT_ID,SUBSTITUTION_GROUPS.DESCRIPTION,namespace_prefix,datatype_localname
FROM
ELEMENTS,SUBSTITUTION_GROUPS,TAXONOMIES,SCHEMAS,DATA_TYPES
WHERE
ELEMENTS.TAXONOMY_ID = TAXONOMIES.RID AND ELEMENTS.ELEMENT_SCHEMA_ID = SCHEMAS.RID
AND ELEMENTS.DATA_TYPE_ID = DATA_TYPES.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = SUBSTITUTION_GROUPS.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = 0