I have two tables in Access, Master Data and Current Data.
They both have columns "key" in them. Master data also has "From date" and "To Date" columns. Current Data has "Creation Date" column. I need to match two tables and for every "Key" match between the tables, I need the code to take a creation date and figure out if that date is between the To and from date in the master file. If it the creation date doesnt fall between the to and from date, then I need all those rows to show up in the query.
This is what I have so far (this code is pulling the match between the tables for the key tab)
SELECT
us.*
FROM [SAP Data] us
INNER JOIN [Master Data] uss ON uss.Key = us.Key
I can't figure out how to transition from this and further tell it to match the dates and see if the date falls between to-from dates in the master file. Is there anyone who could have any guidance as to how I could proceed?
I think you just want not exists:
SELECT cd.*
FROM [Current Data] as cd
WHERE NOT EXISTS (SELECT 1
FROM [Master Data] as md
WHERE cd.Key = md.Key AND
md.[Creation Date] BETWEEN md.FromDate and md.ToDate
);
Related
I have a database in MS Office Access, used to manage stock. I have two types of transactions Addition = when there is a new item, removal = when items go out.
I need to add a section that will return remaining quantity in stock after I add or remove items. What would you suggest?
Note: I have many items, not one item
What do you mean by 'add a section'? Calculate inventory balance when needed. Search topic 'inventory balance'. Could do an aggregate query for stock received and another for stock used then use those queries in another to calculate difference. Or consider:
SELECT [Transaction Item], Sum(IIf([Transaction Type]="Addition", [Quantity],0)) - Sum(IIf([Transaction Type]="Removal", [Quantity],0)) AS Balance GROUP BY [Transaction Item];
A running balance on form is not practical (not to mention just plain hard to do) especially if records are filtered or sorted. However, you could show the net balance of each item with DSum() domain aggregate function. Try this in the [Inventory Transactions Extended] query.
Balance: Nz(DSum("Quantity","[Inventory Transactions]","[Transaction Type]=1 AND [Transaction Item]=" & [Transaction Item]),0)-Nz(DSum("Quantity","[Inventory Transactions]","[Transaction Type]=2 AND [Transaction Item]=" & [Transaction Item]),0)
The calc will update as soon as record is committed to table. Record is committed when moving to another record, closing form, or by code to save record.
I NEVER build lookups in tables. I want to see the real values when I view tables.
Don't really need to pull [Inventory Transactions] ID field individually to define sort criteria. This is primary key index and records will sort by this field by default. So pulling in the field with the query * wildcard will produce the same sort.
I am trying to sum up one field and use it in another query, but when I use the Totals button and then call that sum from the other query it considers that field as multiple instances but with the sum value in each one. How can I sum two fields in two different queries and then use those sums in another query? Note - I only separated them into 3 queries because I felt it would help me avoid "is not part of an aggregate function" errors.
Example Data
Inventory Query: This query groups by item and sums the qty_on_hand field
Item SumOfqty_on_hand
A 300
Job Material query: This query groups on the job's materials and sums up the qty_req field (quantity required to complete the job)
Item SumOfqty_req
A 500
When I make a third query to do the calculation [SumOfqty_req]-[SumOfqty_on_hand] the query does the calculation but for each record in the Job Material query.
Job Material Query
SELECT dbo_jobmatl.item,
IIf(([qty_released]-[qty_complete])<0,0,([qty_released]-[qty_complete]))*[matl_qty] AS qty_req
FROM new_BENInventory
INNER JOIN (dbo_jobmatl
INNER JOIN new_BENJobs
ON (new_BENJobs.suffix = dbo_jobmatl.suffix)
AND (dbo_jobmatl.job = new_BENJobs.job)
) ON new_BENInventory.item = dbo_jobmatl.item
GROUP BY dbo_jobmatl.item,
IIf(([qty_released]-[qty_complete])<0,0,([qty_released]-[qty_complete]))*[matl_qty];
Inventory Query
SELECT dbo_ISW_LPItem.item,
Sum(dbo_ISW_LPItem.qty_on_hand) AS SumOfqty_on_hand,
dbo_ISW_LP.whse,
dbo_ISW_LPItem.hold_flag
FROM (dbo_ISW_LP INNER JOIN dbo_ISW_LPItem
ON dbo_ISW_LP.lp_num = dbo_ISW_LPItem.lp_num)
INNER JOIN dbo_ISW_LPLot
ON (dbo_ISW_LPItem.lp_num = dbo_ISW_LPLot.lp_num)
AND (dbo_ISW_LPItem.item = dbo_ISW_LPLot.item)
AND (dbo_ISW_LPItem.qty_on_hand = dbo_ISW_LPLot.qty_on_hand)
GROUP BY dbo_ISW_LPItem.item,
dbo_ISW_LP.whse,
dbo_ISW_LPItem.hold_flag
HAVING (((Sum(dbo_ISW_LPItem.qty_on_hand))>0)
AND ((dbo_ISW_LP.whse) Like "BEN")
AND ((dbo_ISW_LPItem.hold_flag) Like 0));
Third Query
SELECT new_BENJobItems.item,
[qty_req]-[SumOfqty_on_hand] AS [Transfer QTY]
FROM new_BENInventory
INNER JOIN new_BENJobItems
ON new_BENInventory.item = new_BENJobItems.item;
Please note that anything that starts with dbo_ is a prefix for a table that sources the original data.
If any more clarification is needed I would be more than happy to provide it.
Looks like you need a GROUP BY new_BENJobItems.item on your final query along with a SUM() on the quantity. Or to remove the IIf(([qty_released]-[qty_complete])<0,0,([qty_released]-[qty_complete]))*[matl_qty] from your Job Material query. Or both. As written, the Job Material Query is going to return a record for every different key value in the joined input tables that has a distinct quantity, which doesn't seem like the granularity you want for that.
Im a little new to Acess and am trying to understand how to merge tables.
My problem is basically this...
I have 2 reports that can be ran and exported to excel. I want to be able to take both reports, combine them, and import the combined result to a table.
In one report.. call it location detail... has the following fields:
ID (PK, and autonumber)
Day (Date call was done)
CallID (Resets every day to 1, and increases by 1 for every new call)
Lat (Lattitude of call)
Long (Longitude of call)
Cost
In the other report.. call it location summary.. has the following fields:
ID (PK, and autonumber)
Location
City
Lat (Lattitude of call)
Long (Longitude of call)
# of Occurences (Number of times there was a call at that location)
The problem is when you have a call in the same location, even though it was on two seperate days, it automatically groups it into the number of occurances. So I tried to run 2 seperate queries from the same range of dates, one matching lat, other matching long, it shows up with duplicates.. my guess is because the ones that do not have a matching lat (because it already used the field once, even though there were 4 occurences) are showing null value and are added to the query as a duplicate.
I want to add the City Column that matches the long/lat data, to the location details report, and then make that into a new table/ and then be able to run the same reports, and add them to the new table, but somehow ensure there arent any duplicates.
My biggest problem is there isnt any real primary key since the Call ID is Dependent on the Date.... I am not sure how to really accomplish this.
Any help would be much appreciated... I am Stumped.
EDIT:
The first Query is to join lattitudes:
SELECT
[Location Detail Report_142].Day, [Location Detail Report_142].[Call ID],
[Location Detail Report_142].Lat, [Location Detail Report_142].Long
FROM [Location Detail Report_142]
LEFT JOIN [Location Summary Report_14]
ON [Location Detail Report_142].Lat = [Location Summary Report_14].Lat;
The second one matches call id:
SELECT qryRelationshipToLat.Day, qryRelationshipToLat.[Call ID],
[Location Summary Report_14].City
FROM [Location Summary Report_14]
INNER JOIN qryRelationshipToLat
ON [Location Summary Report_14].Long = qryRelationshipToLat.Long
ORDER BY qryRelationshipToLat.Day, [Location Summary Report_14].City;
I was thinking maybe it could be possible to make some sort of if-then statement stating that if call id shows duplicate THEN match Call ID... It has to match both, when the original Location Summary report is uploaded, it has 283 reccords, but it shows duplicate locations grouped into anouther field (#_of_Occurances), so the total amount of calls would be the total amount in the Location Detail report, witch in this case is 288. so when I run the first qry - 305 results, and the second qry turns the 305 to 337. So I end up with a lot of just duplicates I guess, or null value ones.
But the two reports both don't include Call ID or Call Numbers. The only information that they share is the LONG and LAT Coordinates. So I would have to match those first, then assign call ID, date, and city to the corresponding coordinates.
I think that you need to learn how to use a GROUP BY statement to aggregate your table. These links may help:
http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server
http://beginner-sql-tutorial.com/sql-group-by-clause.htm
It's possible also that your table designs have to be revised, and presumably your approach for adding to the call record as well.
I'm sorry to just throw some links at you, but I'm not quite up to analyzing what you have got in order to construct a complete answer, and in any case I think you will have to tackle these areas of learning.
I have a form called “Search” with the question regarding the section under Transaction Search. It invokes a query based on the selection of full name (Combo40) which pulls from a table called “Individuals” and the committee(s) the user selected in Combo40 has contributed to in the Committee field:
Committee Query:
SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions.[Committee Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=[Forms]![Search]![Combo40]));
I’m trying to add an additional parameter that also compares the year in which an individual gave which is a specific filed in the Transactions table called Combo46.
New Query:
SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions. [Committee Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40) And (((Transactions.Combo46)=Forms!Search!Combo51)))
Forms!Search!Combo40 = pulls the values from a table called “Individuals”
Transactions.Combo46 = year (i.e. 2011, 2012, 2013 etc.)
Forms!Search!Combo51 = year (i.e. 2011, 2012, 2013 etc.)
When I make my selections on the form I get a window that asks me to enter the value for Combo 46 and the committee list displays all records for that individual and not just transactions with the year I entered. I would expect it to not come up with a pop-up at all but instead take the value from Combo46 in the Transactions table and compare it to the value entered in Combo51.
It's too long and unclear for a comment, so I use this anwser space:
I suppose that the table column Transactions.Combo46 does not exist, so Transactions.Combo46 is popuped by Access for Input, it should be Transactions.Year or something meaning this:
SELECT Transactions.[Committee Name]
FROM Transactions
GROUP BY Transactions.[Committee Name], Transactions.[Employee Name]
HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40)
And (((Transactions.Year)=Forms!Search!Combo51)))
What jacouh says is surely the answer (i.e., your SQL is referring to Combo46 as a member of Transactions when it is a member of Forms!Search). As an appendum, I'd simplify things a bit like this:
(1) Name the combo boxes something sensible (e.g. cboName and cboYear) rather than leaving them as Combo40 and Combo51 (yuck!).
(2) The GROUP BY seems unnecessary to me:
SELECT DISTINCT [Committee Name] FROM Transactions
WHERE ([Employee Name] = Forms!Search!cboName) AND ([Year] = Forms!Search!cboYear)
I've searched for an answer to this, but can't seem to find anything that works to do a unique count in crosstabs. The closest I could find was: select count(myField) from (select distinct myField from myTable), but I keep getting errors.
The training type is in columns, the dates in rows (formatted to month so I can separate out quarters or individual months), and ideally I would have the values as number of unique trainees that are identified by their Grower Record IDs. It currently displays all trainees who have been to the same training multiple times.
Any help would be greatly appreciated! Below is the SQL. Thanks for your time!
SQL:
TRANSFORM Count([TDB - Master - Trainee Attendence].[Grower Record ID]) AS [CountOfGrower Record ID]
SELECT Format([Dat Fòmasyon],'mm/yy') AS Months, Count([TDB - Master - Trainee Attendence].[Grower Record ID]) AS [Total Of Grower Record ID]
FROM [TDB - Master - Trainee Attendence]
WHERE (((Format([Dat Fòmasyon],'mm/yy'))="01/13" Or (Format([Dat Fòmasyon],'mm/yy'))="02/13"))
GROUP BY Format([Dat Fòmasyon],'mm/yy')
PIVOT [TDB - Master - Trainee Attendence].[Tit Fòmasyon an];
I would suggest that you create a query of the data you want to report (presumably):
SELECT DISTINCT [Grower Record ID], [Dat Fomasyon], [Tit Fomasyon an]
FROM [TDB - Master - Trainee Attendance]
Then generate a pivottable using this query as the data source.
Now I am not completely familiar with access's nuances, and dont know for certain that you can generate a pivottable from a query, but if not, I'd suggest changing your code (in a clone of the form) to do the same thing, replacing the from clause with a [aliased] query (and removing the references to the base table appearing in other clauses):
TRANSFORM Count([Grower Record ID] AS [CountOfGrower Record ID]
SELECT Format([Dat Fomasyon], 'mm/yy' AS Months, Count([Grower Record ID]) as [Total of Grower Record ID]
FROM (SELECT DISTINCT [Grower Record ID], [Dat Fomasyon], [Tit Fomasyon]
FROM [TDB - Master - Trainee Attendance]) Uniq
WHERE Format([Dat Fòmasyon],'mm/yy') = "01/13" Or Format([Dat Fòmasyon],'mm/yy' = "02/13"
GROUP BY Format([Dat Fòmasyon],'mm/yy')
PIVOT [Tit Fòmasyon an]
Note, I couldnt help thinking while rewriting the logic that it has been editted since being generated, as the name of the count (in [] after "AS") is different in the transform and select lines. I wouldnt expect this to matter, though in access you never know.
Hope this helps...
Please mark as answer if this helps you.