sql column - trans_value contain both positive and negative value amount.
so i'm trying to figure out how do i set a sum of positive value and negative value aside, so that i can calculate the how much sum of positive and how much is sum of negative.
so in the end, i can minus both, positive - negative.
edit,
i forgot to mention before that
there is also column name product id in same table, so
product_id | trans_value
1 200
1 250
1 -150
2 500
2 -300
3 200
3 -150
so i need to get sum of product id 1, display it out, then for 2, 3 and so on.
thanks
If you just want to see the total for each product_id
SELECT product_id, SUM(trans_value)
FROM table
GROUP BY product_id
ORDER BY product_id
If you really need the positive and negative values seperately:
SELECT SUM(IF(trans_value<0;trans_value;0)) neg, SUM(IF(trans_value>0;trans_value;0)) pos
FROM table
Will put the sum of the negative values in neg, the sum of the positive values in pos. pos + neg will be the total sum.
SELECT SUM( trans_value ) AS sum FROM table WHERE trans_value > 0;
Related
I am trying to write a query in PHP that searches the database (By date, Desc) getting the Sum of the Cost Column until the cumulative sum of the Quantity column reaches a certain number.
Something like this:
SELECT Sum(Cost), Sum(Quantity) FROM Table UNTIL cumulative_total of (Quantity) <= 1000 WHERE Product='myProduct' ORDER BY `Date` Desc;
I thought this might work, but it returns no results:
SELECT Quantity, Cost, (#csum := #csum + Quantity) as cumulative_sum FROM Orders WHERE Product='selectedProduct' HAVING #csum <= '1500' ORDER BY Date DESC;
The purpose is that I want to be able to get the Average cost (Cost/Quantity) of recent entries in the database, but the Quantity will be a user-generated variable.
UPDATE:
Here is an example of the table structure:
ID
Product
Quantity
Cost
Date
1
Fork
10
5.00
2022-04-19
2
Fork
10
5.00
2022-04-19
3
Knife
7
5.00
2022-04-20
4
Fork
20
10.00
2022-04-20
5
Fork
10
5.00
2022-04-21
If I wanted to know the average cost of the last 30 Forks, How would I proceed?
It would be better if you could include a sample data in an online database fiddle.
However please do try this
SELECT *, #running_sum:=#running_sum+Quantity AS cumulativeSum FROM ( SELECT * FROM Orders o WHERE o.Product = 'selectedProduct' )a, (SELECT #running_sum:=0)rn ORDER BY DATE DESC;
I already searched but I always find LEAST and GREATEST as hints. I want to have the next ascending number in a row that's not used. Like the following:
entries
1
2
3
5
6
7
If every of the numbers is for one row in my table I want the number 4 as a result and in the following example:
1
2
3
4
5
6
I want the number 7 as a result. Is there any possiblity to accomplish this in an SQL statement?
Best,
Robin
This query assumes that the number 1 is in your table
select min(number) + 1 from entries e1
where not exists (
select 1 from entries e2
where e2.number = e1.number + 1
)
If you want all missing numbers (where gaps are no larger than 1) instead of the smallest one, then remove min()
It think the solution is to do a self-join with the next value, and extract the first lowest result. Example:
Table: values, with column value
SELECT v1.value
FROM values v1
LEFT JOIN values v2 ON v1.value = (v2.value + 1)
WHERE v2.value IS NULL
ORDER BY v1.value ASC
LIMIT 1
I am using the following query to display a TOP 10 of today's best scores achieved by users.
$query = 'SELECT *
FROM results
WHERE date ="'.$today.'"
ORDER BY `score` DESC
LIMIT 0 , 10';
the scores are from 1 to 10, and they have the format: 9.20, 9.00 , 8.10, 8.00 etc.
the highest possible score is 10.00, but it is the only score that isn't displayed in the TOP 10 list.
I am having a hunch it is seen as 1..something instead of 10, but I am not shure how to debug it. please help
I suspect that your score column is a varchar type and not decimal type. When you use order by on a varchar column, it will sort with the char value and not the integer value. The string '10.00' is lower than '9.90'.
Make sure your database stores scores as decimal instead of varchar.
In my MySQL database I have a table (PERSONS) with over 10 million rows, the two important columns are:
ID
POINTS
I would like to know the rank of the person with ID = randomid
I want to return to the person his "rank", which depends on his points. But his rank will not be the exact row number, but more like a percentage layer. Like: "You are in the top 5%" or "You are in the layer 10% - 15%".
Of course I could query the table and convert the row number to the layer% by dividing it with the total number of rows. But my question is, would it be faster (with 10M+ rows) to just grab the several rows with LIMIT X, 1, where X will be a row on percentage 100, 95, 90, 85 .. of the table. Next step: check if the points of this row is lower than the current persons points and if yes, grab next layer % row, if not, return previous layer row.
In the persons table there are 9 columns with 2 bigints, 4 varchars 150, 1 date and 2 booleans.
Of course I would prefer to get the exact row rank, but from what I tested, this is slow and takes at least several seconds, with my wat it can be done in a few hundreds of a second.
Also, the way I suggested is not precise when there are several layers with the same points, but it doesn't need to be that precise, so we can neglect that fact.
Extra info, I program in PHP, so if there is a specific solution for this in PHP + MySQL it would be nice too.
At last, it's worth to mention that the table grows with 20k rows an hour (almost 500k a day).
I appreciate all the help.
You could try this. I first count the number of rows with more points, and then add one to that, in case there are a number of rows with the same number of points. So if there are 10 rows with the same number of points, they all have the same rank as the first one in that group.
SELECT SUM(CASE WHEN points > (SELECT POINTS FROM YOUR_TABLE WHERE ID = randomid) THEN 1 ELSE 0 END) + 1 as Rank,
(SUM(CASE WHEN points > (SELECT POINTS FROM YOUR_TABLE WHERE ID = randomid) THEN 1 ELSE 0 END) + 1) / COUNT(*) as Pct
FROM YOUR_TABLE
If that is slow, I would run two queries. First get that ID's points and then plug that into a second query to determine the rank/pct.
SELECT POINTS
FROM YOUR_TABLE
WHERE ID = randomid
Then compute the rank and pct, plugging in the points from above.
SELECT SUM(CASE WHEN points > POINTS THEN 1 ELSE 0 END) + 1 as Rank,
(SUM(CASE WHEN points > POINTS THEN 1 ELSE 0 END) + 1) / COUNT(*) as Pct
FROM YOUR_TABLE
In PHP, I've a contest question such as "How many people will participate ?". I need to select the 10 closest answers near this total participants.
I've a table called answers with an ID and number field.
Let's say the total participants are 100 and I want 10 results.
I need to select the 10 results where number is closest to 100. It should be above and below 100.
How could I do that ?
Thanks,
Select the (abs(delta))...
select id, number, abs(100 - number) as delta
from mytable
order by delta
limit 0, 10
Something like this.
You can calculate the proximity with the absolute number of the substraction;
$proximity=abs($answer - 100);
the smaller, the closer!