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
Related
I have a sales table and need to calculate the average call time when a case is met. I'm doing the query for each day of the month.
My query is
SELECT AVG(case when (outcome= 'Sale1' or outcome='Sale2') then call_length else 0 end) as avg_call_length
FROM SALES
WHERE year(call_date)='2018' and month(call_date)='7' and day(call_date)='30'
Lets say I have 100 records then avg_call_length is divided by 100 instead of how many records are Sale1 or Sale2. How do I write the correct query?
Delete the case and move the condition to the where:
SELECT AVG(call_length) as avg_call_length
FROM SALES
WHERE date(call_date) = '2018-07-30'
AND outcome IN ('Sale1', 'Sale2')
Note the simpler way of coding the conditions.
i am working with bank transaction table witch contain 67000 transactions for around 800 customer from January to October so what i want is to calculate number of transactions for each customer per month i want the final result will be like cust_id , number of transaction for jan ,number of transaction for Feb ... til October i tryed this query
SELECT
cut_id,
CASE WHEN tra_date LIKE '%Oct%'
THEN
(select count(tra_date) from tab where tra_date like '%Oct%' GROUP by cut_id)
ELSE 0
END AS oct,
CASE WHEN tra_date LIKE '%Sep%'
THEN
(select count(tra_date) from tab where tra_date like '%Sep%' GROUP by cut_id)
ELSE 0
END AS sept
FROM tab
GROUP by cut_id
but i found this error #1242 - Subquery returns more than 1 row
I'm going to assume you have a sane schema that uses actual an actual datetime type for your transaction dates, rather than the apparent insane schema in the question that uses strings. Given that, your query would look like this:
SELECT
cut_id,
SUM(CASE WHEN month(tra_date) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN month(tra_date) = 9 THEN 1 ELSE 0 END) AS Sept
FROM tab
GROUP by cut_id
In this section
(select count(tra_date) from tab where tra_date like '%Oct%' GROUP by cut_id)
When you do a count with a group by, and there are multiple values for the thing you are grouping by, you get multiple values. Based on your requirements, you need to choose which single value you want.
Maybe you need to do this instead
(select count(tra_date) from tab t1 where t1.tra_date like '%Oct%' and t1.cut_id = tab.cut_id)
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
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;
I have "Result" column in my table. Result column contains one of the below three values,
Pass, Fail, Incomplete.
I need to write a query which gives below three results
1. total count of passed records
2. total count of failed records
3. total count of incomplete records
Is it possible? If yes, please guide me to write it. Thanks in advance.
SELECT COUNT(CASE result WHEN 'Pass' THEN 1 END) AS passed,
COUNT(CASE result WHEN 'Failed' THEN 1 END) AS failed,
COUNT(CASE result WHEN 'Incomplete' THEN 1 END) AS incomplete
FROM mytable