I have the following two tables
activity
activity_bak
I want to take the comments from activity_bak and update the comments in activity to match by using the corresponding activity_no and activity_seq.
I've tried doing it like this but to no success:
update Animal.sysadm.activity
set activity_comment = ab.activity_comment
from Animal.SYSADM.activity a
left join Animal.SYSADM.activity_bak ab
on ab.activity_no = a.activity_no
left join Animal.sysadm.activity_bak ab2
on ab2.activity_seq = a.activity_seq
Any help or pointers would be greatly appreciated.
No need to do 2 joins, you need just one. The right syntax is:
UPDATE a
SET a.activity_comment = ab.activity_comment
FROM Animal.SYSADM.activity a
INNER JOIN Animal.SYSADM.activity_bak ab
ON ab.activity_no = a.activity_no
AND ab.activity_seq = a.activity_seq;
I think you want:
update A set activity_comment = ab.activity_comment
from Animal.SYSADM.activity a
left join Animal.SYSADM.activity_bak ab
on ab.activity_no = a.activity_no
And ab.activity_seq = a.activity_seq
Related
UPDATE
Chemicals.dbo.ChmProductCompositions
SET Vhap = SUM(PercentOfProduct)
FROM
Chemicals.dbo.ChmProductCompositions PC
JOIN Chemicals.dbo.ChmCompositionChemicals CC ON CC.ProductCompositionID = pc.ProductCompositionID
JOIN Chemicals.dbo.ChmChemicals C ON C.ChemicalID = CC.ChemicalID
WHERE IsHazardous = 1
I have the query above and I was wondering does something like the above query get the total sum and set it or the sum for each specific and set it?
I was also wondering if it gets the total sum and sets each one to the total how might I go about getting the sum for each individual item and then set it to that?
It doesn't. Just try the query -- it is totally safe, because it returns an error. UPDATEs are not aggregations, so SUM() is not allowed.
You need to aggregate before joining:
UPDATE pc
SET Vhap = cc.PercentOfProduct
FROM Chemicals.dbo.ChmProductCompositions pc JOIN
(SELECT cc.ProductCompositionID, SUM(PercentOfProduct) as PercentOfProcue
FROM Chemicals.dbo.ChmCompositionChemicals CC JOIN
Chemicals.dbo.ChmChemicals C
ON C.ChemicalID = CC.ChemicalID
GROUP BY cc.ProductCompositionID
) cc
ON CC.ProductCompositionID = pc.ProductCompositionID
WHERE IsHazardous = 1;
Note that IsHazardous may belong in the subquery. Your question is not clear.
You have two levels of the problem. First, you need to perform an aggregation. Next, you need to update the corresponding record for each record in the aggregates. You can perform this as Gordon Linoff described in a subquery, or, you can use temporary tables instead. Let's assume that you have a table
pc_temp(ProductCompositionID, result)
insert-select:
INSERT INTO pc_temp(ProductCompositionID, result)
SELECT PC.ProductCompositionID, SUM(PercentOfProduct)
FROM
Chemicals.dbo.ChmProductCompositions PC
JOIN Chemicals.dbo.ChmCompositionChemicals CC ON CC.ProductCompositionID = pc.ProductCompositionID
JOIN Chemicals.dbo.ChmChemicals C ON C.ChemicalID = CC.ChemicalID
WHERE IsHazardous = 1
So you end up with records in the temp table that can be used in your simplified update because the aggregation already happened.
simplified update:
UPDATE PC
SET Vhap = result
FROM
Chemicals.dbo.ChmProductCompositions PC
JOIN pc_temp
ON PC.ProductCompositionID = pc_tmp.ProductCompositionID;
I am new to JOINS and testing my query, but it's just not working for me...
The situation:
The database has got the following columns:
links (contains unique data)
cl_link (contains the relation between links & cats)
cats (cat. descriptions
images (contains multiple images of one link)
cfvalues (contains the values of the multiple custom fiels
customfields (contains the multiple customfields)
I am using the following query, but the Joins are not working for me. Because I only get one image while sometimes there are multiple. And I only get one customfield instead of multiple and I get none cfvalues.
I guess something is wrong with the JOINS, but I am not sure. Can somebody help me out here?
The SQL
SELECT DISTINCT
rqypj_mt_links.link_name,
rqypj_mt_links.link_desc,
rqypj_mt_links.address,
rqypj_mt_links.city,
rqypj_mt_links.state,
rqypj_mt_links.country,
rqypj_mt_links.postcode,
rqypj_mt_links.telephone,
rqypj_mt_links.fax,
rqypj_mt_links.email,
rqypj_mt_links.website,
rqypj_mt_links.price,
rqypj_mt_links.lat,
rqypj_mt_links.lng,
rqypj_mt_links.zoom,
rqypj_mt_cats.cat_name,
rqypj_mt_images.filename,
rqypj_mt_cfvalues.value,
rqypj_mt_customfields.caption
FROM rqypj_mt_links
LEFT JOIN rqypj_mt_cl
ON rqypj_mt_links.link_id = rqypj_mt_cl.link_id
LEFT JOIN rqypj_mt_cats
ON rqypj_mt_cl.cat_id = rqypj_mt_cats.cat_id
LEFT JOIN rqypj_mt_images
ON rqypj_mt_links.link_id = rqypj_mt_images.link_id
LEFT JOIN rqypj_mt_cfvalues
ON rqypj_mt_links.link_id = rqypj_mt_cfvalues.link_id
LEFT JOIN rqypj_mt_customfields
ON rqypj_mt_customfields.cf_id = rqypj_mt_customfields.cf_id LIMIT 100
Thanks in advance!
Jelte
your last condition doesn't look right:
on rqypj_mt_customfields.cf_id = rqypj_mt_customfields.cf_id
translates to 1=1
Shouldn't it be:
on rqypj_mt_customfields.cf_id = rqypj_mt_cfvalues.cf_id
Probably because you don't have an order by and are using limit.
Change it to
order by rqypj_mt_links.link_id, rqypj_mt_cl.cat_id
limit 100
and then your multiple pictures for the same link should be together.
Also please consider use of alias to make your code easier to read:
SELECT DISTINCT
links.link_name,
links.link_desc,
links.address,
links.city,
links.state,
links.country,
links.postcode,
links.telephone,
links.fax,
links.email,
links.website,
links.price,
links.lat,
links.lng,
links.zoom,
cats.cat_name,
images.filename,
cfvalues.value,
--custom.caption
FROM rqypj_mt_links links
LEFT JOIN rqypj_mt_cl cl ON links.link_id = cl.link_id
LEFT JOIN rqypj_mt_cats cats ON cl.cat_id = cats.cat_id
LEFT JOIN rqypj_mt_images images ON links.link_id = images.link_id
LEFT JOIN rqypj_mt_cfvalues cfvalues ON links.link_id = cfvalues.link_id
--LEFT JOIN rqypj_mt_customfields custom ON custom.cf_id = custom.cf_id
ORDER BY links.link_id, cats.cat_id
LIMIT 100
I get a error that FROM Keyword not found where expected. Please let me know what might be wrong in the below code. I am trying to do update using two inner joins and also doing a select from an other view. Please let me know how this can be accomplished in oracle.
update (Select asset.CRV_AMOUNT as ACRV,
cmd.CRV_PERCENT as CRVP,
(select CRV$
from recapt.facility_rec_crv_V fac_v
where fac_v.fac_code = fac.code
AND fac_v.complex_code = fac.complex_code) CRV_TOTAL as CRVT
from recapt.asset asset
inner join recapt.facility_rec fac
on fac.code = asset.fac_rec_code and
fac.complex_code = asset.complex_code
inner join recapt.cost_model_detail cmd
on cmd.cost_model_id = fac.cost_model_id and
cmd.mf_division_id = asset.mf_division_id) t
SET t.ACRV = ((t.CRVP * t.CRVT)/100);
You can try switching to MERGE statement - it will probably be much easier to code and understand.
I cannot test this, but something in the lines of:
merge into recapt.asset asset
using (select fac.code,
fac.complex_code,
cmd.mf_division_id,
cmd.crv_percent as crvp,
(select crv$
from recapt.facility_rec_crv_v fac_v
where fac_v.fac_code = fac.code
and fac_v.complex_code = fac.complex_code) as crvt
from recapt.facility_rec fac
inner join recapt.cost_model_detail cmd
on cmd.cost_model_id = fac.cost_model_id) t
on (asset.fac_rec_code = t.code and
asset.complex_code = t.complex_code and
asset.mf_division_id = t.mf_division_it)
when matched then
update set asset.acrv = (t.crvp * t.crvt) / 100;
If you provide create table and insert statements (with some small amount of test data) I may be able to test this and/or offer a better solution.
Im trying to match this SQL query in querydsl
SELECT tr.* FROM test.TRIP_REQ tr left outer join test.ADDR_BOOK ab on tr.REQ_USERID=ab.USER_ID
I know how to make left join query if you join into identity column but struggle to make it work with joining on 2 alternative columns. tr.REQ_USERID and ab.USER_ID are not identity columns
This is my querydsl:
QTripReq qTripReq = QTripReq.tripReq;
QAddressBook qABook = QAddressBook.addressBook;
JPAQuery query = new JPAQuery(entityManager);
query.from(qTripReq).leftJoin(qABook).on(qTripReq.requestorUser.id.eq(qABook.user.id)).list(qTripReq);
This throws error:
Path expected for join! [select tripReq from com.TripReq tripReq left join ADDR_BOOK addressBook with tripReq.requestorUser.id = addressBook.user.id where tripReq.assignedCompany.id = ?1]
You need to add a target entity path to leftJoin(), so that
QTripReq qTripReq = QTripReq.tripReq;
QAddressBook qABook = QAddressBook.addressBook;
JPAQuery query = new JPAQuery(entityManager);
query.from(qTripReq).leftJoin(qTripReq.addressBook, qABook).on(qTripReq.requestorUser.id.eq(qABook.user.id)).list(qTripReq);
Take a look on Using joins section in docs.
I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;