I have two tables, Actual Use and Budget. I need to update my [Actual Use].Goals with my monthly Budget.goals. I have an update query:
UPDATE [Actual Use]
INNER JOIN Budget ON [Actual Use].Property_ID = Budget.Property_ID
SET
[Actual Use].Goal = [Budget].Goal
WHERE
[Actual Use].Date = [Budget].Date
This query updates my Actual Use table but only for one month. 1/1/2016. Both Actual Use and Budget have a date field and all dates are entered with the first of each month so 1/1/2016, 2/1/2016 etc... Why is my update only working on one month and not every month where the Property_ID and month are the same on both tables?
Edit
The Acutal Use Table has the following fields in this order
Property_Id, Date, Use, Goal and the Budget Table has Property_ID, Date, Goal
I agree with the comment by Olivier. You need to join on both ID and Date.
Depending on what your table keys are, the above listed query could produce a One to Many or Many to Many result, in which, the program doesn't know which goal to assign. I suppose the Where clause may catch the many to many circumstance, but in my opinion, could produce some weird behavior.
Possible solution:
UPDATE [Actual Use]
INNER JOIN Budget ON [Actual Use].Property_ID = Budget.Property_ID
AND [Actual Use].Date = Budget.Date
SET
[Actual Use].Goal = [Budget].Goal
On a side note, it seems like the data is a bit repetitious? Presuming there was no 'goal' in table [Actual Use], you could still reference the data by the following:
SELECT * FROM [Actual Use] au
JOIN Budget b on au.Property_ID = b.Property_ID AND
au.Date = b.Date
Related
This concerns two tables I have in Access:
A Transactions table
An Invoice Table
Every entry has a Contract Number, under a contract number can be many invoices (which is why there is a separate invoice table linked by contract number to the transactions table). When I have a biller that misses billing an invoice on a rented out piece of equipment I want to make this evident to them by having a query that returns contract numbers that (are on rental but do not have an invoice date between [start date] and [end date]
So far I made a query that lists all invoices that have been billed, but ran into a mental block on how to list the ones that don't have a record created containing x date.
EG:
SELECT
Transactions.[Contract Number]
, Transactions.[Rented To]
, tblInvoice.InvoiceDate
, Transactions.[Rental Start Date]
FROM Transactions
INNER JOIN tblInvoice ON Transactions.[Contract Number] = tblInvoice.ContractID
WHERE (((Transactions.[Rental Start Date])<>0)
AND ((Transactions.[Rental Return Date]) Is Null));
This gets me a list of the invoices with the dates, but I need to find the ones that don't have a record containing date parameters and list those contract numbers.
My task is to produce a report that shows the on time delivery of products to consumers. In essence I have achieved this. However, as you will see only some of the data is accurate.
Here is our test case: we have a sales order number '12312.' This sales order has had 5 partial shipments made (200 pieces each). The result is shown below from our DUE_DTS table.
Due Dates table data
The following code gives me the information I need (excluding due date information) to show the packing details of the 5 shipments:
DECLARE #t AS TABLE (
CUSTNAME char(35),
SONO char(10),
INVDATE date,
PACKLISTNO char(10),
PART_NO char(25),
SOBALANCE numeric(9,2)
)
INSERT INTO #t
SELECT DISTINCT c.CUSTNAME, s.SONO, p.INVDATE, p.PACKLISTNO, i.PART_NO, q.SOBALANCE
FROM [manex].[dbo].[SODETAIL]
INNER JOIN [manex].[dbo].[SOMAIN] s ON s.SONO = SODETAIL.SONO
INNER JOIN [manex].[dbo].[CUSTOMER] c ON c.CUSTNO = s.CUSTNO
INNER JOIN [manex].[dbo].[INVENTOR] i ON i.UNIQ_KEY = SODETAIL.UNIQ_KEY
INNER JOIN [manex].[dbo].[DUE_DTS] d ON d.SONO = s.SONO
INNER JOIN [manex].[dbo].[PLMAIN] p ON p.SONO = s.SONO
INNER JOIN [manex].[dbo].[PLDETAIL] q ON q.PACKLISTNO = p.PACKLISTNO
WHERE s.SONO LIKE '%12312'
SELECT * FROM #t
Here is a screenshot of the results from running this query:
Query Result
Now is when it should be time to join my due dates table (adding in the appropriate column(s) to my table definition and select statement) and make DATEDIFF comparisons to determine if shipments were on time or late. However, once I reference the due dates table, each of the 5 shipments is compared to all 5 dates in the due dates table, resulting in 25 rows. The only linking column DUE_DTS has is the SONO column. I've tried using DISTINCT and variations of the group by clause without success.
I've put enough together myself to figure joining the DUE_DTS table on SONO must be causing this to happen, as there are 5 instances of that value in the table (making it not unique) and a join should be based on a unique column. Is there a workaround for something like this?
You will need to use additional fields to join the records and reduce the results. You may need to link SONO to SODETAIL to DUE_DTS because the dates are tied to the items, not to the SONO.
I have two tables in my Firebird DB. The first table is called LEDGER and is showing the details of my transaction, including the date of of a transaction (yyyy/mm/dd). The second table is called PERIODS and consists of the following fields:
Period number (INTEGER)
Period start date (yyyy/mm/dd)
Period end date (yyyy/mm/dd)
I want use the date as per the LEDGER table to look up the corresponding period from the PERIODS table and join the two tables based on that. I short, I want to show the period for each line of the ledger table to assist me in preparing consolidated reporting.
Herewith an example of the PERIODS table:
What would be the best approach to do this?
If I was the developer I would have added the period to the ledger table and populate the period on transactional level but unfortunately it is too late for that now.
Any assistance will be greatly appreciated.
You can just use join:
select l.*, p.per
from ledger l left join
periods p
on l.date_of_transaction >= p.start_date and
l.date_of_transaction <= p.end_date
Use below query. Please change the columns names, i have provided the column names as an example.
select P.PER, L.* from LEDGER L
inner join PERIODS P
on (L.date_of_transaction = P.start_date);
select P.PER, L.* from LEDGER L
inner join PERIODS P
on (L.date_of_transaction between P.start_date);
On my Left Table I have all EMP Information joint with all possible Asset, SO all Employees are assigned to take all courses listed in the database. AND the Right Table I have Only Employees who has taken or in process of taking the course.( Both tables are queries and their results are correct)
I would like to do Left join between two tables (queries) so I get All Employees from Left-Table joint Right-Table with new columns (Completion date & Completion status)!
However, when I do left-join it returns all my rows and fills the blanks with the two possibilities for Completion Status which is Completed OR in Progress). The most stupid thing happens when I add Completion date and returns some random dates and fills all rows for Completion Dates and repeats those rows for following asset titles.
My result should be a list of all EMP joint with required courses with the status of course, this person has completed the course or not and I would like to get null for all those non related rows. Will you be kind and check my code and let me know what can cause this problem? Thank you
SELECT qryEmployeeCourse.[EMP ID], qryEmployeeCourse.Name, qryEmployeeCourse.Role, qryEmployeeCourse.Location, qryEmployeeCourse.Region, qryEmployeeCourse.[Asset ID], qryEmployeeCourse.[Asset Title], qryCourseStatus.[Completion Status]
FROM qryEmployeeCourse LEFT JOIN qryCourseStatus ON qryEmployeeCourse.[EMP ID] = qryCourseStatus.Username
GROUP BY qryEmployeeCourse.[EMP ID], qryEmployeeCourse.Name, qryEmployeeCourse.Role, qryEmployeeCourse.Location, qryEmployeeCourse.Region, qryEmployeeCourse.[Asset ID], qryEmployeeCourse.[Asset Title], qryCourseStatus.[Completion Status]
ORDER BY qryEmployeeCourse.Name, qryEmployeeCourse.Role, qryEmployeeCourse.Location;
http://i.stack.imgur.com/0zNVz.png here is the pic of my tables and result.
It looks to me that your LEFT JOIN should be
qryCourseStatus ON qryEmployeeCourse.[EMP ID] = qryCourseStatus.Username AND qryEmployeeCourse.AssetID = qryCourseStatus.AssetID
Otherwise you will be getting a cross join (all rows in qryEmployeeCourse with all rows in qryCourseStatus, for each employee ).
In the first set of results in your screenshot, you are only getting two rows because the employees records in the qryCourseStatus only has two distinct statuses Completed and In progress, and your group by is removing all the other duplicates.
You get a lot more rows in the second set of results, because you are adding Completion Date which has 7 distinct values for this employee.
I'm just starting out with SQL, I have been playing around with simple select queries and grouping data, now I want to pull some actually useful data out of our database for analysis. The data is organized as follows:
Access 2010 Database
I didn't set it up, I know it isn't set up as it should be
I can't change data, only poll
-Customers are kept in one table
-Closed orders are kept in another table (each line item is listed with invoice #, date closed, Customer ID as well as other info)
-Archived closed orders table keep sales records that are a year + old (table is laid out exactly the same as Closed order table)
I want to start with a simple query, list all the customers from a certain branch and their past year totals. Here's what I have tried:
SELECT CUSTOMERS.Company, CUSTOMERS.[Ship City], (SELECT SUM (CLOSEDORDERS.Quant*CLOSEDORDERS.SellPrice) FROM CLOSEDORDERS WHERE CUSTOMERS.ID = CLOSEDORDERS.CustID) AS LifeTotal
FROM CUSTOMERS, CLOSEDORDERS
WHERE CUSTOMERS.Branch=33;
When I run the query, it asks me to enter a parameter value for CLOSEDORDERS.Quant. What am I doing wrong?
I think this is what you're looking for with an OUTER JOIN:
SELECT CUSTOMERS.Company,
CUSTOMERS.[Ship City],
SUM(CLOSEDORDERS.Quant*CLOSEDORDERS.SellPrice) AS LifeTotal
FROM CUSTOMERS
LEFT JOIN CLOSEDORDERS ON CUSTOMERS.ID = CLOSEDORDERS.CustID
WHERE CUSTOMERS.Branch=33
GROUP BY CUSTOMERS.Company,
CUSTOMERS.[Ship City]
If you only want to return matching results from both tables, then use a standard INNER JOIN instead of the LEFT JOIN.