Sum of multiple rows using different cases - mysql

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

Related

Subquery returns more than 1 row in like %..%

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)

How to combine multiple colums in a MYSQL Query that uses COUNT and WHERE

So I have figured out how to generate a report that will count items for each month based upon a WHERE criteria like:
SELECT
Year(FROM_UNIXTIME(`tTime1`)) as YEAR,
Month(FROM_UNIXTIME(`tTime1`)) as MONTH,
Count(*) As BUY_RESULT
FROM zupsbackup
WHERE Result='BUY' AND tPeriod='60'
GROUP BY Year(FROM_UNIXTIME(`tTime1`)), Month(FROM_UNIXTIME(`tTime1`))
This produces table result like
YEAR|MONTH|BUY_RESULT
However my Result column has 2 possible values BUY and SELL.
So I want to add another column to the result so that it looks like
YEAR|MONTH|BUY_RESULT|SELL_RESULT
How can I create another column in MYSQL ???
I have tried UNION but this adds the SELL results to the end of the results table and does not create the extra column.
So how do I create an extra column using a WHERE clause so that RESULT='SELL' to be added next to the existing column where RESULT='BUY' ?????
Thanks !!!!!
Use conditional aggregation:
SELECT Year(FROM_UNIXTIME(`tTime1`)) as YEAR, Month(FROM_UNIXTIME(`tTime1`)) as MONTH,
SUM(Result = 'BUY') As BUY_RESULT, SUM(Result = 'SELL') as SELL_RESULT
FROM zupsbackup
WHERE tPeriod = '60'
GROUP BY Year(FROM_UNIXTIME(`tTime1`)), Month(FROM_UNIXTIME(`tTime1`));
An easy way to accomplish this is to use a sub-query:
SELECT
Year(FROM_UNIXTIME(tTime1)) as YEAR,
Month(FROM_UNIXTIME(tTime1)) as MONTH,
Count(*) As BUY_RESULT,
(
SELECT
COUNT(sub.*)
FROM
zupsbackup AS sub
WHERE
sub.Result = 'SELL' and
sub.tPeriod = '60' AND
Year(FROM_UNIXTIME(sub.tTime1)) = YEAR AND
Month(FROM_UNIXTIME(tTime1)) = MONTH
) AS SELL_RESULT
FROM
zupsbackup
WHERE
Result='BUY' AND
tPeriod='60'
GROUP BY
Year(FROM_UNIXTIME(tTime1)),
Month(FROM_UNIXTIME(tTime1))

complex math routines in mysql

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

MySQL: Get the average of a column

I have a table name invoices. There is a column named user and late_fee. I am trying to find out the percentage of late invoices compared to how many invoices total.
He has 16 invoices, which 2 of those invoices are late. I feel like this should be an easy pie query but I can't figure it out for the life of me?
You could use something like this. It gets the count of the late_fee depending on it's value.
select sum( case
when late_fee = 1
then 1
else 0
end
)
/ count(*)
from invoices
group
by user
As #Ravinder pointed out, in MySQL this is also valid (does not work on other platforms though):
select sum( late_fee = 1
)
/ count(*)
from invoices
group
by user

How to select based on different column data

I want to perform a different SELECT based on the column data. For example I have a table http://sqlfiddle.com/#!2/093a2 where I want compare start_date and end_date only if use_schedule = 1. Otherwise select all data. (A different select) Basically I only want to compare the start and end date if only use_schedule is 1 and if use_schedule is 0 then select rest of the data.
An example may be something like
select id, name from table
where use_schedule = 0
else
select id, name, start_date from table
where use_schedule = 0 and current_date >= start_date.
Basically I have the data where schedule is enabled only then look into start and end date. Because if schedule is not enabled there is no point of looking into the dates. Just select the data. With schedule enabled, I want to be more selective in selecting the scheduled data.
I am trying to figure out if MySQL CASE or IF statements would work but not able to do so. How can I run this select?
Thanks.
You can use UNION to mix and match the results of 2 different SQL queries into one result set:
select id, name, null from table
where use_schedule = 0
union
select id, name, start_date from table
where use_schedule = 1 and current_date >= start_date
Note that both queries have to have compatible output fields (same number and type for this to work). The use of UNION automatically merges only distinct records - if you want to keep double results use UNION ALL instead.
In this specific case a more extensive WHERE-clause would also work obviously:
where use_schedule = 0 or (use_schedule = 1 and current_date >= start_date)
But given the question I'm assuming your real case is a bit more complex.
Documentation over at MySQL site.
Use CASE, in this case..:
SELECT id, name,
(CASE
WHEN start_date >= DATE(NOW()) AND use_schedule = 1
THEN start_date
ELSE NULL
END) AS cols FROM campaigns
This way it selects only the schedule 0 OR the 1 with a date bigger or equals to now;
I used DATE(NOW()) so that it removes the time which you are not interested in.