Showing Sum of amount based on another row value - php

I have SQL tables:
id_details
year-month
type (only have 2 values , income and outcome)
amount
and table have row like this:
(year-month) (type) (amount)
November-2012 income 50000
November-2012 income 30000
December-2012 income 20000
November-2012 outcome 10000
December-2012 outcome 50000
December-2012 income 10000
What I want is, showing a query with result like this:
(year-month) (sum income) (sum-outcome) (sub-total balance)
November-2012 80000 10000 70000
December-2012 30000 50000 -20000
finally
total-balance = 50000
How can I do this?
or can you recommend me better tables design?

SELECT `year-month`,
SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END) `sum-income`,
SUM(CASE WHEN type = 'outcome' THEN amount ELSE 0 END) `sum-outcome`,
SUM(amount) `sum-total balance`
FROM <JOINS>
GROUP BY `year-month`

Related

Sum of columns with item and item type grouping

I have table with
seller_id, transaction_type, sub_total, total_commission_fee, refund_fee, cancellation_fee
3 transaction_type
payment, cancel, refund
i want to get the sum of sub_total, total_commission_fee, refund_fee, cancellation_fee for each seller_id
sample
seller_id transaction_type sub_total total_commission_fee refund_fee cancellation_fee
3 order 40 0 0 0
4 order 10 0 0 0
3 cancel 0 0 0 3
3 refund 28 0 2 0
i want result like this
seller_id payment_total(sum of all sub_total transaction_type order) cancel_total(sum of all cancellation_fee transaction_type cancel) refund_total (sum of all refund_fee transaction_type refund)
i can get total without transaction type.
Transaction::groupBy('seller_id')
->selectRaw('sum(sub_total) as total, seller_id')
Is there a way to get result as i want.
Else i have to do a get request and loop through each one.
This may cause problem when the table becomes big
And what is this kind of operations called?
You can use IF(condition, value_if_true, value_if_false) to sum the values:
Transaction::groupBy('seller_id')
->selectRaw('SUM(IF(transaction_type = "order", sub_total, 0)) AS payment_total,
SUM(IF(transaction_type = "cancel", cancellation_fee, 0)) AS cancel_total,
SUM(IF(transaction_type = "refund", refund_fee, 0)) AS refund_total,
seller_id')
You can use directly with group by seller id(seller_id). please try to use below query
Transaction::groupBy('seller_id')
->selectRaw('SUM(sub_total) AS payment_total,
SUM(cancellation_fee) AS cancel_total,
SUM(refund_fee) AS refund_total,
seller_id')

how to insert a value to sql database by summing last row value and the inserting value

my table like this
id debt balance
1 1000
2 500
3 600
i want table like this when i insert new debt value
id debt balance
1 1000 1000
2 500 1500
3 600 2100
eg when i add debt value 1000 need to update balance as sum of the value
id debt balance
1 1000 1000
2 500 1500
3 600 2100
4 1000 3100
SELECT balance from table order by desc limit 1
after fetching last balance add it to the current dept value
$balance = $row['balance'];
$dept = $_POST['dept'];
$new_val = $dept+$balance;
add this new_val to the new balance field
INSERT INTO table (debt, balance) VALUES ('$dept', '$new_val');
You can try below
update tablename
set balance=debt+(select balance from tablename order by id desc limit 1)

How to stop the loop and add the value?

I have the following table:
id tax rate quantity
1 20 400 5
2 20 566 2
3 5 200 4
Here is my expected output:
taxableamount taxamount total
3132.00 626.40 3758.40
800.00 40.00 840.00
Let me explain my attempts here:
If each row have the same tax value, I calculated the amount (rate × quantity) from each row of data and then calculate the tax amount (amount × tax ÷ 100) and displayed the result in a single row even if they come from multiple rows. But if the rows do not have same tax value, it will be displayed in separate rows.
$query = "SELECT SUM(
CASE WHEN tax=tax
THEN rate*quantity ELSE 0 END) AS taxableamount,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS tax,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)*tax/100 ELSE 0 END) AS taxamount,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS total
FROM pricing group by tax";
while($row = show($query)):
echo '<td>'.$row->taxableamount.'</td>';
echo '<td>'.$row->taxamount.'</td>';
echo '<td>'.$row->total.'</td>';
endwhile;
The above code gives me wrong and unexpected result. So looking at my expected output, how do I go about it. Please help.
You should remove your CASE statements. As i said in my comment, tax it´s allways equal to tax, because you are comparing with the same row. I have added the tax column, so you know the amounts for each kind of tax:
SELECT
SUM(rate * quantity) AS taxableamount,
SUM(rate * quantity * tax / 100) AS taxamount,
SUM(rate * quantity * (1 + tax) / 100) AS total,
tax
FROM
pricing
GROUP BY
tax
what you are looking for is a group by tax in your sql statement. remove the remove case when. and two lines giving col called "tax"? that lokks like an error to me.
group by
aggregation

How to get SUM of a column with number of affected rows

How to get SUM of a column with number of affected rows
I am trying to get the sum of a column and I will also like to return the number of affected columns.
Example:
orders table
id customer_id name amount
----------------------------------------
1 2 burger 5.00
2 2 pizza 6.00
3 2 grape 1.00
4 1 sandwich 4.00
Now I want to SUM the amount column for a particular customer(customer_id) and also return number of items(count rows affected)
I am doing this but it only gets the sum of the amount, I would also like to get number number affected rows (count) from this single query:
SELECT SUM(amount) AS amount
FROM orders
WHERE customer_id = 2
Just do a COUNT as well:
SELECT SUM(amount) AS amount, COUNT(*) AS cnt
FROM orders
WHERE customer_id = 2
If amount is a nullable field and you want to only count NOT NULL rows, then do a COUNT(amount) instead of COUNT(*).
SELECT SUM(amount) AS amount, COUNT(1) AS cnt
FROM orders
WHERE customer_id = 2

query to find data based on max of one field and specific value of another

My SQL skills are minimal so I hope someone will help me. I am writing a PHP script in which I need to do some SQL queries:
I have a table called product_status, which has three 4 columns:
id
prod_id
status_date
status_code
So if we consider a product with id 1000, the table would have entries like:
id prod_id status_date status_code
1 1000 2015-09-01 08:20:35 100
2 1000 2015-09-01 09:22:40 200
3 1000 2015-09-01 09:35:51 300
4 1000 2015-09-01 09:42:55 400
Now, considering that say 300 is the status code for 'out-of-stock'. I want to write a query, where for a given date, it gives me all products that were NOT out-of-stock at the end of that day. In other words it should give me products 1000 if I query it for date '2015-09-01' since 300 is NOT the LAST entry for that product for that date in this table.
I am unable to write a query that works for this :( My query is:
select prod_id, status from product_status
where status_code != 300
group by prod_id, status
having date(max(status_date)) = '2015-09-01'
This returns me products which have statuses other than 300 as final status for the given day as well... Can anyone help correct my SQL?
Try this query
SELECT prod_id,
SUBSTRING_INDEX(GROUP_CONCAT(status_date ORDER BY status_date DESC), ',', 1) as max_date,
SUBSTRING_INDEX(GROUP_CONCAT(status_code ORDER BY status_date DESC), ',', 1) as recent_code
FROM product_status
GROUP BY prod_id
HAVING recent_code != '300' AND date(max_date) = '2015-09-01'
you want to filter by status, not group by it in this query, like this:
select prod_id, status from product_status
where status_code != 300
group by prod_id
having date(max(status_date)) = '2015-09-01'

Categories