I have an MS-Access query that I am trying to design with little luck. I have 2 columns...one of check numbers and one of invoice numbers. For every check number there has to be an invoice number. The columns look like this...
Check# Invoice#
012564
012564 PC0935
012564 PC0935
012564
I need to fill in the Invoice# wherever it is missing on the check.
I can get it to work as an index/match with a vlooup in Excel but I need to be able to do this in Access.
I have tried dlookups with replace but no luck. I am not looking for the absolute answer but some ideas in the general direction would be most appreciated!
I have no knowledge of VBA and very limited SQL.
Based on my comment that you want the table updated so that each Check# entry has a corresponding Invoice# entry and there will always be at least one entry in that table that does have the 'Check# ~ Invoice#` pairing so use that pairing to complete the empty records.
e.g. Check 012564 has Invoice# PC0935, and 012565 has PC0939 in at least one record. We just need to complete the blanks for those Check numbers.
All records will have a Check #.
This SQL should do it:
UPDATE Table1 T1 INNER JOIN Table1 T2 ON T1.Check = T2.Check
SET T1.Invoice = T2.Invoice
WHERE T1.Invoice IS NULL AND NOT T2.Invoice Is Null
Related
I have an MS Access related question. I have a table that looks like this:
Table1
What I need to do is this:
1. Find deadline 29.01.2017 (first month of the year),
2. look at the corresponding AccountNumber and SubAccountNumber,
3. find deadline 28.02.2017 (so the next month) that matches with the AccountNumber and SubAccountNumber of the previous month,
4. subtract the Amounts,
5. and display the result.
In this example (Table 1): The first deadline of 29.01.2017 has the AccountNumber 1 and SubAccountNumber 23 as well as the Amount 400€. Looking for the next month that matches AccountNumber and SubAccountNumber, we find the value 300€. I would now have to subtract 300€ from 400€ and put this in the field that I have added (see also Table 2).
What I have done so far is concatenate the two attributes AccountNumber and SubAccountNumber, so my table now looks like this:
Table2
I thought it would be easier this way, because now I can look for the first month/deadline (29.01.2017), get the corresponding CONCAT, look for the next month/deadline that has the same CONCAT, and subtract the amounts.
Seeing as I am new to Access though, I am clueless as to how to go about this. Any hint would be greatly appreciated. If you think that my approach using a concatenation is not target-aimed or if I haven't made the problem clear enough, let me know.
PS I read online that Access offers an if-else statement - would that be an idea?
That should get you there or very close:
SELECT T1.ACCOUNTNUMBER, T1.SUBACCOUNTNUMBER, T1.AMOUNT-T2.AMOUNT AS AMOUNTDIFF
FROM Table1 T1
INNER JOIN Table1 T2
ON T1.ACCOUNTNUMBER=T2.ACCOUNTNUMBER
AND T1.SUBACCOUNTNUMBER=T2.SUBACCOUNTNUMBER
AND DateDiff('m',DateAdd('d',1,T1.DEADLINE), DateAdd('d',1,T2.DEADLINE))=1
WHERE T1.DEADLINE=#29.01.2017#
Make sure whatever date format you use with WHERE is a valid date format for your locale. Obviously substitute Table1 with the actual name of your table.
I do not have an easy way of testing this so there may be typos.
I have a Microsoft Access table of data with 3 fields: "part_number", "date_of_price_change" and "new_price", but I need to convert the "new_price" field to show the "price_change", rather than the full "new_price" of each part on each date.
This will obviously involve some process that looks at each unique part number on each date and looks up the price of the record with the same part number with the next earliest date and deduct the prices to get the price change.
Problem is, I have no idea how to do this in Access and there are too many records for Excel. Can anyone assist with how to do this in Access? (Note that date changes can happen any time and are not periodic).
Many thanks in advance.
Ana
Add the new column price_change as a Money data type, then run a query something like below. Make sure you backup the table first with an APPEND table to a new table, just in case. Since it is a new column i may not matter.
UPDATE
T1
SET
T1.price_change = T1.new_price - Nz((SELECT top 1 T2.new_price from MyTable T2 WHERE T2.part_number = T1.Part_Number ORDER BY date_of_price_change DESC),0)
FROM
MyTable T1
I am trying to create an UPDATE query that will replace the NameID field on a table called TimeStamps with the user's id field value from another table called Names. Here is some sample data.
Names Table
id:1
name:John
password:1234
TimeStamps Table
id:1
name:**John**
timestamp:01/01/2000 12:00:00
I want to replace the Name field in the TimeStamps table with the corresponding id value from the Names table. I'm not entirely sure on how to write the query but I know it starts with something like this.
UPDATE TimeStamps
SET NameID=(NamesTableReference)
WHERE NameID=(TimeStampsTableReference);
This way I can start to run queries against the user's id and not the user's Name. We have more than one user with the same name and it doesn't pull the right data because it is pulling multiple users. I can update the data manually but there are several hundred thousand rows in the table and that would take entirely too much time. Can anyone shed some light on what I need to add/change in the query below? Thanks!
I think you just want a join:
update timestamps t join
names n
on t.id = n.id
set t.name = n.name;
However, the update shouldn't be necessary. You have an id connecting the two tables. Just use it when you are using timestamps. That is, use a join to look up the name rather than storing it in both places -- and running the risk that the names associated with an id somehow end up different in the two tables.
It looks like this query did the trick.
UPDATE TimeStamps t, Names n
SET t.NameID = n.id
WHERE t.NameID = n.Name
Thanks everyone for your input and your help! Hope this can help someone else in the future!
Working with MS Access for the first time and coming across a few problems if someone could please point me in the right direction.
So I'm doing a mock database (so it looks silly) just to learn the ins and outs and need some help with DLookUp at the moment.
My database has two tables, with the following fields:
C_ID the PK in Courses and FK in Student
tblCourse: C_ID, Title, Subject
tblStudent: S_ID, C_ID, Name, EnrollDATE
As I said this is just for testing/learning. So what I want is to have a filter that gives me a list of C_ID's based on which EnrollDates are NULL.
so filter is:
Expr1: DLookUp("[tblStudent]![C_ID]","tblStudent","isNull([tblStudent]![EnrollDATE])")
I have also tried with the criteria being
[tblStudent]![EnrollDATE] = Null
Currently I get just blank fields returned. Any help is greatly appreciated, and please ask me to elaborate if my explanation is off.
Thank You!
The correct syntax looks like this:
DLookup("C_ID", "tblStudent", "EnrollDate is null")
You don't need to include the table name when you specify the columns
In Access, you check for Null by using xxx is null or xxx is not null
Note that DLookup only returns one value (if the criteria matches more than one row, the value is taken from any row), so you can't use it to get a list of C_IDs back.
EDIT:
What you actually want to do is select data from one table, and filter that based on data from the other table, correct?
Like, selecting all courses where at least one student has an empty EnrollDATE?
If yes, you don't need the DLookup at all, there are two different ways how to do that:
1) With a sub-select:
select *
from tblCourse
where C_ID in
(
select C_ID
from tblStudents
where EnrollDATE is null
)
2) By joining the tables:
select tblCourse.*
from tblCourse
inner join tblStudent on tblCourse.C_ID = tblStudent.C_ID
where tblStudent.EnrollDATE is null
This is SQL, so you need to switch to SQL View in your query in Access.
I'm sure there are a ton of ways to do this, but right now I'm struggling to find the way that will work properly given the data.
I basically have a table containing duplicates which have additional fields tied to them and source details that take priority over others. So basically I added a "priority" field to my table which I then updated based on source priority. I now need to select the distinct records to populate my "unique" records table (which I'll then apply unique key constraint to prevent this from happening again on the field required!)....
So I have basically, something like this:
Select phone, carrier, src, priority
from dbo.mytable
So basically I need to pull distinct on phone in order of priority (1,2,3,4, etc), and basically pull the rest of the other data along with it and still keep UNIQUE on phone.
I've tried a few things using sub-select from the same table with min(priority) value, but outcome still doesn't seem to make sense. Any help would be greatly appreciated. Thanks!
EDIT I need to dedupe from the same table, but I can populate a new table with the uniques if needed based on my select statement to pull the uniques. This is in MSSQL, but figured anyone with SQL knowledge could answer.
For example, let's say I have the following rows:
5556667777, ATT, source1, 1
5556667777, ATT, source2, 2
5556667777, ATT, source3, 3
I need to pull uniques based on priority 1 first..... the problem is, I need to remove any all other dupes from the table based on the priority order without ending up with the same phone number twice again. Make sense?
So you're saying the combination (phone, priority) is unique in the existing table, and you want to select the rows for which the priority is smallest?
SELECT mytable.phone, mytable.carrier, mytable.src
FROM mytable
INNER JOIN (
SELECT phone, MIN(priority) AS minpriority
FROM mytable
GROUP BY phone
) AS minphone
ON mytable.phone = minphone.phone
AND mytable.priority = minphone.minpriority