The following code works in Sage200.
SELECT bcs.BomReference
,bcs.DateTimeCosted
,bcs.TotalCost
FROM (
SELECT BomReference
,Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN BomCostSession AS bcs ON bcs.BomReference = ldc.BomReference
AND bcs.DateTimeCosted = ldc.MaxDate
ORDER BY BomReference
As soon as I try extending this with an INNER JOIN to another table to get more columns (using BomReference), I get the error message:
Could not add the table (.
See below for example of modified code; I have to use 2 joins to get to the table I need, but have the same error whatever I join onto the working code.
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost, BomBuildProduct.StockDescription
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
BomCostSession as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
***** Fails when adding INNER JOIN here *****
INNER JOIN
BomBuildPackage
ON BomCostSession.BomBuildPackageID = BomBuildPackage.BomBuildPackageID
INNER JOIN
BomBuildProduct
ON BomBuildPackage.BomRecordID = BomBuildProduct.BomRecordID
ORDER BY BomReference
What am I doing wrong ? I need to expand the query with data from several tables.
I also think that when using MSQuery on the section that works, it offers no options to add any tables - this makes it rather difficult to try options.
Why ?
The issue with MSQuery is that it attempts to display your query graphically in it's design view, this works OK for simple queries but not for complex queries which usually generates the could not add table message. The way I found around this is to treat your query as one big sub query inside a wrapper query, this forces MSQuery to give up the design view and work as pure SQL text.
Another issue might be that for one table you have the full path but not the others, is it correct for the table you have included it and does it need to be used on the other tables.
Here is an example of the changes I think you should make:
SELECT * FROM (
SELECT bcs.BomReference
,bcs.DateTimeCosted
,bcs.TotalCost
,BomBuildProduct.StockDescription
FROM
(SELECT BomReference
,Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference) AS ldc
INNER JOIN NDM_Sage200.dbo.BomCostSession AS bcs ON bcs.BomReference = ldc.BomReference
AND bcs.DateTimeCosted = ldc.MaxDate
INNER JOIN NDM_Sage200.dbo.BomBuildPackage ON BomCostSession.BomBuildPackageID = BomBuildPackage.BomBuildPackageID
INNER JOIN NDM_Sage200.dbo.BomBuildProduct ON BomBuildPackage.BomRecordID = BomBuildProduct.BomRecordID) x
ORDER BY BomReference
Related
I have two scripts that work in isolation, but I don't know how to stitch them together to do this all at once.
The first script:
SELECT *
FROM PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTES bq
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.PRODUCT p ON bq.ProductId = p.Id
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTEFEES F ON F.ApplicationQuoteId = bq.Id
LEFT JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.COVERAGESECTIONTYPE CST ON F.CoverageSectionTypeID = CST.SECTIONID
This produces a table (is that the right word?). Suppose I could save that table as 'appQuote'. I then want to run this script on it:
SELECT
ApplicationId,
STRING_AGG(YEARLYPAYAMOUNT) allPremium,
STRING_AGG(SECTIONID) allSection,
STRING_AGG(SHORTNAME) allSectionName,
FROM
appQuote
GROUP BY
ApplicationId
For storage reasons I don't want to actually save the table from the first script, I just want to get the result from the first script and immediately apply the second script to it.
This is very basic, so any guidance would help.
The relevant term is known as a derived table.
In SQL you can use the result of a query as the input to another. The "nested query" produces the derived table which is then consumed by the outer query:
Select <columns>
from (
select <columns> from table join table etc
)as TableAliasName
To get the nomenclature right, what you have are not scripts or tables, but rather two queries. When a query runs it produces a result set.
To accomplish your goal, either of these queries will work:
SELECT
ApplicationId,
STRING_AGG(YEARLYPAYAMOUNT) allPremium,
STRING_AGG(SECTIONID) allSection,
STRING_AGG(SHORTNAME) allSectionName,
FROM (
SELECT *
FROM PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTES bq
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.PRODUCT p ON bq.ProductId = p.Id
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTEFEES F ON F.ApplicationQuoteId = bq.Id
LEFT JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.COVERAGESECTIONTYPE CST ON F.CoverageSectionTypeID = CST.SECTIONID
) appQuote
GROUP BY
ApplicationId
Here, the inner nested query is formally called a derived table, but people will often use the phrase "subquery".
The other option is a Common Table Expression (CTE):
With appQuote As (
SELECT *
FROM PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTES bq
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.PRODUCT p ON bq.ProductId = p.Id
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTEFEES F ON F.ApplicationQuoteId = bq.Id
LEFT JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.COVERAGESECTIONTYPE CST ON F.CoverageSectionTypeID = CST.SECTIONID
)
SELECT
ApplicationId,
STRING_AGG(YEARLYPAYAMOUNT) allPremium,
STRING_AGG(SECTIONID) allSection,
STRING_AGG(SHORTNAME) allSectionName,
FROM
appQuote
GROUP BY
ApplicationId
Finally, you could also create a View from the first query:
CREATE View AppQuote As
SELECT *
FROM PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTES bq
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.PRODUCT p ON bq.ProductId = p.Id
JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.BCAPPLICATIONQUOTEFEES F ON F.ApplicationQuoteId = bq.Id
LEFT JOIN PROD_ANALYTIC.SRC_MVC_AU_DBO.COVERAGESECTIONTYPE CST ON F.CoverageSectionTypeID = CST.SECTIONID
A view is not just a query; it will actually become part of the database, such that you can use it in many of the same ways you would use a table saved on disk, and then the second query in the original question will run unmodified.
First Way:
SELECT ST.PersonID, ST.CustomerID, ST.SaleTypeID, ST.PaymentGatewayID,
ST.CustomerMembershipID, ST.CustomerPaymentGatewayID, P.Currency
FROM ServiceTransaction ST
INNER JOIN dbo.Person P ON ST.PersonID = P.PersonID;
Second Way:
SELECT ST.PersonID, ST.CustomerID, ST.SaleTypeID, ST.PaymentGatewayID,
ST.CustomerMembershipID, ST.CustomerPaymentGatewayID, P.Currency
FROM ServiceTransaction ST
INNER JOIN dbo.Person P ON P.PersonID = ST.PersonID;
I want to optimize this query in SQL. Currently I am using second way and I need expert opinion, which one is best approach.
However, any one can add some new way to optimize this query as well.
As comments suggest, they are identical in many aspects. For a personal note tho if you keep the same format it gets easier to follow so I use the following schema to keep tabs on tables where I always use lower numbered table on left side
SELECT Column_list
FROM TABLE1
INNER JOIN TABLE2
INNER JOIN TABLE3
ON Table1.ColName = Table2.ColName
ON Table1.ColName = Table3.ColName | ON Table2.ColName = Table3.ColName
This way when you are working with multiple tables and joins it is easier to follow which table is joined where and which table was mentioned first in the query.
SELECT
Trs.itemID, Trs.imtName, Trs.sumQty, Sum(whiQty)
FROM
((SELECT
trsitemID AS itemID, trsimtName AS imtName,
Sum(trsQty) As sumQty
FROM
tblTransactionSub AS T
WHERE
trstraID = 1231
AND trsActive = True
GROUP BY
trsitemID, trsimtName) AS Trs
INNER JOIN
tblWarehouseItem AS WHI ON Trs.itemID = WHI.whiitemID)
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
WHERE
whiActive = True
AND whiCansel = False
AND whiwrhID = 19
GROUP BY
Trs.itemID,Trs.imtName, Trs.sumQty
HAVING
SUM(whiQty) < Trs.sumQty
If you please help me me out since I am new to SQL commands I can not easily find my mistake.
Thanks in advance
The error that occurred when I added the Right Join is:
Join expression not supported
In MS Access, you have to use parenthesises with multiple joins:
select ...
from
((table1
... join table2 on ...)
... join table3 on ...)
... join tableN
/edit/
As OP question changes its syntax often, then my answer seems out of place :) Initially there were no parens there.
About RIGHT JOIN: You need to use table name (or entire subselect) after JOIN keyword, not skip it or use some other alias. Your query part
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
currently uses alias WHI, which is wrong in two ways: 1) it is not table name 2) it is already used. You need something like this:
RIGHT JOIN
tblWarehouseItem AS WHI2 ON Trs.trswhiID = WHI2.whiID
It could be possible that MS Access restricts your kind of JOINs usage (like INNER join should not come after LEFT join); I have currently no possibility to check precise rules.
Your problem is that you have no table name after the RIGHT JOIN:
RIGHT JOIN
ON Trs.trswhiID = WHI.whiID
Should be:
RIGHT JOIN YOURTABLENAMEHERE as alias
ON Trs.trswhiID = WHI.whiID
However, you have already defined Trs and Whi, so I have no idea what table you want there, or why. Perhaps you just want to change the INNER JOIN to a LEFT JOIN or RIGHT JOIN.
I am getting a Syntax Error in FROM Clause dialog box when I try to run the following query in MS-Access 2010:
SELECT
Contact_addresses.AddressID,
Contact_addresses.ContactID,
Contact_addresses.Address1,
Contact_addresses.Address2,
Contact_addresses.City,
Contact_addresses.State,
Contact_addresses.Zip,
Owner_Operator.FIRM_NAME,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Contact_addresses
(
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator)
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
)
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent)
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
;
When I dismiss the dialog box, access highlights the ( after FROM Contact_addresses.
I know that I need parentheses with multiple nested joins in Access, but can someone please explain the concepts of how this works, in addition to showing how to fix whatever the problem is?
The relevant aspects of the schema of the underlying tables should be clear from the SQL SELECT statements.
I think this should work, but I lack access to test. General syntax to hold to
From table
left join (statement) alias
on table.col = alias.col
left join ...
Altering your statement to this:
FROM Contact_addresses
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator) Owner_Operator
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent) Official_Correspondent
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
;
I've added the table alias to match what you call it in your join and I removed the offending set of brackets.
The problem was in the location of the opening brackets ((. They need to be immediately after the first FROM. Here is what works:
SELECT Contact_addresses.AddressID, Contact_addresses.ContactID, Contact_addresses.Address1, Contact_addresses.Address2, Contact_addresses.City, Contact_addresses.State, Contact_addresses.Zip, Owner_Operator.FIRM_NAME, Official_Correspondent.FIRST_NAME, Official_Correspondent.LAST_NAME, Official_Correspondent.SUBACCOUNT_COMPANY_NAME, Official_Correspondent.PHONE_NUMBER
FROM ((Contact_addresses
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator) AS Owner_Operator
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
)
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent) AS Official_Correspondent
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
)
;
I'm trying to run a query on a table (the one Wordpress uses) where I want to select the ID and post_type columns from one table, then do a Left Join to another table, two separate times (getting separate data).
This is what I have so far, but it's not cutting the mustard:
SELECT derby_posts.id AS pID,
derby_posts.post_type AS tier
FROM derby_posts
LEFT JOIN (SELECT derby_postmeta.post_id AS dbID1,
derby_postmeta.meta_key AS dbMeta1)
ON pid = dbid1
AND dbmeta1 = 'twitter'
LEFT JOIN (SELECT derby_postmeta.post_id AS dbID2,
derby_postmeta.meta_key AS dbMeta2)
ON pid = dbid2
AND dbmeta2 = 'website'
WHERE tier IN ('local', 'regional', 'national')
I'm sure I'm missing something super simple...
Edit: here's the solution that worked for me. Table alias helped, putting all my SELECT statements together cleaned things up. Also, I realized I could remove items from the SELECT, even though I'm using them in the Join, which cleans up the results a lot.
SELECT
db.ID as id,
db.post_type as tier,
dpm1.meta_value as twitter,
dpm2.meta_value as website
FROM derby_posts db
LEFT JOIN derby_postmeta dpm1 ON (db.ID = dpm1.post_id AND dpm1.meta_key = 'twitter' )
LEFT JOIN derby_postmeta dpm2 ON (db.ID = dpm2.post_id AND dpm2.meta_key = 'website' )
WHERE db.post_type IN ('local','regional','national')
I 'm sure I'm missing something super simple...
You are right!
You need to give your selects an alias, and use that alias in the ON clause. You are also missing a FROM <table> - a required part of a SELECT statement that reads from a table:
LEFT JOIN (
SELECT derby_postmeta.post_id AS dbID1,
derby_postmeta.meta_key AS dbMeta1
FROM someTable
) dpm ON pid = dpm.dbid1 AND dpm.dbmeta1 = 'twitter'
I gave the results of your SELECT an alias dpm, and used it to "link up" the rows from the inner select to the rows of your outer select.
SELECT
db.derby_posts.ID as pID,
db.derby_posts.post_type as tier,
dpm1.post_id as dbID1,
dpm1.meta_key as dbMeta1,
dpm2.post_id as dbID2,
dpm2.meta_key as dbMeta2
FROM derby_posts db
LEFT JOIN derby_postmeta dpm1 ON (db.pID = dpm1.post_id AND dpm1.meta_key= 'twitter')
LEFT JOIN derby_postmeta dpm2 ON (db.pID = dbm2.post_id AND dbm2.meta_key = 'website')
WHERE tier IN ('local','regional','national')