I was wondering how I would go about summing the costs of an individual record with all related records from a subtable into a "total Cost" column, see picture
Thank you :)
Related
I have a query that returns the list of sales by customers, but I would like to know just the customer details and total value of sales. The only way I think of being able to do this is to create a second query based on the first one, remove the fields that cause unique records (e.g. description, goods) so I am only left with customer and sale value and then total/group that.
Is that the only way around or can this be done in one query.
Either this, or you can also copy your existing query, and do the grouping and summing in the copy.
But obviously you need two queries if you want both the full list and the totals query.
Edit: If you only need the totals, then edit the original query. Remove the columns you don't need, group by customer, sum the sale values.
I am using MS Access queries. I have a price list with several items that I am matching to actual invoice data to find discrepancies. Let me get specific. The price list can have duplicate item numbers but the only thing different between them is the frequency (in this example it's mat rentals). Here is a sample of the price list data:
The invoice data that comes in has the items listed by item number but not the frequency. Here is a sample of the invoice data that comes in:
I have a query currently set up to return the difference in the total paid and the total calculated from the price list. The query is just matching on item number and returning the difference [Total Price] - [Price From Price List * Quantity]. Here is how the query returns the data currently:
What I want the query to do is only return a record if there is a problem, meaning the price charged on the invoice is not matching any of the price list prices. If the invoices actually had frequency data, this would not be an issue. However, since the invoices don't, I need to look at the invoice unit price and see if it matches any of the prices on the price list. If it does, I don't have a problem. In the example above, invoice #1 doesn't need to show up because the $1.80 unit price matches the bi-weekly price from the price list and invoice #2 doesn't need to show up because the $1.20 unit price matches the weekly price from the price list. The one problem is invoice #3, because the $2.70 unit price does not match any price for that item on the price list. Now, what price from the price list should I use to calculate the difference? I can use either the minimum or the maximum or the closest one, that part doesn't really matter, I just need to come up with the method and the query to only return the records that don't match any price on the price list.
Let's go with the closest price to calculate the difference. If I use that, then the only record the query should return is:
Hopefully this makes sense. In closing, what I am trying to do is take an invoice record, scan through the price list to find if there is a match on price, and if there isn't, return a record that calculates the difference using the closest amount. If you don't think this can be done with just a design query, then let me know, perhaps I can write a small method.
Thank you in advance for answers!!
You could run a query on Report Price with a left join on Unit Price, keeping all records from the Report. Then filter out any null fields, from Unit Price.
Then your Overpayment, is just an expression, that subtracts one from the other. i.e. "Overpayment: (ReportPrice - UnitPrice) * Units"
You could probably do this with one query, but it wouldn't harm anything, if you wanted to use two.
I have a MySQL table which contains Aggregate Revenue of a company for two year from it's different clients to perform analysis on Current year Revenue and future Revenue forcast, having these fields for time,
MonthID,
QuarterID and
YearID
It is having month wise data, also have quarter wise (I can get that by aggregating the month, but have added row for faster select for quarter..i am showing that on a graph) and so on Year wise.
Now to reduce some data count and for optimization for faster select, I have removed MonthID, QtrID and YrID. And added a new column Frequency, that has values like
Last Month
1st Qtr
2nd Qtr
So on, It has reduced my row counts to half, but still i don't feel it very optimized, some expert advice will be highly appreciated that what can else be done on this. My table has around a million records.
Data Warehousing with millions of rows begs for "Summary Tables". With them you can get significant speedup; typically 10-fold.
I would build Summary Table(s) on a per-month basis (based on your specs), then roll them up to get Qtr and Year "reports".
I have a pair of blogs that goes into more detail:
http://mysql.rjweb.org/doc.php/datawarehouse and
http://mysql.rjweb.org/doc.php/summarytables
One million rows is somewhat small as DW applications go. Those blogs are aimed at that size, plus much bigger DWs (10s, even 100s, of millions of rows). (For 1 billion rows, even more techniques need to be pulled into play.)
If you have further difficulties, please provide SHOW CREATE TABLE and some tentative SELECTs. Those would help me point out details.
I have an order table that contains dates and amounts for each order, this table is big and contains more that 1000000 records and growing.
We need to create a set of queries to calculate certain milestones, is there a way in mysql to figure out on which date we reached an aggregate milestone of x amount.
For e.g we crossed 1 m sales on '2011-01-01'
Currently we scan the entire table then use the logic in PHP to figure out the date, but it would be great if this could be done in mysql without reading so many records at 1 time.
There maybe elegant approaches, but what you can do is maintain a row in another table which contains, current_sales and date it occurred. Every time you have a sale, increment the value, and store sales date. If the expected milestones(1 Million, 2 Million etc) are known in advance, you can store them away when they occur(in same or different table)
i think using gunner's logic with trigger will be a good option as it reduce your efforts to maintain the row and after that you can send mail notification through trigger to know the milestone status
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.