How can I use sql db2 update function? - sql

I have a sql db2 code that get information on a existing data table and I want to modify it to not download the data starting from 2020-02-01, instead just update this table everyday. Please help me to modify this script not to retrieve the whole data over and over again just to update but keeps on updating only the new data everyday.
create table public.fc_TDPMean_By_DIM2_EtchDate as
select DIM2_EtchDate, Model, tool_id, avg(TDPower) as TDPower_Mean, count(slider_id)
from (select distinct a.slider_id, trunc(a.test_date_time), left(a.product_id,2) as Model, a.wafer_id, a.row_number,
a.column_number, a.x_coordinate, a.y_coordinate, a.error_code, a.grade, a.bin,
a.TFCTDPWR as TDPower, b.job_number, trunc(c.transaction_date_time) as DIM2_EtchDate, c.tool_id
from ah.param_jade_wide a left join ah.param_lap_summary b on a.wafer_id = b.wafer_id and a.row_number = b.row_number
left join ah.his_job c on c.job_number = b.job_number
where c.transaction_date_time > '2020-02-01'
and left(a.product_id,2) in ('L2','L3','L8','C3','C2','V8')
and b.source_system_code in ('MFG2.SLDR.LAPRUN')
and c.operation_id in ('545600')
and a.retest_number = 0
and a.class_description in ('PROD')
and not c.tool_id = 0 and not c.tool_id in ('') )
group by DIM2_EtchDate, Model, tool_id;
commit;

As You want to add a new "day" entry/entries every day (as DIM2_EtchDate == DATE(c.transaction_date_time) is a part of the summary table), and if You are OK with not selecting "today's" entries, You can just use a simple INSERT for "yesterdays and before" entries, run every day like this:
INSERT INTO public.fc_TDPMean_By_DIM2_EtchDate
SELECT DIM2_EtchDate, Model, tool_id, avg(TDPower) AS TDPower_Mean, count(slider_id)
FROM (SELECT DISTINCT a.slider_id, LEFT(a.product_id,2) as Model,
a.TFCTDPWR AS TDPower, TRUNC(c.transaction_date_time) as DIM2_EtchDate, c.tool_id
-- (I got rid of not used columns.)
FROM ah.param_jade_wide a
LEFT JOIN ah.param_lap_summary b ON (a.wafer_id, a.row_number) = (b.wafer_id, b.row_number)
LEFT JOIN ah.his_job c ON c.job_number = b.job_number
WHERE c.transaction_date_time > '2020-02-01' --MAX(DIM2_EtchDate)
AND c.transaction_date_time < CURRENT DATE
AND LEFT(a.product_id,2) IN ('L2','L3','L8','C3','C2','V8')
AND b.source_system_code IN ('MFG2.SLDR.LAPRUN')
AND c.operation_id IN ('545600')
AND a.retest_number = 0
AND a.class_description IN ('PROD')
AND NOT c.tool_id = 0 AND NOT c.tool_id in ('')
)
GROUP BY DIM2_EtchDate, Model, tool_id
-- WITH appropriate isolation
;
(I haven't tested it. This should work if c.transaction_date_time is DATE. If it is TIMESTAMP, then c.transaction_date_time > '2020-02-01' changes to c.transaction_date_time >= TIMESTAMP('2020-02-01', '00:00') + 1 DAY.)
If You would want to update it more often than once per day, having also incomplete data for today, it could be done using MERGE. If so, just tell me, I could manage to write a MERGE for it. (Or You could, that would be better.)

Related

<= less than equal to does't work as expected for sequilize query in nodejs

I have the following query in sequilize.js
const [results] = await sequelize.query(`select DISTINCT ON (variant_id) variant_id, Count(wl) as count, pds.name AS name,
CASE
WHEN pvs.name is NULL THEN pds.name
ELSE pvs.name
END
as variant_name,
im.url as img_url
from wish_lists as wl
LEFT JOIN products AS pds ON pds.id = wl.product_id
LEFT JOIN images as im on im.product_id = wl.product_id
LEFT JOIN product_variants as pvs ON pvs.id = wl.variant_id
where wl.created_at >= '${startDate}' AND wl.created_at <= '${endDate}' AND wl.tenant_id=${merchant_id}
group by ( variant_id, pds.name, im.url, pvs.name) LIMIT 100 OFFSET 0`)
The problem is if today is the 20th and i have products in the db that are selected on the 20th the query does't include those even though i have <= for the endDate, why is that ?
So why is the same day entries not being selected ?
Firstly, take a look at timezone of datetime data in database and in query param.
Secondly, created_at is the timestamp, which contain Time data (for example 2019-12-20T01:20:30.000Z). Then, your query param ${endDate} is just Date data only, it will append time = 0 (which is 00:00:00.000) before the comparison.
So the comparison would look like:
2019-12-20T01:20:30.000Z <= 2019-12-20T00:00:00.000Z
And the result is false, then it cannot be selected.

Not Exists clause -Query

I am using a NOT EXSITS clause in my query and wanted to make sure it was working correctly since I was getting lesser rows than expected.
SELECT DISTINCT offer.courier_uuid,
offer.region_uuid,
offer.offer_time_local,
Cast(scores.acceptance_rate AS DECIMAL(5, 3)) AS acceptance_rate
FROM integrated_delivery.trip_offer_fact offer
JOIN integrated_product.driver_score_v2 scores ON offer.courier_uuid = scores.courier_id
AND offer.region_uuid = scores.region_id
AND offer.business_day BETWEEN date '2019-04-04' AND date '2019-04-07'
AND scores.extract_dt = 20190331
AND NOT EXISTS
(SELECT NULL
FROM source_cassandra_courier_scheduling.assigned_block_by_id_v2 sched
JOIN source_cassandra_delivery.region r ON sched.region_id = r.id
WHERE offer.courier_uuid = sched.courier_id
AND offer.offer_time_local >= date_parse(date_format(AT_TIMEZONE("start",r.time_zone),'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d %H:%i:%s')
AND offer.offer_time_local <= date_parse(date_format(AT_TIMEZONE("end",r.time_zone),'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d %H:%i:%s')
AND element_at(sched.state,-1) = 'ASSIGNED')
ORDER BY 3
Is there anything wrong with my not exists clause? I am only asking since I am getting back lesser rows than expected. The not exists caluse contains a time conversion but i dont think that would affect anything.
I am trying to get all possible ids and their offer times that do NOT EXIST in the scheduled shifts table. I wanted confirm if the way I have the NOT EXISTS clause is correct or if there is something else I would need that would correctly pull all records that exist or not exist in that shed table?

SQL Most recently updated row

I am pulling information from an SQL database but it displays all lines various times because of multiple updates.
i have managed to eliminate duplicate using the distinct function however still shows multiple lines for the changes.
i.e.
original Qty = 4
update 1 Qty = 5
update 2 Qty = 6
All i want is the most recent update each line which is column OLLNID
SELECT DISTINCT
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
Date( OLTRDJ, CYYDDD ) AS OLTRDJ,
OLLNID AS OLLNID_1,
OLDSC1,
OLDSC2 AS OLDSC2_1,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY
FROM
E1PDES01.PRODDTA.F43199 F43199
WHERE
OLMCU = '13248'
AND Date( OLTRDJ, CYYDDD ) >= '01/01/2017'
AND OLDCTO = 'OP'
AND OLDOCO = 13484379
ORDER BY
6
Like subkonstrukt I am guessing what you really want.
In case that you want the latest results by OLLNID (which is then e.g. a date), then you could group by all other values (when they differ) and choose the max OLLNID.
SELECT
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
Date(OLTRDJ, CYYDDD) AS OLTRDJ,
MAX(OLLNID) AS OLLNID_1,
OLDSC1,
OLDSC2 AS OLDSC2_1,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY
FROM E1PDES01.PRODDTA.F43199 F43199
WHERE OLMCU = '13248'
AND Date(OLTRDJ, CYYDDD) >= '01/01/2017'
AND OLDCTO = 'OP'
AND OLDOCO = 13484379
GROUP BY
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
OLTRDJ,
OLDSC1,
OLDSC2,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY;
depending on what SQL Flavor you´re using this might not work, also I am made some wild assumptions on your data so if you could supply more information (maybe schema / types, SQL Flavor, maybe two or three dummy rows) I will edit my answer
SELECT
org.OLMCU,
org.OLKCOO,
org.OLDOCO,
org.OLDCTO,
Date(org.OLTRDJ, CYYDDD ) AS OLTRDJ,
org.OLLNID AS OLLNID_1,
org.OLDSC1,
org.OLDSC2 AS OLDSC2_1,
org.OLUOM,
org.OLUORG,
org.OLPRRC,
org.OLAEXP,
org.OLANBY
FROM
E1PDES01.PRODDTA.F43199 F43199 as org
LEFT OUTER JOIN E1PDES01.PRODDTA.F43199 F43199 aux ON org.OLLNID = aux.OLLNID AND org.OLLNID < aux.OLLNID
WHERE
org.OLMCU = '13248'
AND Date( org.OLTRDJ, CYYDDD ) >= '01/01/2017'
AND org.OLDCTO = 'OP'
AND org.OLDOCO = 13484379
AND aux.OLLNID IS NULL
GROUP BY
org.OLMCU,
org.OLKCOO,
org.OLDOCO,
org.OLDCTO,
Date(org.OLTRDJ, CYYDDD ) AS OLTRDJ,
org.OLLNID AS OLLNID_1,
org.OLDSC1,
org.OLDSC2 AS OLDSC2_1,
org.OLUOM,
org.OLUORG,
org.OLPRRC,
org.OLAEXP,
org.OLANBY
ORDER BY org.OLLNID

How can I replicate a query to pull data side by side?

I have the following query, and what I am trying to do is take the query data, replicate it but change the names on the column headers from FY14 Q1 to FY14 Q2 and change the filters in the query.
Is there a way I can have this data shown all in one big table by combining the different queries??? Please help.
Select H.OpportunityID, H.CREATEDDATE, H.STAGENAME as 'Q1-14 Stage', H.CLOSEDATE as 'Q1-14 Close Date'
From
(SELECT
OpportunityID, Max(CREATEDDATE) as MaxDate
FROM [BVSFWarehouse].[dbo].[sf_OPPORTUNITYHISTORY]
WHERE
CREATEDDATE <= '2013-05-01'
GROUP BY
OpportunityID) X
Join [BVSFWarehouse].[dbo].[sf_OPPORTUNITYHISTORY] H ON H.OpportunityID = X.OpportunityID
And X.MaxDate = H.CREATEDDATE

SQL, Search by Date and not exists

I have two tables and need to search for all entries that exist in one table in another table by idProduct, only if the date (dateStamp) is less than or older than 7 days.
Because the api I'm using is restricted to only processing 3000 results at a time, the application will close and the next time I run the application I only want the idProducts that are say 3000 or greater for that idProduct, this will be run numerous times for the Suppliercode wll most likely already exist in the table.
So I've been looking at the not exists and getdate functions in sql but not been able to get the desired results.
SELECT
*
FROM
products
WHERE
(active = - 1)
AND suppliercode = 'TIT'
and (NOT EXISTS
(SELECT
idProduct
FROM compare
WHERE
(products.idProduct = idProduct)
OR (compare.dateStamp < DATEADD(DAY,-7,GETDATE()))))
Any pointers would be great, I've changed the OR to AND but it doesn't seem to bring back the correct results.
I am guessing you want to match the rows in the two tables by idProduct as right now your inner query (NOT EXISTS (SELECT idProduct FROM compare WHERE (products.idProduct = idProduct) OR (compare.dateStamp < DATEADD(DAY,-7,GETDATE())))) looks like it is finding all rows that don't match. As your subquery finds all rows that match or where the date is older than 7 days and makes sure that they don't exist.
Is this what your want?
SELECT *
FROM products as p
LEFT JOIN compare as c
ON p.idProduct = c.idProduct
WHERE p.active = -1 and p.suppliercode = 'TIT' and c.dateStamp < DATEADD(DAY,-7,GETDATE())
Have you tried this one yet?
SELECT * FROM products
WHERE (active = - 1) AND
suppliercode = 'TIT'
and ipProduct NOT IN
(
SELECT idProduct FROM compare
WHERE
(products.idProduct = idProduct) OR
(compare.dateStamp < DATEADD(DAY,-7,GETDATE()))
)
Try NOT IN instead:
...
and ProductId NOT IN
(SELECT
idProduct
FROM compare
WHERE
(products.idProduct = idProduct)
OR (compare.dateStamp < DATEADD(DAY,-7,GETDATE()))))
....