SQL Query Accross Three Tables Requiring Different Joins - sql

I have 3 tables. An Applications (relevant field is AppID) table containing information about various application available to download, a Packages table (relevant field is PackageID) containg information on the various downloadable items available and an App/Packages table (relevant field is AppID and PackageID) which shows which packages should be downloaded for each application. I need a query to find out which packages are not related to a specific application.
My idea was to INNER JOIN Applications to AppPackages to get a list of all the packages that are related to an Application and then OUTER JOIN this resultant set to Packages and find all the null results to show which packages were not related.
The problem is I can't seem to work out how to chain these queries. Subqueries don't seem to work, and EXIST/NOT EXIST requires information from the outer query which I just can't make work. I'm assuming there's something simple I'm mising, but all the literature I read is for two tables, never more than two.
This is my best attempt, but the EXISTS part doesn't work for the reason mentioned above.
SELECT Packages.PackageID FROM Packages WHERE NOT EXISTS
(Select AppPackages.PackageID, AppPackages.AppID FROM AppPackages WHERE AppPackages.AppID = #AppID)
Thanks in advance for any help :)
Cheers,
Matt :)

I need a query to find out which packages are not related to a specific application.
SELECT
*
FROM
Packages p
WHERE
NOT EXISTS (
SELECT 1
FROM Applications a
INNER JOIN AppPackages ap ON ap.PackageId = p.PackageId
WHERE ap.AppId = <Your App ID>
)

I would try sth like this:
SELECT Packages.PackageID FROM Packages WHERE Packages.PackageID NOT IN
(Select AppPackages.PackageID FROM AppPackages WHERE AppPackages.AppID = #AppID)

Related

Counting the number of times a result from a query shows up (basic SQL question)

I have these three tables:
I need to list staff names and the number of projects they have worked on, I'm not really sure how to do this. Right now I'm able to list all the staff members who have worked on a project using this:
SELECT staff.s_name FROM Staff staff
INNER JOIN Work_on work ON staff.s_id = work.s_id
INNER JOIN Projects proj ON work.p_id = proj.p_id
And this displays the names of all the people who have worked on a project, some names are listed twice, indicating how many projects they've worked on. I don't know how to list this as a separate value though and display both their names and the number of projects they've been working on, obviously I'd need to use the COUNT() function somewhere but I don't know how to count everytime a name appears from the result. Could someone provide me a tip of sorts?
The result is supposed to look like this:
Right now I'm only getting this:
Which is correctly displaying how many times a user has worked on a project, but I don't know how to use this to create another column displaying the count.
Would it help?
SELECT staff.s_name, count(1) FROM Staff staff
JOIN Work_on work ON staff.s_id = work.s_id
GROUP BY staff.s_name

Error running MS Access delete query with left join

I am using Microsoft Access, I believe from the Office 16 suite.
I am trying to delete records in a Contacts table that are:
listed as "Out of Business" and
no longer have any logged donations in a Donations table.
(We only keep Donations records for five years to maintain a manageable database size. So once an out-of-business company's Donations have worked their way out of that five-year mark, we can go ahead and delete them.)
I've successfully built a query that returns the results I'm looking for - I can see the results in Datasheet view both when doing a SELECT and when switching to DELETE.
However, when I go to run the Delete Query, I get an error message:
Specify the table containing the records you want to delete.
Here is the SQL code:
DELETE Contacts.*
FROM Contacts
LEFT JOIN Donations ON Contacts.[ContactID] = Donations.[DonorID]
WHERE Contacts.ProcurePreference="Out of Business" AND Donations.DonorID Is Null
I am new to SQL code, so am guessing there's something in my syntax that's wonky (although, again, Datasheet view shows exactly what I would expect). Or maybe there's something in the way I've set up my tables and records and relationships that is preventing the final step.
I've searched and read other similar problems on this forum but could not successfully incorporate any of those answers to address my own problem.
Thanks in advance!
You can do what you want with NOT EXISTS:
DELETE Contacts.*
FROM Contacts
WHERE Contacts.ProcurePreference = 'Out of Business'
AND NOT EXISTS (SELECT 1 FROM Donations WHERE Contacts.[ContactID] = Donations.[DonorID])
try using this SQL:
DELETE FROM Contacts
WHERE ProcurePreference = "Out Of Business"
AND ContactId NOT IN (SELECT DonorID FROM Donations)

Adding New Fields via LEFT JOIN in Running ETL from SQL to MongoDB

I need to run an ETL to get data from a Sybase/SQL back-end into my MongoDB environment. We already have data from Sybase, but now there are a couple of additional fields we want to pull data in for. So with my familiarity being with Mongo (not so much Sybase), I'm trying to determine how I need to adjust our ETL to get this additional data.
The current SELECT statement looks like this:
`SELECT DISTINCT TOP 100 d.*, d10.code code10, d10.id_number as Code10ID FROM diagnosis d LEFT JOIN diagnosis_icd10 d10 on d.icd10_id = d10.id_number ORDER BY d.id_number`
Now, within the diagnosis_icd10 table that we're doing the LEFT JOIN on, there are now a couple of extra fields available.
So, my question is, do I need to explicitly include these additional fields here in the SELECT statement in order for them to be available in the ETL process? Or is this only the case if I want to rename the fields? What should this look like?
Yes you need to explicitly request them, as your current query only gets all columns from the diagnosis table (aliased to d via the d*).
This has nothing to do with Sybase though this is basic SQL so would be the same for most databases. You just add them to the select statement as in d10.column_name like the others.

Where is the content stored in Umbraco database?

I've inherited a database for an Umbraco website that I need to pull out the content so it can be loaded into another CMS.
I've restored the SQL Server database and I still cannot find where what is essentially 'blog posts' would be stored.
What I'm looking to do is something like:
SELECT *
FROM blogposts
Thanks
Unfortunately, it won't be quite that simple. Almost everything in umbraco has representation in the umbracoNode table.
Then, all of the custom property data that you have will be stored in these two tables: cmsPropertyData and cmsPropertyType.
I've found that I can get most of what I want with queries like this:
SELECT TOP 1000 *
FROM cmsPropertyData pd
INNER JOIN cmsPropertyType pt
ON pt.id = pd.propertytypeid
INNER JOIN umbracoNode n
ON n.id = pd.contentNodeId
WHERE n.id = 1853
However, you're after info about a specific document type, so you're might look more like this:
SELECT TOP 1000 *
FROM cmsPropertyData pd
INNER JOIN cmsPropertyType pt
ON pt.id = pd.propertytypeid
INNER JOIN cmsContent c
ON c.nodeId = pd.contentNodeId
INNER JOIN cmsContentType ct
ON ct.nodeId = c.contentType
WHERE ct.alias = 'BlogPost'
And then, you'd probably be getting data back for multiple versions of each blog post node, so you'd need to do more joins with the cmsContentVersion table to get just the latest data.
You might think about trying to sidestep some of the database queries and try using the xml cache on disk that already exists. You can find an xml representation of all of the published content at App_Data\umbraco.config. You should be able to trim it down to just the xml representation of the Blog Posts.
I was also reading at this stackoverflow post that you can just export by document type as a Package. You could go to Developer->Packages->Created packages and right click to create. Then in the Package Contents tab, just check the Blog Post document type. Not sure if that will be useful to you because I haven't tried that myself. Looks promising though.

Data from right table does not show using Left Join in Dev Studio

Im using Endeca Dev studio to perform a left join. I have record adapters Items and ItemDetail with unique key itmno which im using for a Left join. Followed all the doc steps.
1) Create both record adapters
2) Create Cache for ItemDetail and add itmno in index
3) Create record assembler, specify itmno as key for Items and ItemDetailCache in Left Join setting.
I dont get any errors but data from ItemDetail doesnt show up in the jsp app. Im new to this so any guidance will really help.
Thanks
A debugging tip - either add an output record adapter after each step or run forge with the --printRecords [number] flag. These are both techniques to capture the state of the records for debugging and review. This should help you see why the join isn't happening.