SQL statement with formula - mysql

I am trying to write an SQL statement that will calculate the total value of a purchase order.
Example time!
SELECT PO_Number, LineItem, Quantity, Cost
FROM POs
Say on PO_Number=484 there are 2 units of LineItem-1 at $2 each. There are also 3 units of LineItem-2 at $5 each. Is there a way to output $19? (2 + 2 + 3 + 3 + 3 = 19)

SELECT SUM(Quantity*Cost) WHERE PO_Number = 484;
UPDATE
If you want to show the totals for multiple purchase orders, you need to "group" your results by purchase order:
SELECT SUM(Quantity*Cost)
WHERE PO_Number IN (484,485,486) -- if you want specified ones only, omit for all
GROUP BY PO_Number;

Or...
SELECT PO_Number, SUM(Quantity*Cost)
GROUP BY PO_Number;

Related

Mysql condition on WHERE clause

I m using mysql database using php to fetch data. I have distributor column, i m using WHERE clause like WHERE d_id = 1 . i have 1 to 4 distributors. On front end i have HTML select option in which if i select any distributor it shows it's data but i added an option value="0" as Total . Now if i select Total it should show all data, actually where clause should not work then. (show all distributors data)
Dist: Product Sales
1. dis_a abc 100
2. dis_b abc 50
3. dis_c cde 10
4. dis_c cde 10
Here is an example of a select that should do roughly what you want.
select d.[disc:], d.[Product], d.[Sales]
from your_table d
where d.your_table_seq = case when #dist_id = 0 then d.your_table_seq else #dist_id end
The case block will allow you pull every row when the #dist_id parameter is equal to 0, but will only pull id's that are equal to #dist_id when #dist_id is not equal to 0.

Find number of items left based upon credit's id and all ids prior

I have a t variable which contains 100 and one table .
that table name credit and it contains the following data
Id
1
2
3
I would like the result set to look like this:
result
99 (100 - 1)
97 (100 - 2 - 1)
94 (100 - 3 - 2 - 1)
So far, I have been able to use the following code successfully:
set #t=100;
select #t:=#t-id as result from credit;
Is there a way to do this without using a variable?
This is quite simple and you shouldn't have to use the variable at all:
SELECT 100-(SELECT SUM(c2.id) FROM credit c2 WHERE c2.id <= c.id)
FROM credit c;
Here is a SQL Fiddle for you:
http://sqlfiddle.com/#!9/1fc3c6/6
The subquery simply gets the sum of all numbers including, and prior to the credit id.

MySQL : Max value of combined columns

I need to return the highest value of two combined columns.
SELECT id, max(points1 + points2) as points from schema.table;
I want it to combine the two columns BEFORE looking for the highest value. What it seems to be doing is finding the highest value for points1, then the highest value for points2, and then combining them.
I hope that makes sense!
UPDATE WITH SAMPLE:
ID Points1 Points2
1 100 200
2 80 30
3 40 400
What max(points1 + points2) seems to be returning is a value of 500. What i'm hoping to see is a value of 440 -- which is the highest COMBINED value. Hopefully that makes more sense...
If I am understanding your post correctly, you are looking for:
SELECT id FROM schema.table
WHERE points1 + points2 = (SELECT MAX(points1 + points2) FROM schema.table)
You are looking for a pretty straightforward query:
SELECT id, point
FROM schema.table
INNER JOIN (SELECT MAX(points1 + points2) as point
FROM schema.table) q ON q.point=(points1+points2);
Another solution:
SELECT id, points1+points2 AS point FROM table ORDER BY point DESC LIMIT 1
without the LIMIT 1, you can get the full standings

How to update the score column in one table with the average from columns in other three tables?

I have 4 tables that have an score column (10 points max as score)
This structure is similar to the one I have in my real database:
- master_entity(id, name, **score**, other-columns...)
- scores_table1(id, score_giver1_id, master_entity_id, **score**)
- scores_table2(id, score_giver2_id, master_entity_id, **score**)
- scores_table3(id, score_giver3_id, master_entity_id, **score**)
I would like to execute a simple SQL script (for MySQL database) daily with a cron job. This script must update the master_entity.score column with the average of the averages of the score column in other three tables.
But the average from score column in scores_table1 represent just 1/6 of the average score column value in master_entity.
Also, the average from score column in scores_table2 must represent just 2/6 of the average score column value in master_entity and finally the average from score column in scores_table3 must represent just 3/6 of the average score column value in master_entity
What would be the recommended SQL to update the master_entity.score column using the proportional averages of scores in the other 3 tables?
I have not tried, but based on your question, your sql query should looks like this:
update master_entity m
join(
select master_entity_id,avg(score) as score
from score_table1
group by master_entity_id
) as s1 on s1.master_entity_id = m.id
join(
select master_entity_id,avg(score) as score
from score_table2
group by master_entity_id
) as s2 on s2.master_entity_id = m.id
join(
select master_entity_id,avg(score) as score
from score_table3
group by master_entity_id
) as s3 on s3.master_entity_id = m.id
set m.score = (1/6)*s1.score + (2/6)*s2.score + (3/6)*s3.score
which master_entity.score = (1 /6)*avg(tbl1.score) + (2 /6)*avg(tbl2.score) + (3 /6)*avg(tbl3.score)

MySQL fetch results, sum it up

i'm trying to sum up values from one record (A) then add it to the sum values from another record (B), am wondering if this is possible?
ID ValueA ValueB ValueC
1 5 5 5
2 1 2 3
3 6 3 3
So what i'm trying to do here is to take ValueA, ValueB and ValueC of each record, adds it up so i can make an average.
So for
ID 1, i'll have 15 divide by 3 = 5
ID 2 i'll have 6 divide by 3 = 2
ID 3 i'll have 12 divide by 3 = 4
then i will have to add all 3 of these up
i'll get 11
and divide it by 3 and get an average of 3.67.
My Query
$result = mysql_query('SELECT * FROM teams WHERE UPPER(team)=UPPER("'.$team.'")');
while($row = mysql_fetch_array($result))
{
$ValueA = $row['ValueA'];
$ValueB = $row['ValueB'];
$ValueC = $row['ValueC'];
$All = $ValueA + $ValueB + $ValueC;
}
I know how to get the sum of 1 record, but not sure how can i do it with all 3 records. any help?
Edit : Sorry i forgot to add that i'll have to do average on each record first.
SELECT AVG(valueA + valueB + valueC)
FROM teams
should do the trick. Since you're selecting all records from the table, there's on grouping required.
Note that by default MySQL uses case-insensitive collations on tables, so your UPPER(team) business might not be required - removing the function calls would allow indexes (if any) to be applied to that particular match.
You can sum SQL functions together:
SELECT
(sum(ValueA) + sum(ValueB) + sum(ValueC)) / 3 as average
FROM
<your table>
WHERE
<your conditions>
SELECT SUM(ValueA + ValueB + ValueC) / (SELECT COUNT(*) FROM Teams)
FROM Teams
That will you the average you are looking for.