Access SQL: I am trying to update a table with values from the same table - sql

I am trying to import a CSV file exported from Ebay into MS Access.
Ebay cram into one file what should be in two files. So if someone purchases two products, Ebay puts the customer details in one row and the products bought in the following lines.
Customer cruze130613 bought two products, so has information spread over three lines!
What I would like to do is fill the empty User ID column blanks.
So:
Set [User ID] equal same 'User ID' as where [Sales record number] = [Sales record number]
I think the select clause for [Sales record number] should use DISTINCT as is duplicated on >1 rows.
Any help with the SQL code will be appreciated.

Perhaps something along the lines of the following:
update YourTable t1
set t1.[user id] = dlookup
(
"[user id]",
"YourTable",
"[user id] is not null and [sales record number]=" & t1.[sales record number]
)
where t1.[user id] is null
Change both references of YourTable to the name of your table.
Here, the domain aggregate function DLookup is used to acquire the value of the User ID field for a record meeting the supplied criteria (in this case where User ID is not null, and where the sales record number is equal to that of the record being updated).
There are other ways to achieve the same result, but DLookup is used here to retain the 'updateability' of the query in MS Access.

Thank you Lee for your suggestion.
As far as I can see Ebay only export these pseudo CSV files.
I had another look today and managed to find a solution. I wonder which would be more efficient yours or mine?
UPDATE tEbImportRaw AS eir1
INNER JOIN tEbImportRaw AS eir2
ON eir1.[Sales Record Number] = eir2.[Sales Record Number]
SET eir1.[User ID] = eir2.[User ID];

Related

Access Update Query Updating Both Tables of the SET Statement

I have created an update query in MS Access. In the query there are two tables. One is the table I want to update (000DeleteMeNames), and the other is a linked table (it's actually a list) in Sharepoint (Work Orders). Every time I run the query, it does what it is supposed to and updates 000DeleteMeNames like it's supposed to, however it also modifies the joined records in the Sharepoint list Work Orders. No actual changes are made to the Sharepoint records, however the modified date changes and an alert is sent to me that the Sharepoint records have changed.
UPDATE 000DeleteMeNames INNER JOIN [Work Orders]
ON WO_Num = [Work Orders].[Work Order Number])
AND ([000DeleteMeNames].[Date-Forecast] = [Work Orders].[Work Order Date])
SET [000DeleteMeNames].DummyField = [Work Orders].[Manager];
Is there a way to change my SQL so that the Sharepoint list is not updated, and only the records in the 000DeleteMeNames table that are native to Access are updated? Am I doing something wrong in the SQL above? Thanks in advance.
Try this:
UPDATE 000DeleteMeNames
SET [000DeleteMeNames].DummyField = [Work Orders].[Manager]
FROM 000DeleteMeNames INNER JOIN [Work Orders]
ON WO_Num = [Work Orders].[Work Order Number])
AND ([000DeleteMeNames].[Date-Forecast] = [Work Orders].[Work Order Date]);

Need help structuring SQL query to find collisions tied to same key value

I'm working on merging individual Access databases into a single SQL database, but there are collisions that need to be sorted out first.
For example, the table in each database that holds course/class information (it's school-related) uses the [Course ID] as a key value, but the other columns in that table don't necessarily match across all of the individual Access databases. So, a [Course ID] of "0106" might exist in multiple databases, and I need to make sure that the other 19 columns associated with that [Course ID] are identical in the other access databases.
I've already compiled the data from all of the access databases into an Access database as an aggregate with all of the duplicated/colliding data.
I've already developed a query to find all of the duplicate [Course ID] values, but I need another query to find any other inconsistent data between records.
How can I structure a SQL query to find records with duplicate [Course ID] values, but only the ones where one of the other columns has an inconsistency?
I have two tables that I'm working with; CHI(fields Expr1000,School) and CRS (fields Expr1000,Course ID, Course title).
The query I'm using atm is:
SELECT CRS.[Course ID], CRS.[Course title], CHI.Name
FROM CHI RIGHT JOIN CRS ON CHI.Expr1000 = CRS.Expr1000
WHERE (
((CRS.[Course ID]) In
(SELECT [Course ID] FROM [CRS] As Tmp GROUP BY [Course ID] HAVING Count(*)>1 ))
AND ((CRS.Expr1000)=[CHI].[School]))
ORDER BY CRS.[Course ID];
This generates a list of all records with duplicate [Course ID]s, though I'd prefer a list that will exclude occurrences where all the records with a given [Course ID] have matching [Course title].
E.g., If all records with Course ID=106 also have matching [Course Title], then they should be excluded.
I'm not sure if I should try to consolidate everything into a single query or create multiple queries to whittle the data down, and I'm not sure how to structure the queries for the data I desire.
You could do something like this:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.key = table2.key
and (
table1.property1 <> table2.property1
or table1.SomeOtherProperty <> table2.SomeOtherProperty
-- etc
)
this would return records from the tables that have a matching key, but at least one other column in the table is not matching - note using column stubs here, plug in your own column names.

Verify Data in Access with two tables using SQL

I have two tables in Access, one is essentially a customer record with their personal info and another is full of valid values, for example state abbreviation and gender. I am trying tocompare the two tables so that it checks the gender and the state codes against whats in the reference table and pull the customer ID (another field in the first table) and report that theres an issue by putting the customer id and the values im using to validate, in another table.
B
ut instead of only returning values that dont match it returns every record from the first table. so its not really verifying anything so much as just copying and pasting, instead of only returning values with no match in the second table.
My code looks like this
DoCmd.RunSQL
"INSERT INTO Issues
SELECT Eligibility.ID, Eligibility.[Member Id], Eligibility.[Sex Code], Eligibility.State
FROM Eligibility
LEFT JOIN Ref ON
(Eligibility.[Sex Code]<>Ref.[Sex Code]) AND (Eligibility.State<>Ref.State);"
What you will want to do, is actually change the JOIN clause to match the Sex Code and State, but only return the rows that do NOT match.
INSERT INTO Issues
SELECT Eligibility.ID, Eligibility.[Member Id], Eligibility.[Sex Code], Eligibility.State
FROM Eligibility
LEFT JOIN Ref ON
(Eligibility.[Sex Code]=Ref.[Sex Code]) AND (Eligibility.State=Ref.State)
WHERE Ref.[Sex Code] IS NULL
The WHERE Ref.[Sex Code] IS NULL will filter out the valid rows, and only return the rows that do not meet your JOIN criteria.

JOIN on Access "Lookup" field returns no results

I have a strange problem that I believe is related to the way my lookups are structured.
TABLE Category
ID
CategoryName
TABLE Product
ID
ProductName
Category - Number (SELECT [Category].[ID], [Category].[Category Type] FROM Category ORDER BY [Category Type];)
TABLE SalePrices
Several fields related to sale date, price, &c.
ProductName - Text (SELECT [Product].[ID], [Product].[Product Name] FROM Product ORDER BY [Product Name];
For some reason I get a blank result set when I run the following query:
SELECT SalePrices.[Product Name], Product.[Product Name]
FROM SalePrices INNER JOIN Product ON SalePrices.[Product Name] = Product.[Product Name];
I have a query that displays the MIN of SalePrices.UnitPrice which I want to display with the ProductName and CategoryName, but I'm not getting results for that so I wanted to simplify things first.
When I join Product and Category I have to match Product.[Category Type] = Category.ID;, but when I try to do SalePrices.[Product Name] = Product.[ID]; I get a TYPE MISMATCH error. I'm not sure where I went wrong.
The eventual goal is to combine the SalesPrices <-> ProductName join with this one:
SELECT Product.[Product Name], Category.[Category Type]
FROM Product INNER JOIN Category ON Product.[Category Type] = Category.ID;
As suggested in the comments by #Scotch and #HansUp, defining "Lookup" fields in an Access table is generally regarded as a Bad Idea for all but the most trivial of cases. They hide what is really going in the database and they can lead to all kinds of confusion (as you have discovered) when you actually try to use them. (Ref: here, via comment by #HansUp.)
Your best course of action would be to replace the Lookup fields in your tables with regular numeric fields containing the ID (key) values in the related tables. Then, if necessary, you can create and save Select Queries to explicitly do the lookups and display the results as they previously appeared in the [Product] and [SalePrices] Datasheet Views.

Access 2007 Update Query has more Rows than Database

I have an Update query that says it's updating 1029 rows, when there are only 994 rows in the database that it's updating.
The query references an external Excel file and updates the "Master Calendar" with "Posted Date" when the batch numbers are equivalent:
UPDATE [Mapped Link] RIGHT JOIN Master_Calendar ON [Mapped Link].Reference = Master_Calendar.[Batch Number] SET Master_Calendar.[Actual Posted Date] = [Mapped Link]. [Entry Date];
Whenever rows get added to the external 'Mapped Link' document, the rows increase on the update query. There are about 2000 rows in 'Mapped Link', so it's not adding the rows from Master Calendar to Mapped Link together.
The data doesn't seem to be updating incorrectly -- all the data is correct. But how can it be updating more rows than it exists? Is it just counting wrong? Thoughts?
Thanks, guys.
You have either duplicates in either Master_Calendar.[Actual Posted Date] or [Mapped Link].[Entry Date]
To check for duplicate dates, you can run a query. For example:
SELECT [Actual Posted Date], Count(ID) AS CountOfID
FROM Master_Calendar
GROUP BY [Actual Posted Date]
HAVING Count(ID)>1
You will need to change ID to the name of the index on your table.