I have a database table full of transactions. The transactions contain negative numbers from people returning items. I would like to add up all the amount field while also subtracting the negative values of returns from the total. How can I do this and output it out?
Currently the best I can do is get:
SELECT SUM(amount)
FROM outputaddition
GROUP by SIGN(amount);
But this only puts positive and negative in the same column.
SELECT personId,SUM(CASE WHEN amount<0 THEN amount ELSE 0 END) as NegativeTotal,
SUM(CASE WHEN amount>=0 THEN amount ELSE 0 END) as PostiveTotal
FROM outputaddition
GROUP BY personID
If you want single column
SELECT personId,SUM(amount) as Total
FROM outputaddition
GROUP BY personID
try this,
SELECT SUM(amount) as ActualTotal
,Sum(Case When amount > 0 then amount else 0 end) totalOfPositiveOnly
FROM outputaddition
I'm afraid your answer is ambiguous; we're not sure exactly what you're asking.
Most simply, if "subtracting the negative values" means you just want to ignore the negative returns:
select sum(amount)
where amount > 0;
Related
Where is the mistake in my query
SELECT #Total:=SUM(deposit-cost) as Total FROM `vendor_ledger` Where NOT #Total < 0 GROUP BY
VDR_ID;
Anyone Please Help Me
If you want to ignore any zero or negative deposit-cost amounts in your sum() then use a WHERE condition:
SELECT SUM(deposit-cost) as Total
FROM `vendor_ledger`
WHERE deposit-cost > 0
GROUP BY VDR_ID;
If, instead, you are wanting to ignore any Total where it's less than or equal to 0 then use a HAVING condition:
SELECT SUM(deposit-cost) as Total
FROM `vendor_ledger`
GROUP BY VDR_ID
HAVING Total > 0;
Does anyone know on how can I execute a query on how can I get the total quantity by making a transaction using type (add/subtract) column.
As you can see, raw_material_id=1 first transaction is 200.00.
Second transaction subtract 0.025,
so it should be 200.00-0.025=total quantity
This is a job for SUM() and CASE/WHEN/THEN.
SELECT SUM(CASE WHEN type='add' THEN quantity
WHEN type='subtract' THEN -quantity
ELSE 0 END) total
FROM mytable
In my table for payment details of a shop.Here Payment is done by using credit,debit and by cash.This will represent in the table like a field "mode"
If mode=1 cash,mode=2 credit and mode=3 debit.
Now i take the daily fee details using this query
SELECT * FROM (`fee_data`) WHERE `paid_date` = '2015-20-11'
I want to get the sum of Paid amount in different modes
How can i do this..
You can try this:
SET #paid_date = '2015-01-19 00:00:00';
SELECT
SUM (CASE WHEN MODE = 1 THEN VALUE_PAYMENT ELSE 0 END) TOTAL_1,
SUM (CASE WHEN MODE = 2 THEN VALUE_PAYMENT ELSE 0 END) TOTAL_2,
SUM (CASE WHEN MODE = 3 THEN VALUE_PAYMENT ELSE 0 END) TOTAL_3
FROM (`fee_data`) WHERE `paid_date` = #paid_date;
Where value_payment is the column you store the amount paid.
You can use sum(if( in combination with grouping by date, something like this:
SELECT `paid_date`, sum(if(mode=1, `fee_data`, 0)) sumMode1,
sum(if(mode=2, `fee_data`, 0)) sumMode2,
sum(if(mode=2, `fee_data`, 0)) sumMode3
FROM (`fee_data`) group by `paid_date`
With that you will get per date one line, where you have 3 aggregated columns. For each mode you have one aggregated field. Is that what you are looking for?
Try this :
SELECT * FROM (`fee_data`) WHERE `paid_date` = '2015-20-11' AND ModeID=1
make changes in DB tables accordingly
SELECT *, SUM(paid_amount) as sum_amount FROM (fee_data) WHERE paid_date = '2015-20-11' GROUP BY mode
I think this should work
I have a table in Mysql that has field code with a value = 'A' that represents the type of sale of a part. The sale price is displayed as Price. There is a Quantity field that I need to multiply by the Price. The query sum(quantity*price) where code='A' gives me what I want. I also have a code = 'T' that represents the tax of the sum of the transactions. I can get that easy enough by sum(price) where code='T'. I need to subtract the value of the 'T' (tax) value from the sum(quantity*price).
I can do this by looping through two separate queryies but I would like to combine into one query.
You just want conditional aggregation:
select (sum(case when value = 'A' then quantity * price else 0 end) -
sum(case when value = 'T' then price else 0 end)
) as netprice
please help me with this issue:
I have the following query:
select SUM(price) from prices_adverts
where advert_id="15"
and room_type_id="3"
and (date >= "2013-09-20" AND date <"2013-09-23")
order by price
This searches to find and SUM price for the room for dates between "2013-09-20" AND "2013-09-23"
but for example if dates 2013-09-21 and 2013-09-22 have value 0 and 2013-09-20 has value 25.00 the query will return sum of 25.00 for the whole date range. When the specific date has value 0 this means the room is not available, but when even 1 day has value greater than 0 the query accept the room as available because it has total value greater than 0.. Please advise me how to change the query so if even 1 day within daterange to has value 0 the query to return total value 0 for the whole period.. I hope you to understand the nature of this issue, thanks
Try this:
IF(SUM(price = 0), 0, SUM(price))
And I assume you'd like to GROUP BY room_id.
Also I'd suggest changing those 0's in your database to NULLs. Because at some point you can possibly have 0 price, which is not equivalent to not available. You won't be able to distinguish them which is no good.
I guess you've missed GROUP BY Room_id. So it should be something like:
select room_id,
CASE WHEN MIN(price) = 0 THEN 0 ELSE SUM(price) END as priceSum
from prices_adverts
where advert_id="15"
and room_type_id="3"
and (date >= "2013-09-20" AND date <"2013-09-23")
GROUP BY room_id
ORDER BY priceSum