Basic SQL mechanics, create table, then call that table from another script - sql

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.

Related

Access SQL Select value with more rows many to many relationship

I'm using Access for a cookbook database, an excercise for Uni.
I'm trying to make a query for a many-to-many relationship.
I have ricette and ingredienti tables, and a junction table named ricetta_ingrediente. Now I should make a query that retrieves each ricette associated with ingredienti used.
EDIT: Part of the question got deleted, I need to retrieve the recipe's ingredients of the one with the most ingredients used, it's the result I have to obtain.
Every single try got me either a syntax error or an empty result - how can I achieve this query?
MORE INFOS
The relationship schema
[
I've tried to implement this suggestion, failing at it, how should it be?
Also here's my try and Access error :
[
Use the query builder DesignView to assist in building SQL statement. Result should look like:
SELECT ricette.nome, ingredienti.nome
FROM ingredienti
RIGHT JOIN (ricette RIGHT JOIN ricetta_ingrediente
ON ricette.ID = ricetta_ingrediente.id_ricetta)
ON ingredienti.ID = ricetta_ingrediente.id_ingrediente;
To retrieve recipe with the most ingredients as well as the ingredients, like:
SELECT TOP 1 ricette.nome, ingredienti.nome
FROM (SELECT id_ricetta, Count([id_ingrediente]) AS CountIng
FROM ricetta_ingrediente GROUP BY id_ricetta) AS Q1
RIGHT JOIN (ricette RIGHT JOIN (ingredienti RIGHT JOIN ricetta_ingrediente
ON ingredienti.ID = ricetta_ingrediente.id_ingrediente)
ON ricette.ID = ricetta_ingrediente.id_ricetta)
ON Q1.id_ricetta = ricetta_ingrediente.id_ricetta
ORDER BY Q1.CountIng DESC;
This will not resolve ties. All recipes with the number of ingredients matching the TOP 1 count will return. How should the query know you want only 1 and which one?
Your query is fine. You just need parentheses, because this is MS Access.
I would also use table aliases:
SELECT r.nome, i.nome
FROM (ricette as r INNER JOIN
ricetta_ingrediente as ri
ON r.ID = ri.id_ricetta
) INNER JOIN
ingredienti as i
ON i.ID = ri.id_ingrediente;
EDIT:
For the revised question:
SELECT TOP (1) r.nome
FROM (ricette as r INNER JOIN
ricetta_ingrediente as ri
ON r.ID = ri.id_ricetta
) INNER JOIN
ingredienti as i
ON i.ID = ri.id_ingrediente
GROUP BY r.nome
ORDER BY COUNT(*) DESC;

Can I do a left join without returning the conditional columns?

New to SQL but I want to be able to optimize my query by bringing just the right amount of data. I am doing a left join on CS Rep Name and WE, which are two columns present in both tables. I find that if I don't bring in CS Rep Name and WE in the TECDR table, the query would error. Is there a workaround to this? Since it is a left join, I don't need redundant data.
SELECT *
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN (SELECT CS_Rep_Name,
Team_Leader,
Operations_Manager,
Tenure,
WE,
FileName
FROM Tish_Email_CSAT_Dump_Roster) AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name
AND TECD.WE = TECDR.WE
When you embed a SELECT inside a query in place of a table, the result of a select (projection) behave like a table visible only inside the query.
In your case, the join is the same as if there were a table called TECDR with the columns that you select. Hence, if you leave out some columns of Tish_Email_CSAT_Dump_Roster from your SELECT, these columns would not be available for joining or selection.
However, in your case this is unnecessary: all you need to do is joining to the underlying table, like this:
SELECT
TECD.*
, TECDR.Team_Leader
, TECDR.Operations_Manager
, TECDR.Tenure
, TECDR.FileName
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN Tish_Email_CSAT_Dump_Roster AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name AND TECD.WE = TECDR.WE
select
<place the columns you want here>
from
Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
Hope the following helps or else please share the query that errors:
select TECD.Column1, TECD.Column2, TECDR.Column1, TECDR.Column2
from Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE

MS Access, how to replace a saved query by the full query in a subquery

I have 3 tables:
1 - tblMembers_Info
2 - a junction table
3 - tblCourses
I need to query the members who haven't done a specific course.
After trying to do it manually I gave MS Access "Query Wizard" a try. I ended up with :
A saved query as Query1:
// That one query who did the course
SELECT tblMembers_Info.*, tblCourses.CourseName
FROM tblMembers_Info
INNER JOIN
(tblCourses INNER JOIN tblMembers_Courses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID
WHERE (tblCourses.CourseName) In ('NameOftheCourse');
2nd query using the saved Query1:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
LEFT JOIN [Query1]
ON tblMembers_Info.[Members_ID] = Query1.[Members_ID]
WHERE (((Query1.Members_ID) Is Null));
How can I replace the Query1 in the second query with the full query instead of using a QueryDef (the saved query "Query1")?
Also, there's a better way for sure to write that query, I would really appreciate any help.
You can simply replace LEFT JOIN [Query1] with LEFT JOIN (...) AS [Query1] where ... should be the SQL of the first query, without the ending ;.
But I think in your specific case the use of NOT IN might give a better performance to get the same results:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
WHERE tblMembers_Info.[Members_ID] NOT IN (
SELECT tblMembers_Info.[Members_ID]
FROM ((tblMembers_Info
INNER JOIN tblMembers_Courses
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID)
INNER JOIN tblCourses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
WHERE tblCourses.CourseName = 'NameOftheCourse'
);

Could not add the table (

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

Join with select SQL query?

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')