Finding a records twice and removing from a database - ms-access

I need to search a database where Column A says if a record is Added, or removed.
If the search finds and account number which shows a record showing Added, and another record with the same account number as Removed then it will delete the Added Record.
How is this possible?
Thanks in advance

That could be:
Delete * From YourTable
Where Field6 = "Added" And AccountNummber In
(Select AccountNumber From YourTable As T
Where T.AccountNumber = YourTable.AccountNumber
And T.Field6 = "Removed")

Related

How to update records one by one identified by their minimum id and status

I have a table ticket and I want to update the record which has a min(Ticket_ID) and the status is equal to open reservation. It works in the first execution of the query but when I have tried again nothing happens. All I want is to update it one by one look for the next lowest Ticket_ID that the status='Open for reservation'. I have tried different queries but it did not work.
Fix the condition.
UPDATE ticket SET Status="Closed" WHERE Ticket_ID = (SELECT Min(Ticket_ID) FROM ticket WHERE Status="Open");
In your second code, you are always looking for the minimum ticket id 27 AND Status = 'OPEN' which does not exist, so nothing updates.

MYSQL: how to remove SIMILAR database rows based on username and timestamp

I have a database table that I use for logging. I would like to search the table to find all entries with a particular action on the same day by the same user and only keep one of them.
The column names I think need to be used are:
"activity_action" - where the action sought is 'Daily Website Access'
"user_email" - Looking for the same user on the same day
"activity_timestamp" - where I want to ingnore the time and just check if the action was on the same day
If you are looking for a delete statement, here is one way to do it in MySQL:
delete t
from mytable t
inner join mytable t1
on t1.user_email = t.user_email
and t1.activity_action = t.activity_action
and date(t1.activity_timestamp) = date(t.activity_timestamp)
and t1.activity_timestamp < t.activity_timestamp
where t.activity_action = 'Daily Website Access'
This deletes records that have the sought activity_action and for which another record with the same action and email, that happened during the same day, and whose timestamp is smaller. In other words, this deletes duplicates par action/user/day while retaining the earliest.

Getting rows in Microsoft Access to refer to other rows

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

MySQL - Remove dublets and preserve first instance

I have created a pretty messy table that I need to clean up. This is not an easy task.
Situation:
Messy table is called tbl_users.
It contains information about a user. It has a unique ID.
The most unique colomn is phone_number which identifies a user.
For varoius reasons I have the same user with the same phone number more than once in this table.
You can see when it is created in a colomn called created_date.
What i need:
I need to find the FIRST created instance of each user and then I
need to find
all the ID's of the second and even maybe third instance of the user,
if
such exists.
I need the ID's because there's data from other tables where I need to change the user_id to first instance.
How do I proceed with this challenge?
select t.*, (t.created_date = tf.minDate) as IsFirst
from tbl_users t
inner join (
select phone_number, min(created_date) as minDate
from tbl_users
group by phone_number
) tf on t.phone_number = tf.phone_number

MySql UPDATE only one of the duplicate records

I have a database of customer information. There are separate rows for billing address and shipping address with a flag signifying which it is; BA, SA. Many of the records are set to BA therefore I have duplicates for each customer. I need to set a duplicate record to SA.
I tried this but it updated ALL the records that were duplicate. Instead I want to update only one of the duplicate records;
UPDATE customer1 AS C1 JOIN
(
SELECT Ca.user_id, C2.CID, Ca.address_type FROM
customer1 AS Ca JOIN
customer2 AS C2 ON CC.user_id = C2.CID
GROUP BY Ca.user_id
HAVING COUNT(*) > 1
) AS C2a ON
C1.user_id = C2.CID
SET C1.address_type = 'SA'
...
LIMIT 0,1
Note that as others have said, you should never have duplicate rows in your database in the first place - it implies your schema is wrong. Also, you'll get a warning using LIMIT without an ORDER BY
As far as the data base is concerned, duplicate records are indistiguishable.
I think your best option is to add an automatically generated ID (see auto_increment) to each record. Then you can uniquely identify the record you want to update.
The other alternative is to copy the data from one of the duplicates, delete them, insert one record then insert the other with the required change.
You should normalize your table schemas to avoid dupplicate data.
Meaning, I would suggest an "address" table with all the adresses
and extend the "customer" table by a field BA_address and SA_address, pointing to these addresses.
If you now need to "duplicate" a record from BA to SA, you just put the same addressID inside the SA field, like in the BA field.