I have the sql code below. It works but when I run the query, it repeats the data more than one time. I believe it has something to do with the Inner Join. Since it is Inner Joining twice the data is repeating twice in the report. Any help is appreciated
SELECT NewRelative.FullName, GiftGave.EventName, GiftGave.EventDate, GiftGave.AmountGiven, GiftGave.Notes, GiftRcvd.EventName
FROM (NewRelative INNER JOIN GiftGave ON NewRelative.FullName = GiftGave.FullName) INNER JOIN GiftRcvd ON NewRelative.FullName = GiftRcvd.FullName
WHERE (((NewRelative.FullName)=[forms]![FormRelativeReport]![TxtPickRelative]));
Related
I have 3 tables, CASES, REPORTS and REPORT_DETAIL
My current report shoes all REPORTS where there is a matching CASE. This is accomplished by
SELECT * FROM CASES
JOIN REPORTS
WHERE CASES.ID = REPORTS.ID
I am looking to extend this to now show additional report detail if it is available. So show the same report, but if there is any report detail available, then tag that on the end. I have tried a series of LEFT and RIGHT JOINS but am not getting the required dataset.
Could it be that I need to to the RIGHT JOIN once the rest of the SQL has run, and that the SQL is getting confused with where the JOIN should be?
I have tried the following but have not managed to get it to work.
SELECT * FROM CASES
JOIN REPORTS ON CASES.ID = REPORTS.ID
RIGHT JOIN REPORT_DETAIL ON REPORTS.DETAILID = REPORT_DETAIL.DETAILID
I think you want LEFT JOIN:
SELECT *
FROM CASES C JOIN
REPORTS R
ON C.ID = R.ID LEFT JOIN
REPORT_DETAIL RD
ON R.DETAILID = RD.DETAILID;
This keeps all rows returned by the first JOIN, along with additional columns from any matching REPORT_DETAIL records.
MS Access has been just killing me when It comes to writing my own SQL queries and I hate it's GUI query tool. I gave up trying to combine LEFT JOINS and INNER JOINS now I'm doing all inner join's and i'm still getting murdered. Can someone link to me to a comprehensible MS Access SQL guide and/ or tell me what I am doing wrong here. I've tried putting my code in parenthesis, bracketing my fields. I'm at my wits end.
SELECT answers.*
FROM (((answers
INNER JOIN caseInfo
ON answers.[ABAWDNum] = caseinfo.[ABAWDNum])
INNER JOIN Questions)
ON answers.[questionID] = questions.[questionID])
INNER JOIN responseCodes
ON answers.[responseIDCode] = responseCodes.[responsecode]
I'm not sure if my picture of the error is coming up but it says Sytnax error in FROM clause and it has the parenthesis right after the Questions table is mentioned
enter image description here
EDIT: Database relationships:
enter image description here
Access INNER JOIN can only work on two data sets at once, so if you want to join more than two tables, you have to nest the INNER JOINS using brackets, as described here:
https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb208854(v=office.12)
From that page it says the syntax to do this is...
SELECT fields FROM table1 INNER JOIN (table2 INNER JOIN [( ]table3 [INNER JOIN [( ]tablex [INNER JOIN …)] ON table3.field3compoprtablex.fieldx)] ON table2.field2compoprtable3.field3) ON table1.field1compoprtable2.field2;
I believe this will give you all fields from the table answers that meet your join conditions.
SELECT answers.*
FROM answers
INNER JOIN caseInfo
ON answers.[ABAWDNum] = caseinfo.[ABAWDNum]
INNER JOIN Questions
ON answers.[questionID] = questions.[questionID]
INNER JOIN responseCodes
ON answers.[responseIDCode] = responseCodes.[responsecode]
Just to humor Access I used the GUI/Design mode to build a query.
Here's the query I got that ran. From a table with 674 records, it only returned 6 answers, but that is a separate problem that I have to deal with. At least this thing runs.
But I have a feeling once I start asking for left joins I'm in trouble. <_< F* access.
SELECT answers.*
FROM (Questions
INNER JOIN (CaseInfo
INNER JOIN answers ON CaseInfo.ABAWDNum = answers.ABAWDNum)
ON Questions.questionID = answers.questionID)
INNER JOIN responseCodes ON (Questions.questionID = responseCodes.questionCode)
AND (answers.responseIDCode = responseCodes.responseCode);
EDIT:
I used the GUI/Designer again to try to make a query that was closer to what I needed, which is all of the answers in the answers table plus just a few things from the question table, caseinfo table, and responsecode tables , and this is what I got
SELECT
CaseInfo.ABAWDNum, CaseInfo.Admin, CaseInfo.Unit,
CaseInfo.[ASV-DRV Completed By], CaseInfo.reviewerID, CaseInfo.[Recieved Date],
CaseInfo.[Date Reviewed],
Questions.Question,
answers.responseIDCode, responseCodes.responseDescription, answers.comments
FROM
CaseInfo
RIGHT JOIN
((Questions
RIGHT JOIN
answers ON Questions.questionID = answers.questionID)
LEFT JOIN
responseCodes ON answers.responseIDCode = responseCodes.responseCode)
ON CaseInfo.ABAWDNum = answers.ABAWDNum;
This is really insane. I'm going to have to study this really hard. It looks like access is very particular on how it wants it's joins, but this seems to give me the correct results. 674 records returned.
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
I need to get values like
AdviserInit, AdviserSurname, MaterialName and Area using AdviserID, MaterialID and AreaCode from Chores Table as my reference.
I don't know what will be my approach in creating the query. This is what I've come up with so far:
SELECT Chores.ChoresID, Chores.ChoresDate, Materials.MaterialName,
Advisers.AdviserInit, Advisers.AdviserSurname, Area.Area,
Chores.StudentID
FROM Chores INNER JOIN Materials on Chores.MaterialID = Materials.MaterialID
INNER JOIN Advisers on Chores.AdviserID = Advisers.AdviserID
INNER JOIN Area on Chores.AreaCode = Area.AreaCode
Here's the screenshot of the relationships between the tables:
You can try this query, if you want to map all tables with main Chores Table,
SELECT Chores.ChoresID, Chores.ChoresDate, Materials.MaterialName,
Advisers.AdviserInit,Advisers.AdviserSurname, Area.Area,
Chores.StudentNo, Student.Forename
FROM (((Chores INNER JOIN Materials on Chores.MaterialID = Materials.MaterialID)
INNER JOIN Advisers on Chores.AdviserID = Advisers.AdviserID)
INNER JOIN Area on Chores.AreaCode = Area.AreaCode)
INNER JOIN Student on Student.StudentNo = Chores.StudentNo
The placement of the parentheses is important here. Basically, you need to have n - 2 left parentheses after the from clause and one right parenthesis before the start of each new join clause except for the first, where n is the number of tables being joined together.
The reason is that Access's join syntax supports joining only two tables at a time, so if you need to join more than two you need to enclose the extra ones in parentheses.
Ok.. So I'm trying to improve my SQL skills and have a question. Here is a screen shot of the schema.
Schema http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif
(http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif)
Alright so I'm selecting a bunch of Report Bundles and other rows from a table you can see. I've got these two tables joining together correctly and displaying what should be returned. Now I need to add another field onto my result rows that states what type of report this is. How can I join up to the ReportGroupType table through the ReportBundleGroup table without getting a shwack of results?
Here is the query I am using so far.
SELECT *
FROM ReportBundleCustomerVisibility INNER JOIN ReportBundle
ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
Thanks again SO
SELECT *
FROM ReportBundleCustomerVisibility AS v
JOIN ReportBundle AS b ON b.ID = v.ReportBundleID
JOIN ReportBundleGroup AS g ON b.ID = g.ReportBundleID
JOIN ReportGroupTYpe AS t ON t.ID = g.ReportGroupTypeID
WHERE v.ReferenceCustomerID = 2303
It sounds like you just need another inner join to get the information you need. You can think about the second join as joining the result of the join with the ReportGroupType table. I added parenthesis to try to join the two sets the second INNER JOIN is operating on.
SELECT * FROM ((ReportBundleCustomerVisibility
INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID)
INNER JOIN ReportGroupType ON ReportBundleGroup.ReportGroupTypeID = ReportGroupType.ID)
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
I also highly suggest against using "SELECT *" in production code or any query you plan on reusing as the table schema can change and possibly effect reports and UI. Explicitly specify the columns instead.