MySQL Keep record with most current date - mysql

I created a table w_provider_remove to keep the Provider_no and max(prov_effective_Date) of the records in w_providers_load because I have duplicate providers but some have expired. I later try to join the w_providers_load table and the w_provider_Remove table and insert the records into the w_providers_main table. The problem is I get back to many records because it turns out I have more than one record for the particular provider with the same effective date. How can I limit it so it only inserts one of them? Or maybe there is another way to go about this were I do not need 3 tables to accomplish this task
Truncate w_provider_remove;
insert into w_provider_remove
select provider_no as provider_no, max(PROV_DATE_EFFECTIVE) as prov_date_effective
from w_provider_load
group by provider_no;
Truncate w_provider_main;
INSERT INTO w_provider_main
Select l.*
from w_provider_load as l
inner JOIN w_provider_remove as r on l.provider_no = r.provider_no AND l.prov_date_effective = r.prov_date_effective;

If you want to limit the rows in the table to one effective date per provider, then create a unique index:
create unique index w_provider_load_provider_effectivedate on w_provider_load(provider_no, prov_effective_date);
This will generate an error if two records for the same provider have the same effective date in the load.
You can do the same thing for the main table:
create unique index w_provider_main_provider_effectivedate on w_provider_main(provider_no, prov_effective_date);

Related

Insert multiple records into a table based on AVG values from another table in mySQL

I have a mysql database with 2 tables. The first "spec" is a specification table, the second 'log' is table containing logged entries of previous measurements. Each part being logged is identified by a part number and test measurement. There may be many log entries for any given part number, but only 1 entry per part number in the 'spec' database giving the actual specification. What I need to do is obtain an average of the test measurement for every different part in the 'log' table, and insert this into the 'spec' table as a new specification. The log table will have already have been corrected to remove outliers.
I have been able to update existing records in the 'spec' table, but have been unable to insert records that do not already exist.
This works
update no_flow.spec s join
(select part, round(avg(cc),0) as avgcc
from no_flow.log l
group by part) l
on s.part = l.part and l.avgcc > 0
set s.cc = l.avgcc;
This does not work
INSERT INTO no_flow.spec set (part, cc) s join
SELECT part, avg(cc)
FROM no_flow.log l
WHERE id != 0
values (l.part, l.avgcc);
Suggestions?
If there is a unique index on part in spec you could use INSERT ... ON DUPLICATE KEY UPDATE Syntax
It would look something like this:
INSERT INTO noflow.spec (part, cc)
select
part as logPart,
round(avg(cc),0) as avgcc
from
no_flow.log
group
by logPart
ON DUPLICATE KEY UPDATE cc = VALUES(cc);
This inserts all the records from the inner SELECT into the spec table. When a given inserted record encounters a duplicate key error (i.e. there is already a record for the current part number) the ON DUPLICATE KEY clause updates the existing record by setting its cc column equal to the cc column on the record it was trying to insert.

Insert/Update on table with autoincrement and foreign key

I have a table as such:
id entity_id first_year last_year sessions_attended age
1 2020 1996 2008 3 34.7
2 2024 1993 2005 2 45.1
3 ... ... ...
id is auto-increment primary key, and entity_id is a foreign key that must be unique for the table.
I have a query that calculates first and last year of attendance, and I want to be able to update this table with fresh data each time it is run, only updating the first and last year columns:
This is my insert/update for "first year":
insert into my_table (entity_id, first_year)
( select contact_id, #sd:= year(start_date)
from
( select contact_id, event_id, start_date from participations
join events on participations.event_id = events.id where events.event_type_id = 7
group by contact_id order by event_id ASC) as starter)
ON DUPLICATE KEY UPDATE first_year_85 = #sd;
I have one similar that does "last year", identical except for the target column and the order by.
The queries alone return the desired values, but I am having issues with the insert/update queries. When I run them, I end up with the same values for both fields (the correct first_year value).
Does anything stand out as the cause for this?
Anecdotal Note: This seems to work on MySQL 5.5.54, but when run on my local MariaDB, it just exhibits the above behavior...
Update:
Not my table design to dictate. This is a CRM that allows custom fields to be defined by end-users, I am populating the data via external queries.
The participations table holds all event registrations for all entity_ids, but the start dates are held in a separate events table, hence the join.
The variable is there because the ON DUPLICATE UPDATE will not accept a reference to the column without it.
Age is actually slightly more involved: It is age by the start date of the next active event of a certain type.
Fields are being "hard" updated as the values in this table are being pulled by in-CRM reports and searches, they need to be present, can't be dynamically calculated.
Since you have a 'natural' PK (entity_id), why have the id?
age? Are you going to have to change that column daily, or at least monthly? Not a good design. It would be better to have the constant birth_date in the table, then compute the ages in SELECT.
"calculates first and last year of attendance" -- This implies you have a table that lists all years of attendance (yoa)? If so, MAX(yoa) and MIN(yoa) would probably a better way to compute things.
One rarely needs #variables in queries.
Munch on my comments; come back for more thoughts after you provide a new query, SHOW CREATE TABLE, EXPLAIN, and some sample data.

Unable to copy column data from one table to another in MySQL

I have one table in which each row (event) has a unique (integer) ID. I have another table, which includes the same events, and I am trying to copy the ID column from the first table to the next with no luck. I have tried creating a blank column in the second table first and I have tried it without having a blank column prepared. When I run my queries, it will spin and say that it was successfully executed, but the column in the second table is never updated.
Here is my query:
insert into games2 game_id
(
select games.game_id as game_id
from
games2 join games
on games2.DATE = games.DATE and
games2.HOME = games.HOME and
games2.AWAY = games.AWAY
)
INSERT INTO Item (Name)
SELECT Item
FROM IName
I think this source would help you out Copy from one column to another (different tables same database) mysql

MySQL: Delete records whose id is not in the result set of another query

This should be a pretty simple thing, but I'm quite new to (My)SQL.
Basically, given a customers table with attributes id, client, etc., where the client field is not necessarily unique, I want to eliminate rows where the client field is a duplicate of a previous value.
The following:
SELECT MIN(id) FROM customers GROUP BY client
returns the unique id's of the rows I want. I want everything else out.
I tried
DELETE FROM customers WHERE customer.id NOT IN
(SELECT MAX(id) FROM customers GROUP BY client)
to no avail. (ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause).
Why doesn't it work and what do I need to do to accomplish my goal?
Thank you.
You could create a temporary table that holds the values you want to remove. Then your delete query could be based on that temporary table.

MSQL creating a table which automatically updates with data from other tables

I have two tables:
Table1 - Sales
id, Timestamp, Notes, CSR, Date, ExistingCustomer, PubCode, Price, Pub, Expiry, Size, KeyCode, UpSell, CustName, CustAddress, CustCity, CustState, CustPostal, CustCountry, CustPhone, CustEmail, CustCardName, CustCardNumber, CustCardType, CustCardExpiry, CustCardCode
Table2 - Refunds
id,Timestamp,CSR,Date,OrderId,Refunded,Saved,Publication
Basically, I want to create a table (MySQL) which will have some columns that are the same between the two tables and which will update automatically with the values from these two columns.
ie. Table3
Timestamp, CSR, Date, Publication
And this table would automatically update whenever a new record is posted into either of the other two tables, so it would essentially be a merged table.
Because there's nothing to join these two tables, I don't think the JOIN function would work here. Is there anyway I can do this?
You can use an trigger which actives on insert on both tables to make it automatically update.
As for combining tables with no common tables, view this question.
You need to use a stored procedure and a trigger on insert/update of the non merged table
There's got to be some way to join it, and in fact you mention Timestamp, CSR, Date, Publication.
You could join on them in a view. You could add table three and then add triggers though that would be an awful mess.
Why do you want to denormalise in this way?
How about Table3 is a unique key to use as a surrogate, and your 4 join fields, and then you take those out of Table 1 and 2 and replace them with the key the suurrogate key in table 3.
Then it'sa simple join query and no data duplication.