![]() |
Merge duplicate table entries to calculate totals?
I just started messing with our monthly sales reports and how they get collected is in CVS file by weekly sales, then a total for the month.
The monthly total CSV has the following schematic: TITLE | BOOKID | SALES QTY | SALES $ Because the data seems like it is generated every few days, the titles are duplicated in the CSV file across the month for example: Book1 | 001 | 12 | $340 Book2 | 002 | 10 | $260 Book1 | 001 | 8 | $240 Book5 | 005 | 2 | $60 How do I make it so that, if last months report has the same title mentioned 3 times, I can merge those rows AND update the total sales QTY and sales $ amounts into a single row? In essence, it ends up looking like this: Book1 | 001 | 20 | $580 Book2 | 002 | 10 | $260 Book5 | 005 | 2 | $60 Is this possible to do with an SQL query or is it simpler to do using Excel (if so how? LOL) and just reupload a 'clean' CSV to the database? :helpme |
So I managed to bastardize some code I found to merge the data but, I can't figure out where I should put the ORDER BY 'Quantity' ASC part (is this even the right query?).
Quote:
|
For reports try Google Data Studio - It's built for exactly what you are trying to do...
|
Quote:
They all have their own admin dashboard which contains information pertinent to their role, even though it pulls all the data from a central database. |
Quote:
I actually coincidently came across this today for example - https://www.youtube.com/watch?v=pBhQvLF9hsI&t=25s |
I don't know what you're trying to accomplish overall, but to group and add the figures together you could...
Process the original CSV to look like this: Book1,001,12,340 Book2,002,10,260 Book1,001,8,240 Book5,005,2,60 And from that you'd produce a CSV like this: Book1,001,20,580 Book2,002,10,260 Book5,005,2,60 From this: Code:
<?php |
Quote:
|
I try to avoid using databases, or even PHP, as much as possible, but if I have to, I use SQLite. From table 'data', column names book,id,units,amount this does what you seem to want:
select book,id,sum(units),sum(amount) from data group by book order by sum(amount) ASC; MySQL is probably similar. |
All times are GMT -7. The time now is 01:46 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123