I tried to make an UPDATE query using data from another table
which in my case was a result of a query and it looks like this:
UPDATE CalculatedQueryINNER JOIN Orders
ON CalculatedQuery.orderid = Orders.OrderID
SET Orders.TotalPrice = [CalculatedQuery].[calculated];
But it's not working, it shows an error must use an updateable query
But i went and copied the results from the query to a table named temp and i did the same and it worked!!
UPDATE temp INNER JOIN Orders
ON temp.orderid = Orders.OrderID
SET Orders.TotalPrice = [temp].[calculated];
Can anyone please provide me with a solution on how to use the query as a table
please?
EDIT: I tried to treat a query as if it was a table by itself, that was my issue, joining tables is not an issue in update queries inaccess
I read a lot through different sites
The problem that I introduced is unsolvable in access.
There is an issue with the update queries since they are not standart in access.
The problem is That in access you can't use another sql query inside an update statement because as i read any queries that use group by or join or select within select etc.. Are marked as unupdateable queries and you can't use them.
As i read i found a few good solutions using a temporary table like i did
Or using dlookup() function.
Keep in mind that you can also perform DELETE and UPDATE statements with a 'FROM', which will help in writing the query in a syntax that is more familiar. Given that you are running two very different update statements above, I've re-written both of them here. Also, given that you're using an Inner Join on both statements, which performs an intersect of data, I've rearranged the tables in the order of update.
UPDATE O
SET [TotalPrice] = [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
UPDATE O
SET [TotalPrice] = [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];
Setting up your Updates (and likewise Deletes) in this syntax is pretty slick, as it allows for another nice feature: running a select to see what data will be changing:
SELECT [O].[TotalPrice]
, [T].[calculated]
FROM [Orders] AS O
INNER JOIN [temp] AS T
ON [T].[orderid] = [O].[OrderID];
SELECT [O].[TotalPrice],
, [CQ].[calculated]
FROM [Orders] as O
INNER JOIN [CalculatedQuery] as CQ
ON [CQ].[orderid] = [O].[OrderID];
Related
SELECT
c.ConfigurationID AS RealflowID, c.companyname,
c.companyphone, c.ContactEmail, COUNT(k.caseid)
FROM
dbo.Configuration c
INNER JOIN
dbo.cases k ON k.SiteID = c.ConfigurationId
WHERE
EXISTS (SELECT * FROM dbo.RepairEstimates
WHERE caseid = k.caseid)
AND c.AccountStatus = 'Active'
AND c.domainid = 46
GROUP BY
c.configurationid,c.companyname, c.companyphone, c.ContactEmail
I have this query - I am using the configuration table to get the siteid of the cases in the cases table. And if the case exists in the repair estimates table pull the company details listed and get a count of how many cases are in the repair estimator table for that siteid.
I hope that is clear enough of a description.
But the issue here is the count is not correct with the data that is being pulled. Is there something I could do differently? Different join? Remove the exists add another join? I am not sure I have tried many different things.
Realized I was using the wrong table. The query was correct.
I have 7 Tables as per attached following Image.
I will either enter Engine Number or Chassis Number and it should show the respective tables information (these tables have only mentioned fields) so all fields can be shown as result.
I can use hard coded Engine Number or Chassis Number. Every time of execution of this Query, I will hard code the required Engine/Chassis Number and will get the result.
Can anybody please help me to write this query for me?
Click Here to See the Tables
This might be a starting point for your solution.
SELECT prod.EngineNo AS engNo, prod.ChassisNo, doral.doralNo [, table.column [AS name]]
FROM DOProductSpecsDetais AS prod
INNER JOIN DORAL AS doral
ON prod.DOProductSpecsDetailID = doral.DOProductSpecsID
INNER JOIN DOProductDetail AS prodDetail
ON prod.DOProductDetailID = prodDetail.DOProductDetailID
WHERE prod.ChassisNo = '<input>' OR prod.EngineNo='<input>'
Between the SELECT and the FROM Statement, you can select any column out of your JOIN.
You can cascade as many JOINs as you like...
Which DBMS are you going to use?
One suggestion: Try to simplify the names of your columns, if possible.
One more: If you just started to do Database things, it is always helpful to start a test environment and use a client tool.
You can write query something like this:
select * from
DoProductSpecsDetail tbl1 inner join Doral tbl2
on tbl1.DoProductSpecsDetailId = tbl2.DoProductSpecsId
inner join DoproductDetail tbl3
on tbl1.DoProductDetailId = tbl3.DoProductDetailId
inner join ProductColor tbl4
on tbl1.ProductColorId = tbl4.ProductColorId
inner join DoDetail tbl5
on tbl3.DeliveryOrderDetailId = tbl5.DeliveryOrderId
inner join ProductMain tbl6
on tbl3.ProductId = tbl6.ProductId
inner join BPMain tbl7
on tbl5.BusinessPartnerId = tbl7.BusinessPartnerId
Please bear with me as this may be a question without a possible answer, but I hope I describe it correctly..
I have a query which joins a number of tables and produces results, and here is the SQL:
SELECT
dbo.Property.PropertyPK,
dbo.Tenancy.TenancyPK,
dbo.Tenant.ContactFK,
dbo.Contacts.strTitle,
dbo.Contacts.strFirstName,
dbo.Contacts.strSurname
FROM dbo.Property
INNER JOIN dbo.Tenancy ON dbo.Property.PropertyPK = dbo.Tenancy.PropertyFK
INNER JOIN dbo.Tenant ON dbo.Tenancy.TenancyPK = dbo.Tenant.TenancyFK
INNER JOIN dbo.Contacts ON dbo.Tenant.ContactFK = dbo.Contacts.ContactPK
The main table is the Property table and I filter out one row by specifying a PropertyPK in my criteria..
My question is.. If the Tenant or Contact record does not exist and I run my query in SQL Management Studio of course I get a message saying there are no rows but can I determine at what stage the join has failed between two tables?
I can of course check this in management studio but I am trying to help the user on the application side to inform them of why there are no rows. My application is in VB and I will write that check if there are no rows and I cannot determine it in SQL..
Sorry for the question in advance..
Derek.. :)
Simply use a LEFT JOIN:
SELECT p.PropertyPK, ty.TenancyPK, t.ContactFK,
c.strTitle, c.strFirstName, c.strSurname
FROM dbo.Property p LEFT JOIN
dbo.Tenancy ty
ON p.PropertyPK = ty.PropertyFK LEFT JOIN
dbo.Tenant t
ON ty.TenancyPK = t.TenancyFK LEFT JOIN
dbo.Contacts c
ON t.ContactFK = c.ContactPK;
This will keep all rows in the Property table. You can then see which primary keys are NULL to see if there were matches in the other tables.
Note that the query is much easier to write and to read when you use table aliases.
I'm not super familiar with SQL but I know the basics. I was recently trying to replicate some logic form reports to SQL Server 2012. I started with the custom query from Webi (a reporting tool) and was trying to make a view from it in SQL.
Here is what the query look like:
SELECT
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
dimGlobalShipDateVw.shipDayOfWeekDesc,
sum(factSalesTblVw.globalSalesValue) AS 'Global Sales Value',
SUM(factSalesTblVw.salesUnitQuantity*GlobalFiles.dimCurrentGTINTbl.unitQty) AS 'Sales Unit Quantity'
FROM
dimGlobalCookCompaniesTbl INNER JOIN factSalesTblVw ON
(dimGlobalCookCompaniesTbl.globalCookCompanyID=factSalesTblVw.globalCookCompanyID)
INNER JOIN dimGlobalHistProductTbl ON (dimGlobalHistProductTbl.globalHistProductID=factSalesTblVw.globalHistProductID)
INNER JOIN dimGlobalCurrentProductTbl ON (dimGlobalHistProductTbl.globalCurrentProductID=dimGlobalCurrentProductTbl.globalCurrentProductID)
INNER JOIN dimGlobalHistShipCustomerTbl ON (factSalesTblVw.globalHistShipCustomerID=dimGlobalHistShipCustomerTbl.globalHistShipCustomerID)
INNER JOIN dimGlobalCurrentShipCustomerTbl ON (dimGlobalHistShipCustomerTbl.shipCustomerID=dimGlobalCurrentShipCustomerTbl.globalCurrentShipCustomerID)
***INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON (dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)***
INNER JOIN dimGlobalSalesAnalysisTbl ON (factSalesTblVw.globalSalesAnalysisID=dimGlobalSalesAnalysisTbl.globalSalesAnalysisID)
INNER JOIN dimGlobalShipDateVw ON (dimGlobalShipDateVw.shipJulianDate=factSalesTblVw.shipDateID)
INNER JOIN GlobalFiles.dimCurrentGTINTbl ON (GlobalFiles.dimCurrentGTINTbl.curGtinId=factSalesTblVw.GtinID)
WHERE
(
dimGlobalShipDateVw.shipYearNumber IN (DATEPART(yy,GETDATE())-1)
AND
dimGlobalCurrentShipCustomerTbl.shipCustomerNumberDesc
IN ( 'JPC000222-3','CNC000012-1' )
AND
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc = 'Return Credits'
)
GROUP BY
dimGlobalShipDateVw.shipDate,
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
Upper(dimGlobalCurrentProductTbl.familyCodeDesc),
dimGlobalShipDateVw.shipYearNumber,
dimGlobalShipDateVw.shipDayOfWeekDesc,
dimGlobalCurrentProductTbl.madeByAbbr,
dimGlobalCookCompaniesTbl.companyDesc
This particular query runs on the production system if ran in the relevant database. When trying to make a view of this query in a different database, I precede the objects by [database_name].[schema/dbo] name.
On running the query, I get the error:
Invalid object name 'WWS.dbo.dimGlobalShipDestinationCountryTbl'
I try to find this particular table on the database, but it isn't there, though hovering over the table name in the query give a table definition but no script.
This table is present in an weird looking inner join (6th inner join) syntax like this:
INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON
(dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)
Two questions:
1. Can someone please explain this query syntax for inner join ?
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Two questions: 1. Can someone please explain this query syntax for inner join ?
The inner join in this case is nothing more than a table alias. The creator of the query thought aliasing the table would be easier to understand this name instead of the actual table name, or the same table is referenced twice and one would have to have an alias.
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Why? I think you just have a syntax error on your SQL when you added the database_name.schema syntax.
Think of the table alias like a column alias.... but and just like columns, you can omit the 'AS' keyword...
dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl is the same as
dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl
I have a query which works, goes like this:
Select
count(InsuranceOrderLine.AntallPotensiale) as potensiale,
COUNT(InsuranceOrderLine.AntallSolgt) as Solgt,
InsuranceProduct.Name,
InsuranceProductCategory.Name as Kategori
From
InsuranceOrderLine, InsuranceProduct, InsuranceProductCategory
where
InsuranceOrderLine.FKInsuranceProductId = InsuranceProduct.InsuranceProductID
and InsuranceProduct.FKInsuranceProductCategory = InsuranceProductCategory.InsuranceProductCategoryID
Group by
InsuranceProduct.name, InsuranceProductCategory.Name
This query over returns what I need, but when I try to add more table (InsuranceOrder) to be able to get the regardingUser column, then all the count values are way high.
Select
count(InsuranceOrderLine.AntallPotensiale) as Potensiale,
COUNT(InsuranceOrderLine.AntallSolgt) as Solgt,
InsuranceProduct.Name,
InsuranceProductCategory.Name as Kategori,
RegardingUser
From
InsuranceOrderLine, InsuranceProduct, InsuranceProductCategory, InsuranceSalesLead
where
InsuranceOrderLine.FKInsuranceProductId = InsuranceProduct.InsuranceProductID
and InsuranceProduct.FKInsuranceProductCategory = InsuranceProductCategory.InsuranceProductCategoryID
Group by
InsuranceProduct.name, InsuranceProductCategory.Name,RegardingUser
Thanks in advance
You're adding one more table to your FROM statement, but you don't specify any JOIN condition for that table - so your previous result set will do a FULL OUTER JOIN (cartesian product) with your new table! Of course you'll get duplication of data....
That's one of the reasons that I'm recommending never to use that old, legacy style JOIN - do not simply list a comma-separated bunch of tables in your FROM statement.
Always use the new ANSI standard JOIN syntax with INNER JOIN, LEFT OUTER JOIN and so on:
SELECT
count(iol.AntallPotensiale) as Potensiale,
COUNT(iol.AntallSolgt) as Solgt,
ip.Name,
ipc.Name as Kategori,
isl.RegardingUser
FROM
dbo.InsuranceOrderLine iol
INNER JOIN
dbo.InsuranceProduct ip ON iol.FKInsuranceProductId = ip.InsuranceProductID
INNER JOIN
dbo.InsuranceProductCategory ipc ON ip.FKInsuranceProductCategory = ipc.InsuranceProductCategoryID
INNER JOIN
dbo.InsuranceSalesLead isl ON ???????? -- JOIN condition missing here !!
When you do this, you first of all see right away that you're missing a JOIN condition here - how is this new table InsuranceSalesLead linked to any of the other tables already used in this SQL statement??
And secondly, your intent is much clearer, since the JOIN conditions linking the tables are where they belong - right with the JOIN - and don't clutter up your WHERE clauses ...
It looks like you added the table join which slightly multiplies count of rows - make sure, that you properly joining the table. And be careful with aggregate functions over several joined tables - joins very often lead to duplicates