Merge and populate tables - sql

I have 2 tables in MS Access, that are updated externally each day (any tables I add will be deleted when the copy arrives in the morning):
Current status of a customer
All customer errors
The current status of a customer includes all customers in a particular process, and shows which status they are in today. The "all customer errors" table shows the details of customers IF they had an error at some stage. Both tables have some fields in common, but not everything (so a simple union isn't possible).
I need some help to do the following:
Join the tables and create a column stating the initial table they came from. I realise I will have some duplicates.
Taking "current status of a customer" table, populate the missing data from "all customer errors"
create an extra column - "number of errors" where I count the number of times the customer appeared in the error table
Help!
My SQL skills are a bit basic, but improving each day :-)
Thanks
Kirstin

You must have a primary key and create an inner join. Use this formula in the SQL tab. You can then go into design view and use 'make table'. Simply select what data you want to appear.
'SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;'
When you have your new table you can update it to have new columns etc.
'UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;'

Related

one-to-one tables relationship, linked by the autonumber in the main table

I have a main table that contain the customers details, I built another table that contain for example a yes/no fields about if the customer paid his taxes, the two tables is linked with autonumber from the main table.
I want always to keep them both with the same amount of records (that means every customer has a record in the second table even if the second table has empty record with data only in the primary key field)
I need that cause with missing records I cannot run update query to auto fill the second table and i got an error of validation rule violation.
I use this sql:
update clients LEFT JOIN MonthlyTbl ON clients.SerialNo = MonthlyTbl.serialno
set sReport04='ready';
I have almost 700 records in the main table and only 80 records in the second, and when I run the sql it updates only 80!!!!
Thanks for Help
Please use below query,
update clients set sReport04='ready' where SerialNo in
(select serialno from MonthlyTbl);
here is the right answer
first run the sql:
INSERT INTO monthlytbl ( serialno )
SELECT clients.serialno FROM clients
WHERE (((clients.[serialno]) Not In (select serialno from monthlytbl)));
and then:
select sreport04 from monthlytbl
set sReport04='ready';

MS Access - Log daily totals of query in new table

I have an ODBC database that I've linked to an Access table. I've been using Access to generate some custom queries/reports.
However, this ODBC database changes frequently and I'm trying to discover where the discrepancy is coming from. (hundreds of thousands of records to go through, but I can easily filter it down into what I'm concerned about)
Right now I've been manually pulling the data each day, exporting to Excel, counting the totals for each category I want to track, and logging in another Excel file.
I'd rather automate this in Access if possible, but haven't been able to get my heard around it yet.
I've already linked the ODBC databases I'm concerned with, and can generate the query I want to generate.
What I'm struggling with is how to capture this daily and then log that total so I can trend it over a given time period.
If it the data was constant, this would be easy for me to understand/do. However, the data can change daily.
EX: This is a database of work orders. Work orders(which are basically my primary key) are assigned to different departments. A single work order can belong to many different departments and have multiple tasks/holds/actions tied to it.
Work Order 0237153-03 could be assigned to Department A today, but then could be reassigned to Department B tomorrow.
These work orders also have "ranking codes" such as Priority A, B, C. These too can be changed at any given time. Today Work Order 0237153-03 could be priority A, but tomorrow someone may decide that it should actually be Priority B.
This is why I want to capture all available data each day (The new work orders that have come in overnight, and all the old work orders that may have had changes made to them), count the totals of the different fields I'm concerned about, then log this data.
Then repeat this everyday.
the question you ask is very vague so here is a general answer.
You are counting the items you get from a database table.
It may be that you don't need to actually count them every day, but if the table in the database stores all the data for every day, you simply need to create a query to count the items that are in the table for every day that is stored in the table.
You are right that this would be best done in access.
You might not have the "log the counts in another table" though.
It seems you are quite new to access so you might benefit form these links videos numbered 61, 70 here and also video 7 here
These will help or buy a book / use web resources.
PART2.
If you have to bodge it because you can't get the ODBC database to use triggers/data macros to log a history you could store a history yourself like this.... BUT you have to do it EVERY day.
0 On day 1 take a full copy of the ODBC data as YOURTABLE. Add a field "dump Number" and set it all to 1.
1. Link to the ODBC data every day.
join from YOURTABLE to the ODBC table and find any records that have changed (ie test just the fields you want to monitor and if any of them have changed...).
Append these changed records to YOURTABLE with a new value for "dump number of 2" This MUST always increment!
You can now write SQL to get the most recent record for each primary key.
SELECT *
FROM Mytable
WHERE
(
SELECT PrimaryKeyFields, MAX(DumpNumber) AS MAXDumpNumber
FROM Mytable
GROUP BY PrimaryKeyFields
) AS T1
ON t1.PrimaryKeyFields = Mytable.PrimaryKeyFields
AND t1.MAXDumpNumber= Mytable.DumpNumber
You can compare the most recent records with any previous records.
ie to get the previous dump
Note that this will NOT work in the abvoe SQL (unless you always keep every record!)
AND t1.MAXDumpNumber-1 = Mytable.DumpNumber
Use something like this to get the previous row:
SELECT *
FROM Mytable
INNER JOIN
(
SELECT PrimaryKeyFields
, MAX(DumpNumber) AS MAXDumpNumber
FROM Mytable
INNER JOIN
(
SELECT PrimaryKeyFields
, MAX(DumpNumber) AS MAXDumpNumber
FROM Mytable
GROUP BY PrimaryKeyFields
) AS TabLatest
ON TabLatest.PrimaryKeyFields = Mytable.PrimaryKeyFields
AND
TabLatest.MAXDumpNumber <> Mytable.DumpNumber
-- Note that the <> is VERY important
GROUP BY PrimaryKeyFields
) AS T1
ON t1.PrimaryKeyFields = Mytable.PrimaryKeyFields
AND t1.MAXDumpNumber= Mytable.DumpNumber
Create 4 and 5 and MS Access named queries (or SS views) and then treate them like tables to do comparison.
Make sure you have indexes created on the PK fields and the DumpNumber and they shoudl be unique - this will speed things up....
Finish it in time for christmas... and flag this as an answer!

How to get the name of a table in MS access?

I have one table that has a list of Facilities, then I have many other tables, one for each Facility with the equipment that is there.
I am trying to make a query that can bring all of these tables together. The problem is that equipment names can be repeated from Facility to Facility so I need to get the Facility name to be associated with the equipment to have unique records. I do not have the ability to edit these equipment tables so I can't just add a column to the table and the only place that the Facility name is referenced in the Equipment Tables is in the title of the Table itself.
Is there any way that I can link the Facility Table records to the names of the Equipment Tables?
The Tables are similar to this:
Facilities
Column1
EI-456
EI-497
EI-456
Column1
Pump1
Pump2
FT1
EI-497
Column1
TT1
Pump1
Riser1
Hopefully that makes it a little more clear.
That looks like terrible design. You should have:
a Facilities table
an Equipments table (optional, depending on your needs)
a Locations table (or Assignment or...) where you just store the FacilityId and the EquipmentId.
I suggest that you read a bit about database normalisation. That will really pay off in the long run.
What you're asking for can be done dynamically through code (vba, etc) using your row record value to populate column.name in a query string.
I'd run an initial query to determine the facility name and then do:
Dim sillyString as String
sillyString = .row(?).item("column") = "EI-456"
strSQL = "SELECT * FROM " & sillyString & "WHERE ..."

SQL - Selecting a field from another table using a primary key in a trigger

I have two tables in my database, one is Transactions and the other is TransactionHistories. The latter is essentially an auditing table, whereby a trigger executes on insert, update and delete on Transactions to capture a screenshot of the data.
I am successfully retrieving all of the data stored in the Transactions table where the columns match, but the difficulty comes where I am trying to retrieve data from another table using a foreign key. For instance:
The transaction table has a field "TransactionType_TransactionTypeId", but in the audit table we wish to store its 'name' equivalent as "TransactionTypeName". This needs to be populated from the "TransactionTypes" table, which has the fields "TransactionTypeId" and "Name".
I am struggling to write a query to retrieve this as we wish. I am trying something similar to the following but having little success:
SELECT #TransactionTypeName=Name
FROM TransactionTypes
WHERE inserted.TransactionType_TransactionTypeId=TransactionTypes.TransactionTypeId;
I'm assuming that is a syntactic nightmare. If someone could point me in the right direction I would be extremely grateful!
well to get a name you should do the following
select #TransactionTypeName = TT.Name
from inserted as i
left outer join TransactionTypes as TT on TT.TransactionTypeId = i.TransactionType_TransactionTypeId
but you have to know that inserted table can have more than one row, and you are getting value for only one row.

problem creating a simple relationship in SQL Server 2005

I am using the database diagram to simply drag one column in a table to another to associate them and then trying to save it. i have done this a million times in the past with no problems. Both of the data types are the same, uniqueidentifier.
Here is the error I get:
'Customer ' table saved successfully
'CustomerOrder ' table
- Unable to create relationship 'FK_CustomerOrder_Customer'.
The
ALTER TABLE statement conflicted with
the FOREIGN KEY constraint
"FK_CustomerOrder_Customer". The
conflict occurred in database
"mydatabase", table "Customer", column
'CustomerID'.
Not sure how to trouble shoot this.
It means that there's a CustomerID in the CustomerOrder which can not be found in the Customer table.
Run this query inside SQL Server Management Studio separately:
SELECT *
FROM CustomerOrder co
WHERE NOT EXISTS (SELECT * FROM Customer c WHERE c.CustomerID = co.CustomerID)
and that should tell you what the "bad" Customer Order records are.
Are there customer orders with customer id's that don't exist in the customer table?