Update a table with results from multiple tables - sql-server-2012

Currently using Microsoft SQL Server 2012 (Management Studio), looking to update a column in a table with a query which uses data from another table.
This is the query I am running:
SELECT
tblGeneralVehicleInformation.EngineerVehicleReg, tblModel.Model_ID,
tblModel.VanOrCar, tblModel.Model, tblMake.Make_ID,
tblMake.WarrantyCars, tblMake.WarrantyVans,
tblMake.WarrantyCarMonths, tblMake.WarrantyVanMonths,
tblGeneralVehicleInformation.PurchaseDate,
tblGeneralVehicleInformation.Mileage
FROM
tblGeneralVehicleInformation
INNER JOIN
tblModel ON tblGeneralVehicleInformation.Model_IDFK = tblModel.Model_ID
INNER JOIN
tblMake ON tblGeneralVehicleInformation.Make_IDFK = tblMake.Make_ID
UPDATE dbo.tblGeneralVehicleInformation
SET WarrantyId = '1'
WHERE (tblModel.VanOrCar = N'Van')
AND (Mileage > WarrantyVans)
AND (DateDiff("M",PurchaseDate,CURRENT_TIMESTAMP) > WarrantyVanMonths)
Should this work or are you not able to have a query like the above within the UPDATE statement?
Thanks for your help

You need to combine both into a single statement. I would also advise you start using table aliases to make your code a little more readable...
UPDATE v
SET WarrantyId = '1'
FROM tblGeneralVehicleInformation v
INNER JOIN tblModel mo
ON v.Model_IDFK = mo.Model_ID
INNER JOIN tblMake ma
ON v.Make_IDFK = ma.Make_ID
WHERE (mo.VanOrCar = N'Van')
AND (Mileage > WarrantyVans)
AND (DateDiff("M",PurchaseDate,CURRENT_TIMESTAMP) > WarrantyVanMonths)

Related

How to insert one column from a table into another based on a join/where clause

I have two tables, temp_am and amphibian. The relationship between the two tables comes from the lake_id and the survey_date column in both tables. Both tables have 24,109 entries.
temp_am
id
lake_id
survey_date
1
10,001
7/25/2001
5
10,005
7/27/2001
6
10,006
7/29/2001
etc...
amphibain
id
lake_id
survey_date
amhibian_survey_id
1
10,002
7/25/2001
2
10,005
7/27/2001
etc...
I want to input the temp_am.id into the amphibian.amphibian_survey_id when both lake_ids and survey dates equal each other.
I have tried this sql query but it never worked. I canceled the query after 600 seconds as I figured a 29,000 observation table should not take that long. Please let me know if you see any issues in my query statement.
update amphibian
set amphibian_survey_id = tm.id
from amphibian a
inner join temp_am tm
on a.lake_id = tm.lake_id
and a.survey_date = tm.survey_date
This query worked in microsoft access but not on DBeaver
UPDATE amphibian
inner JOIN amphibian_survey_meta_data md ON
(amphibian.survey_date = md.survey_date) AND (amphibian.lake_id = md.lake_id) SET amphibian.amphibian_survey_id = [md.id];
Postgres does not require repeating the table name for an update join. In this case even the join is not necessary just set <column> = ( select ... ) is sufficient. See demo here.
update amphibain a
set amhibian_survey_id =
( select tm.id
from temp_am tm
where (tm.lake_id, tm.survey_date) =
(a.lake_id, a.survey_date)
) ;

update statement with two or more inner join oracle and calculate values also select from a view

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.

How to improve performance of SUM aggregate function in SQL Server 2008 R2

We are working on a SQL query to improve performance. Query is
SELECT
SUM(p.fDebitAmount),
SUM(p.fCreditAmount)
FROM
tSCCompany c
LEFT JOIN
tGLPostSummary p ON (c.fCompanyID = p.fCompanyID AND p.fAccount = c.fEnterpriseAccount AND p.fPostDate <= '2014-08-19 21:51:56')
INNER JOIN
tSCLedgerAccount l ON (c.fCompanyID = l.fCompanyID AND c.fEnterpriseAccount = l.fAccount)
WHERE
c.fEnterpriseID = '4FD5CB57-C04E-11D2-8C59-00A02492E6F3'
AND c.fCompanyID = 'A1F266BA-FC99-11D2-B221-0008C7B1BE09'
AND c.fEnterpriseAccount = '202'
AND c.fCurrencyID = '1'
Above query is taking 6 sec. to execute
Please reference the attached image for further explanation.
Is there any way to reduce these time to improve performance of query?
We try use SUM function with over but it is giving exception like order by syntax error.
We are using index properly on our database.Actually database exist on client side and it is Handel my client. So all the task related to database schema or structure is done by client. We have no permission to update database design. Please suggest me on query level how can we improve performance.
Please try this:
SELECT
SUM(p.fDebitAmount),
SUM(p.fCreditAmount)
FROM tSCCompany c
INNER JOIN tSCLedgerAccount l ON (c.fCompanyID = l.fCompanyID AND c.fEnterpriseAccount =l.fAccount)
LEFT JOIN
tGLPostSummary p ON (c.fCompanyID = p.fCompanyID AND p.fAccount = c.fEnterpriseAccount )
WHERE
c.fEnterpriseID = '4FD5CB57-C04E-11D2-8C59-00A02492E6F3'
AND c.fCompanyID = 'A1F266BA-FC99-11D2-B221-0008C7B1BE09'
AND c.fEnterpriseAccount = '202'
AND c.fCurrencyID = '1' AND Convert(char,p.fPostDate,109) <= Convert(Char,Convert(datetime,'2014-08-19 21:51:56'),109)

SQL Query: Comparing two dates in returned record

I'm trying to come up with an automated solution for something I do manually now and I only have minimal, bare-bones SQL skill. I usually modify simple queries others have built or will build basic select queries. I have done some reading but don't know how to make it do what I need in this case. I need to come up with something others can use while I am out for a month (and which will save me time when I return).
What I need is to return the fields below where tblThree.EndDate is later than tblFive.ServiceEnd. I have to do a couple of other compares on the dates, but if I get a working query of the first one I can make it work with the others. We use MS SQL Server 2008.
I tried creating sub-queries with aliases and failed miserably at making it work.
These are the table and fields I am working with:
tblOne.ServiceID
tblOne.ServiceYear
tblOne.Status
tblTwo.AccountNbr
tblTwo.AcctName
tblThree.BeginDate (smalldatetime, null)
tblThree.EndDate (smalldatetime, null)
tblFour.ClientID
tblFour.ServiceName
tblFive.ContractID
tblFive.ServiceBegin (smalldatetime, null)
tblFive.ServiceEnd (smalldatetime, null)
This is how the tables are related:
tblOne.ServiceID = tblThree.ServiceID
tblOne.ContractID = tblFive.ContractID
tblOne.ClientID = tblFour.ClientID
tblTwo.AccountNbr = tblFour.Account
I used MS Access 2003 to generate the Join SQL:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID;
Thanks for any help.
Just add a WHERE clause to get started:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID
WHERE tblThree.EndDate > tblFive.ServiceEnd;
SELECT
tblOne.ServiceID,
tblOne.ServiceYear,
tblOne.Status,
tblTwo.AccountNbr,
tblTwo.AcctName,
tblThree.BeginDate,
tblThree.EndDate,
tblFour.ClientID,
tblFour.ServiceName,
tblFive.ContractID,
tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM tblOne
INNER JOIN tblThree
ON tblOne.ServiceID = tblThree.ServiceID
INNER JOIN tblFive
ON tblOne.ContractID = tblFive.ContractID
INNER JOIN tblFour
ON tblOne.ClientID = tblFour.ClientID
INNER JOIN tblTwo
ON tblTwo.AccountNbr = tblFour.Account
WHERE tblThree.EndDate > tblFive.ServiceEnd

SQL query for filtering data

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")
;