Access SQL Lookup - Dropdown Columns - sql

I have looked for a similar issue with no luck. Maybe I don't know the right term to search for.
This seems so simple, but I just can't get it after spending many hours trying different approaches.
I have a dropdown to select contracts which shows some ids for related fields. How can I get those IDs to show the value of another column.
SELECT tbl_contracts.ID, tbl_contracts.contract_name, tbl_contracts.firm_id, tbl_contracts.agency_id
FROM tbl_contracts;
image of dropdown
I would like the IDs shown for agency_id and firm_id to list the company_name from their respective table "tbl_firm_agencies" where the tbl_contracts looks them up from. I've tried INNER JOINS but when I do, I can only line items to show when both agency AND firm exist, so my dropdown get cut off quite a bit.

Simply LEFT JOIN to those lookup tables as opposed to INNER JOIN. Adjust table and field names to actual ones in query below. Also, the parentheses are required in MS Access SQL.
SELECT c.ID, c.contract_name, f.firm_name, a.agency_name
FROM (tbl_contracts c
LEFT JOIN tbl_firms f
ON c.firm_id = f.firm_name)
LEFT JOIN tbl_agencies a
ON c.agency_id = a.agency_name;

Related

MS Access SQL, sort by age in a specific order

My task: "Compile an SQL query that outputs a specific store (enter parameter window) the age of the youngest buyer"
I´ve tried some things, but because i´m new to SQL and i have no idea what i´m doing non of them seem to work.
I´d really appreciate, if someone would help me.
Thanks!
First you need to know the fields to SELECT (or return) and the table FROM which you are querying (asking) data; let's say you have the following tables: tblStores (containing a list of stores and related info), tblCustomers (containing customers and related info, e.g. ages, names, phone numbers, etc.), and tblPurchases (containing all the purchases at all stores by all customers and related info). You want the minimum age of a customer making a purchase at a specfic store, so you could use a MIN aggregating function. You would want to join (or relate) the tables based on customers and purchases. See my INNER JOINs in the example below. Then you filter the result by the user-inputted store name (inputStoreName) using WHERE; since the inputStoreName is undefined, in Access this would cause the parameter entry popup window to appear.
SELECT list of fields or aggregating functions you want (comma-separated)
FROM list of tables the fields are in (comma-separated) and how to join the tables
WHERE list of conditions to filter the data (separated by AND or OR)
Example:
SELECT tblStores.Name, tblStores.Description, MIN(tblCustomers.age)
FROM tblStores INNER JOIN ( tblPurchases INNER JOIN tblCustomers on tblPurchases.customerID = tblCustomer.customerID) ON tblStores.storeID = tblPurchases.storeID
WHERE (tblStores.Name = inputStoreName);
I recommend checking W3 schools. They are usually helpful for most programming tasks. If you provide more info about your database, we can provide more directed help.

I want to use CONCAT and exclude and duplicate any duplicate entries

I'm trying to use the CONCAT expression, but also exclude any duplicate entries.
So I'm trying to update a report based on a single process held in our product. The problem is that whoever created the tables that the current report is pulling from is not from a single table. Currently I have found three tables that the report pulls from for one column.
SELECT concat(dbo.t_log_TaskBody.TaskDescription,' ', dbo.t_ezDocument.FileName) as Title
FROM dbo.t_logs_SigDocPrintedEmailed
LEFT JOIN dbo.t_log_Data ON t_logs_SigDocPrintedEmailed.t_ezDataPKid = dbo.t_log_Data.PKid
LEFT JOIN dbo.t_log_TaskBody ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_log_TaskBody.DocumentId
LEFT JOIN dbo.t_ezSignDocumentQ ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezSignDocumentQ.PKid
LEFT JOIN dbo.t_ezArcSigDocQLog ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezArcSigDocQLog.t_ezSignDocQPKID
LEFT JOIN dbo.t_ezDocument ON dbo.t_ezSignDocumentQ.t_ezDocument = dbo.t_ezDocument.PKID or dbo.t_ezArcSigDocQLog.t_ezDocument = EasyID.dbo.t_ezDocument.PKID
So now that I have one entry that happens to connect to two of the tables I'm pulling from to get a title of a document I end up with the title appearing twice in one box. Is there anyway I can use CONCAT to combine the two tables while keeping it to unique entries, or is there a better way of doing this.
I'll get Something along the lines of:
Title
null
Title Title
null
Title
Title
Based on your sample results, I think you want COALESCE(), not CONCAT():
concat(dbo.t_log_TaskBody.TaskDescription, dbo.t_ezDocument.FileName) as Title
You have no examples where results are actually concatenated.

Microsoft Access joining 4 tables to display specific values from 3 tables but all values from the main table

I have created a database for users to "follow" TV Shows, I need to create a form to display each show and all the relevant information to that specific show.
The four tables I have are as follows:
Shows (Main Table),
Networks,
ShowGenres (Links multiple genre's to one show),
Genres.
The relationships and all fields are shown in the image below.
Currently I have a page which displays the following information:
showID, showName, showAired, networkName, showStatus, showRuntime, showSeasons, showEpisodes, showOverview.
Ideally i'd like to have a List box to display an array of the genre's associated with the specific show. I have tried for quite a while to come up with a query to do this, the closest I managed to get showed the relevant information but added duplicate pages.
Here's my latest attempt:
SELECT * FROM Shows A
INNER JOIN Networks B ON B.networkID = A.networkID
INNER JOIN ShowGenres C ON C.showID = A.showID
INNER JOIN Genres D ON D.genreID = C.genreID;
Any help would be appreciated, thanks in advance.
One has to appreciate the presentation level logic that can be used in conjunction with the data logic. Very generically - in the 1:Many - - your query of fields that include both fields will repeat the 1 table values with each of the Many records. That can not be altered via any query design as it is inherent in the data logic.
But at the presentation level you can control the display. Using a report - the data source can be the query - but report properties offer grouping whereby you can put the 1 field value as the group header - thus displaying just once; and then below it list all the many records.
It is not a list box per se though depending on how creative one gets with reports/sub reports one could potentially make that style. But in the end you must work this at the presentation level.
One method is to use exists:
select g.*
from genres as g
where exists (select 1
from showgenres as sg inner join
shows as s
on sg.showID = s.showID
where sg.genreID = g.genreID and
s.showName = ?
);

Inner joining two tables returns empty result

I am trying to get two tables that aren't related to pull a column of data each.
I have one table called AlphaData and one called TLAuth. Each includes a column that is labelled invoice and I need to pull both columns so I can at least start a comparison. TLAuth will include some of the invoice numbers from AlphaData, but not all of them.
Right now I am using the following code:
SELECT Alphadata.Invoice, TLAuth.Invoice
FROM Alphadata
INNER JOIN TlAuth
ON TLauth.TLAUthID = Alphadata.TLAUthID;
But every time I run this it comes up totally blank. There is definitely data in there, I can pull one column of data from each, but not both at the same time. I have even setup a relationship (1 to Many from TL Auth to Alphadata) and it doesn't seem to work so any help would be grand.
If the tables could not match you should use left join
SELECT Alphadata.Invoice, TLAuth.Invoice
From Alphadata
LEFT JOIN TlAuth ON TLauth.TLAUthID=Alphadata.TLAUthID;

SQL Query Results Using Joins

I'm trying to do this query to display the names of the stores and the quantity of each book sold with only using joins but I tried to use
SELECT DISTINCT x.stor_name, t.title, s.qty
FROM stores x
INNER JOIN discounts d
ON x.stor_id=d.stor_id
INNER JOIN salesdetail s
ON d.stor_id=s.stor_id
INNER JOIN titles t
ON s.title_id=t.title_id
ORDER BY s.qty desc;
but that only displayed one of the stores results set for 'Bookbeat'.
I tried to use Left, Right & Full Outer joins to no avail so I'm wondering how I would go about doing that query to display the names for the other stores that are not displaying their result set. As there is 7 stores and only 1 is displaying it's results.
The link is a pastebin to the database.
And this is the schema.
It's hard to say without more information about your schema - it strikes me as wrong-ish that you're joining to discounts only on stor_id. I'd expect discounts to be applied to different titles, not store-wide... and I wouldn't expect discounts to be always-enabled. Try running it without the discounts inner join. Futzing around with "Distinct" and outer joins is almost always the wrong approach with things like this
I see from your profile you're a first-year. Is this schoolwork? How do I ask and answer homework questions?