Access: count how many time an item is used - ms-access

I have two tables:
The table Consent Archiving is a list of unique items.
The table Table1 is the association of n rows to 1 item of Consent Archiving.
I would like that in the column Count of Consent Archiving there would be the number of time that that certain item is repeated into Table1.

Isn't a query, build to look like mentioned table, a better option? If so build it on Table1 and just add group by clause for "Consent Archiving" with a count(*) function.

Related

Single record of greatest n per group from a table with three identifiers

I have a table in which each record is a single purchase made by a single client.
Purchases can be made in different categories of product and different geographical areas (each of these is a single field).
I can count how many purchases each client has in each combination of product/area like this
Select client_id, product_id, zone_id, COUNT(purchase_id)
Group by client_id, product_id, zone_id
From this I would like to get a table where each record is the client, product and zone with the highest number of purchases. So only one row per client.
How would I go about doing this?
I think I might be able to do it by using NOT EXISTS where there is no record with the same three identifiers and higher COUNT, but as this is part of a much larger query I'm afraid of performance issues.
I also figure I might be able to concatenate the three identifiers into a single one, but I need those identifiers to be in separate fields as I need them for a join in another query this will be part of.
Step 1: Generate a temp table with the 3 columns plus count as you mentioned.
Step 2: Apply a groupwise-max algorithm. Either follow the link you have or check out Groupwise-Max

Keeping unique records in Access query, based on two columns

I have a list of events that organizations attended. The list has duplicates because one organization might have had multiple sign-ups.
So I need to keep only one record, based on Profile ID (First column) and Event name.
So if profile ID=1 has 3 records where they attended Event=y, I need to keep only one of these and delete the other two rows. See the image:
query screenshot
SELECT DISTINCT eliminates duplicates. Make sure to have only the fields defining a duplicate in the query.
eg if there were for example a date field for the event, you should leave it out in the query.

How would I display two separate tables within the same query/report without combining each entry?

As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.

Removing duplicate data from many rows in mysql?

I am a web developer so my knowledge of manipulating mass data is lacking.
A coworker is looking for a solution to our data problems. We have a table of about 400k rows with company names listed.
Whoever designed this didnt realize there needed to be some kind of unique identifier for a company, so there are duplicate entries for company names.
What method would one use in order to match all these records up based on company name, and delete the duplicates based on some kind of criteria (another column)
I was thinking of writing a script to do this in php, but I really have a hard time believing that my script would be able to execute while making comparisons between so many rows. Any advice?
Answer:
Answer origin
1) delete from table1
2) USING table1, table1 as vtable
3) WHERE (NOT table1.ID>vtable.ID)
4) AND (table1.field_name=vtable.field_name)
Here you tell mysql that there is a table1.
Then you tell it that you will use table1 and a virtual table with the values of table1.
This will let mysql not compare a record with itself!
Here you tell it that there shouldn’t be records with the same field_name.
The way I've done this in the past is to write a query that returns only the set I want (usually using DISTINCT + a subquery to determine the right record based on other values), and insert that into a different table. You can then delete the old table and rename the new one to the old name.
To find list of companies with duplicates in your table you can use script like that:
SELECT NAME
FROM companies
GROUP BY NAME
HAVING COUNT(*) > 1
And following will delete all duplicates except containing max values in col column
DELETE del
FROM companies AS del
INNER JOIN (
SELECT NAME, MAX(col) AS col
FROM companies
GROUP BY NAME
HAVING COUNT(*) > 1
) AS sub
ON del.NAME = sub.NAME AND del.col <> sub.col

adding mysql table values

I have a table in my database that holds numerical values collected from a user's input. How could I add those values together and display that number on the website, with the number updating every time a new number is inputed.
SELECT SUM(value) FROM table
Something like this? You should also look into GROUP BY.
EDIT:
It could be you're meaning that you have a value and you want to increment it by n. Then you can look at this example code.
UPDATE table SET value = value + n WHERE id = 123
Where n is the value you want to increment it by.
I would query a master table of IDs that has the running total of values.
Then, via any inserts into some alternate table that keeps each individual entry accounted for, there is a trigger that forces a SQL-Update to the master table... This way, you don't have to keep doing a web-based query that is always doing a GROUP BY for the results.
If this is a little confusing, think of an inventory system. You have one master item table of all possible inventory items. It has an "on hand" count. Then, as sales of an item are sold, the "on hand" count is reduced by however many are purchased. You are not going to each individual sales order and counting grouped by a given ID, you just go to thee master inventory item table and have that "on hand" count.