I have the following table
id,link_id,url,type,download,filename
44,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
45,11,https://google.com,extra,0,53bfa01d-91d0-4b84-9389-b06e5e4ef618.jpg
46,11,https://google.com,extra,0,364cfdc2-c0b6-43fc-8936-33e49896014a.jpg
47,12,https://google.com,extra,0,9d26efbd-e6e0-42df-bde0-04c05babffe4.jpg
48,13,https://yahoo.com,extra,0,2d58b9f7-1860-40d8-88f0-9fc08cd7275f.jpg
49,13,https://yahoo.com,extra,0,574b1646-6316-4a4b-8e28-56c38c0999b9.jpg
...
I want to write a query to check download=1 and update all the rows with the same url and downoload=0 to set the same filename and download=1
so the output would be
44,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
45,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
46,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
47,12,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
48,13,https://yahoo.com,extra,0,2d58b9f7-1860-40d8-88f0-9fc08cd7275f.jpg
49,13,https://yahoo,extra,0,574b1646-6316-4a4b-8e28-56c38c0999b9.jpg
demo:db<>fiddle
UPDATE mytable t
SET download = s.download, filename = s.filename -- 3
FROM (
SELECT url, download, filename -- 1
FROM mytable
WHERE download = 1
) s
WHERE t.url = s.url -- 2
Find the expected record with download = 1
Find all related url records
Update the columns with the data fetched in step 1
Related
We are running ECC6 on HANA, currently the extraction of RESB does a full table scan. I want to record that a change has happened to a Z table (I don't care what change).
I've attempted to create the triggers (in Word) from Help & Google, I would appreciate any suggestions for improvement.
create trigger tr_RESB_insert
after insert on RESB
referencing new row NR
for each row
begin
insert into ZBW_RESB_CDC (
MANDT
,RSNUM
,RSPOS
,RSART
,CHNGIND
,AEDAT
,AEZEIT
,AETMSTMP
,TZONE
,DELFLG )
values ( NR.MANDT
, NR.RSNUM
, NR.RSPOS
, NR.RSART
, ‘I’
, current_utcdate
, current_utctime
, current_utctimestamp
, ‘UTC’
,’ ’
)
end;
and
create trigger tr_RESB_update
after update on RESB
referencing new row NR
for each row
begin
merge ZBW_RESB_CDC as T
using NR
on ( NR.MANDT = T.MANDT and
,NR.RSNUM = T.RSNUM and
,NR.RSPOS = T.RSPOS and
,NR.RSART = T.RSART )
when matched then update
set T.CHNGIND = ‘U’
,T.AEDAT = current_utcdate
,T.AEZEIT = current_utctime
,T.AETMSTMP = current_utctimestamp
,T.TZONE = ‘UTC’
,T.DELFLG = ‘ ’
when not matched then insert (
MANDT
,RSNUM
,RSPOS
,RSART
,CHNGIND
,AEDAT
,AEZEIT
,AETMSTMP
,TZONE
,DELFLG )
values ( NR.MANDT
,NR.RSNUM
,NR.RSPOS
,NR.RSART
, ‘U’
, current_utcdate
, current_utctime
, current_utctimestamp
, ‘UTC’
,’ ’
end;
similarly for delete
At some time we'll upgrade to S4 but for the moment I would like to improve what we have.
I chose to use MERGE so that I don't need to initialise the table and so I can delete old records. Nor do I want to keep images of every change, just the fact that it has changed.
Thanks,
G.
I am trying to update data in Table: local.import_payments from Table: local.payments based on update and Inner Join queries. The query I used:
Update local.import_payments
Set local.import_payments.client_id = local.payments.payment_for_client__record_id,
local.import_payments.client_name = local.payments.payment_for_client__company_name,
local.import_payments.customer_id = local.payments.customer__record_id,
local.import_payments.customer_name = local.payment_from_customer,
local.import_payments.payment_id = local.payments.payment_id
From local.import_payments
Inner Join local.payments
Where local.payments.copy_to_imported_payments = 'true'
The client_id, client_name, customer_id, customer_name in the local.import_payments need to get updated with the values from the table local.payments based on the condition that the field copy_to_imported_payments is checked.
I am getting a syntax error while executing the query. I tried a couple of things, but they did not work. Can anyone look over the queries and let me know where the issue is
Try the following
UPDATE local.import_payments
Set local.import_payments.client_id =
local.payments.payment_for_client__record_id,
local.import_payments.client_name =
local.payments.payment_for_client__company_name,
local.import_payments.customer_id = local.payments.customer__record_id,
local.import_payments.customer_name = local.payment_from_customer,
local.import_payments.payment_id = local.payments.payment_id
FROM local.payments as lpay
WHERE lpay.<<field>> = local.import_payments.<<field>>
AND local.payments.copy_to_imported_payments = 'true'
You shouldn't to specify the schema/table for updated columns, only column names:
Do not include the table's name in the specification of a target column — for example, UPDATE table_name SET table_name.col = 1 is invalid.
from the doc
You shouldn't to use the updating table in the from clause except of the case of self-join.
You can to make your query shorter using "column-list syntax".
update local.import_payments as target
set (
client_id,
client_name,
customer_id,
customer_name,
payment_id) = (
source.payment_for_client__record_id,
source.payment_for_client__company_name,
source.customer__record_id,
source.payment_from_customer,
source.payment_id)
from local.payments as source
where
<join condition> and
source.copy_to_imported_payments = 'true'
I have this issue where if there is no record in copy_on_write.id then the UPDATE listings SET images = (SELECT images FROM new_vals) runs and wipes out listings.images with nothing.
So, I am trying to use a condition to only run the UPDATE listings if copy_on_write.images exist.
right now I get:
psql:queries/copy-to-source.sh:20: ERROR: syntax error at or near "CASE"
LINE 10: CASE WHEN images <>
WITH
new_vals AS (
SELECT *
FROM copy_on_write
WHERE copy_on_write.posted_by = 102550922::text
AND copy_on_write.id = 4
),
updates AS (
SELECT images FROM new_vals,
CASE WHEN images <> ''
THEN UPDATE listings SET images = (SELECT images FROM new_vals)
END
)
SELECT internal_id FROM new_vals
You can use updates CTE like this:
...
updates AS (
UPDATE listings SET
images = new_vals.images
FROM new_vals
WHERE new_vals.images <> ''
)
....
Note, that:
Your new_vals CTE should always return maximum one record, otherwise this won't works correct.
Also this not updates listings table, if new_vals returns images column, but it is empty string (or null). If in such cases you need run update anyway, then remove WHERE new_vals.images <> '' at all.
And also, this statement will update all listings.images records. Do you really want this?
I have History table and transaction table.....and reference table...
If status in reference table is CLOSE then take those record verify in History table if not there insert from transaction table..... wiring query like this .... checking better one... please advice.. this query can be used for huge data ?
INSERT INTO LIB1.HIST_TBL
( SELECT R.ACCT, R.STATUS, R.DATE FROM
LIB2.HIST_TBL R JOIN LIB1.REF_TBL C
ON R.ACCT = C.ACCT WHERE C.STATUS = '5'
AND R.ACCT NOT IN
(SELECT ACTNO FROM LIB1.HIST_TBL)) ;
If you're on a current release of DB2 for i, take a look at the MERGE statement
MERGE INTO hist_tbl H
USING (SELECT * FROM ref_tbl R
WHERE r.status = 'S')
ON h.actno = r.actno
WHEN NOT MATCHED THEN
INSERT (actno,histcol2, histcol3) VALUES (r.actno,r.refcol2,r.refcol3)
--if needed
WHEN MATCHED
UPDATE SET (actno,histcol2, histcol3) = (r.actno,r.refcol2,r.refcol3)
I am trying to update records in a Sql Server 2012 table by using a MERGE statement. I have my target table and my source staging table. When the ID field matches I want to replace the entire row with the matching row from the source, and if not, add the row to the table as a new row. I don't know what to write after update set (?) to achieve this. here is my code:
USE DataCompany
MERGE INTO MasterAddress ma
USING MasterAddressStaging ms
ON MasterAddress.ID = MasterAddressStaging.ID
WHEN MATCHED THEN
UPDATE SET (?)
WHEN NOT MATCHED THEN
INSERT (ID
,CompanyName
,CompanyNumber
,RegAddress_CareOf
,RegAddress_POBox
,RegAddress_AddressLine1
,RegAddress_AddressLine2
,RegAddress_PostTown
,RegAddress_County
,RegAddress_Country
,RegAddress_PostCode)
VALUES (ms.ID
,ms.CompanyName
,ms.CompanyNumber
,ms.RegAddress_CareOf
,ms.RegAddress_POBox
,ms.RegAddress_AddressLine1
,ms.RegAddress_AddressLine2
,ms.RegAddress_PostTown
,ms.RegAddress_County
,ms.RegAddress_Country
,ms.RegAddress_PostCode);
thanks for your help
USE DataCompany
MERGE INTO MasterAddress ma
USING MasterAddressStaging ms
ON ma.ID = ms.ID
WHEN MATCHED THEN
UPDATE SET ma.CompanyName = ms.CompanyName,
ma.CompanyNumber = ms.CompanyNumber,
ma.RegAddress_CareOf =ms.RegAddress_CareOf,
ma.RegAddress_POBox =ms.RegAddress_POBox,
ma.RegAddress_AddressLine1 = ms.RegAddress_AddressLine1,
ma.RegAddress_AddressLine2 = ms.RegAddress_AddressLine2,
ma.RegAddress_PostTown = ms.RegAddress_PostTown,
ma.RegAddress_County = ms.RegAddress_County,
ma.RegAddress_Country = ms.RegAddress_Country,
ma.RegAddress_PostCode = ms.RegAddress_PostCode
WHEN NOT MATCHED THEN
INSERT (ID
,CompanyName
,CompanyNumber
,RegAddress_CareOf
,RegAddress_POBox
,RegAddress_AddressLine1
,RegAddress_AddressLine2
,RegAddress_PostTown
,RegAddress_County
,RegAddress_Country
,RegAddress_PostCode)
VALUES (ms.ID
,ms.CompanyName
,ms.CompanyNumber
,ms.RegAddress_CareOf
,ms.RegAddress_POBox
,ms.RegAddress_AddressLine1
,ms.RegAddress_AddressLine2
,ms.RegAddress_PostTown
,ms.RegAddress_County
,ms.RegAddress_Country
,ms.RegAddress_PostCode);
See examples here