SQL: select Sub query opposite to its parent query's where condition - mysql

invoice_payments
invoice_id amount type
1 10.00 Cash
1 5.00 Cash
1 5.00 Cash
2 70.00 Store
2 30.00 Cash
I want to get rows with total amounts with cash and total amounts with type = store
I am using this query:
SELECT invoice_id, SUM( amount ) AS total_paid, (
SELECT SUM( amount ) FROM invoice_payments WHERE TYPE = 'store' ) AS total_store
FROM invoice_payments where type!='Store'
GROUP BY invoice_id
Result:
invoice_id total_paid total_store
1 20.00 70.00
2 100.00 70.00
Desired Result:
invoice_id total_paid total_store
1 20.00 0.00
2 100.00 70.00
As you can see total_store field is not values correctly. Please suggest best solution.
I really appreciate any reply.
Thanks

select invoice_id, (select sum(amount) from payments p where p.invoice_id = a.invoice_id) 'total_paid', (select sum(amount) from payments c where c.invoice_id = a.invoice_id and type = 'store') 'total_store' from payments a group by invoice_id

You are better off using a case statement here instead of a subquery:
SELECT
invoice_id,
Sum(amount) as total_paid,
Sum(CASE WHEN type='store' THEN amount ELSE 0 END) as Store_Total
FROM
invoice_payments
GROUP BY invoice_id

SELECT x.invoice_id
, SUM(x.amount) total_paid
, SUM(CASE WHEN x.type = 'store' THEN x.amount ELSE 0 END) total_store
FROM invoice_payments x
GROUP
BY x.invoice_id;

Related

Total purchase, sales and stock remained for each product using mysql

I am trying to get total purchase, sales and sotck remained for reach product using mysql query which is as follows:
select fk_product_id,
(select sum(quantity) from entry_products where status =0 ) as total_purchase,
(select sum(quantity) from entry_products where status =1)as total_sales,
(select sum(quantity) from entry_products where status =0 ) -
(select sum(quantity) from entry_products where status =1) as stock
from entry_products group by fk_product_id
Output
fk_product_id total_purchase total_sales stock
1 1700 660 1040
2 1700 660 1040
3 1700 660 1040
My Expected Output is
fk_product_id total_purchase total_sales stock
1 350 200 150
2 1100 460 640
3 250 0 250
You need conditional aggregation:
select fk_product_id,
sum(case when status = 0 then quantity else 0 end) as total_purchase,
sum(case when status = 1 then quantity else 0 end) as total_sales,
sum(case when status = 0 then quantity else 0 end) - sum(case when status = 1 then quantity else 0 end) as stock
from entry_products
group by fk_product_id
Because MySql evaluates boolean expressions as 1 for true or 0 for false, the code could be written also like this:
select fk_product_id,
sum((status = 0) * quantity) as total_purchase,
sum((status = 1) * quantity) as total_sales,
sum((status = 0) * quantity) - sum((status = 1) * quantity) as stock
from entry_products
group by fk_product_id

How to select values by groups, using select sum()?

I need to select values by groups (from 0 to 10, from 10 to 50, more than 50).
user_id amount
1 20
1 40
2 5
3 30
3 1
Why this query doesn't work correctly?
select (select sum(amount)),
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id);
output
diapason number_of_users
0-10 1
10-50 1
>50 1
Give me hint plz
Your query has a number of issues, but primarily it will not work because you need to do the sum by user prior to the sorting into ranges. Try this instead:
SELECT CASE
WHEN amount BETWEEN 0 AND 9 THEN ' 0-10'
WHEN amount BETWEEN 10 AND 50 THEN '10-50'
ELSE '>50' END AS diapason,
COUNT(*) AS number_of_users
FROM (SELECT SUM(amount) AS amount
FROM payments
GROUP BY user_id) p
GROUP BY diapason;
Output
diapason number_of_users
0-10 1
10-50 1
>50 1
Try below way
select
sum(case when amount <10 then 1 else 0 end) as "0-10",
sum(case when amount >=10 and amount <50 then 1 else 0 end) as "0-50" , sum(case when amount>50 then 1 else 0 end) as ">50"
from table
Your syntax is incorrect:
select case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end
You can try this, writting a subquery get SUM of amount by each user_id, then do CASE WHEN, you don't need select sum(amount) in CASE WHEN.
CREATE TABLE t(
user_id int,
amount int
);
insert into t values(1,20);
insert into t values(1,40);
insert into t values(2,5);
insert into t values(3,30);
insert into t values(3,1);
Query 1:
select
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end as diapason,
count(distinct user_id) number_of_users
from (
SELECT user_id,SUM(amount) total
FROM table
GROUP BY user_id
) t1
group by
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end
Results:
| diapason | number_of_users |
|----------|-----------------|
| 0-10 | 1 |
| 10-50 | 1 |
| >50 | 1 |

SQL calculations based on column value

I have a table transactions:
client_id Amount payment ref contract type
1 300 MTV 1
1 300 MTV 1
1 300 MTV 1
1 150 ML 1
2 150 ML 2
2 150 ML 2
2 150 ML 2
2 150 ML 2
If a client is on contract 1, then their total payments should be 3 x MTV(300) and 3 x ML(150). If they are on contract 2, then they pay 6 x ML(150).
I am trying to figure out how to get the total number of payments remaining for each client based on their contract type. For example client_id = 1:
client_id type Total Paid Payments remaining
1 MTV 900 0
1 ML 150 2
I can do the first three columns using this:
SELECT `client_id`,
`payment ref` AS `type`,
SUM(`Amount`) as `Total Paid`
FROM transactions
WHERE client_id = 1
GROUP BY type;
How do I add on the payments remaining column?
CASE WHEN `contract type` = 1 THEN (3 - COUNT(MTV)... ?
You were pretty close, there was an issue with the GROUP BY clause not including client_id and payment ref:
SELECT
client_id,
payment ref AS type,
SUM(Amount) as 'Total Paid',
CASE
WHEN contract type = 1 THEN (3 - COUNT(*))
WHEN contract type = 2 THEN (6 - COUNT(*))
END as 'Payments remaining'
FROM transactions
WHERE client_id = 1
GROUP BY client_id,
payment ref;
could use case when and group by
select
client_id
, payment
, case
when `contract type` = 1 and `payment ref` = 'MTV' then 3*300
when `contract type` = 1 and `payment ref` = 'ML' then 3*150
when `contract type` = 2 then 3*150
end as to_pay
, sum(Amount) payed
, case
when `contract type` = 1 and `payment ref` = 'MTV' then 3*300/SUM(Amount)
when `contract type` = 1 and `payment ref` = 'ML' then 3*150/sum(Amount)
when `contract type` = 2 then 3*150 / sum(Amount)
end number_of
end as `Payments remaining`
from my_table
group by client_id, `contract type`, `payment ref`
Try the below query
select *
,case contract_type
when 1 then 3-paidCount
when 2 then 6-paidCount
End
from (
select client_id , payment_ref , contract_type,sum(Amount)TotalPaid ,count(*) paidCount
FROM transactions
group by client_id , payment_ref , contract_type
) t

MySQL - users' monthly spend in the first 6 months of initial subscription purchase

I am trying to create report that shows subscription price (AmountPerMonth) month by month for 6 months for all users - where Month1 is the date the user has purchased the 1st subscription date (NOT the registration date) , and Month2 etc are the subsequent months from that date, varying for each account.
The format of the table for this report
I have managed to pull the first 2 months table, but can't figure out how to continue up to the 6th month. Thank you in advance!
SELECT F1.Id, F1.Month1, F2.Month2
FROM
(SELECT Id, AmountPerMonth AS Month1, ActionDate
FROM MONTLYSPEND
GROUP BY ID
HAVING MIN(ActionDate)) AS F1,
(SELECT t1.Id R, t2.AmountPerMonth AS Month2, MIN(t2.ActionDate)
FROM MONTLYSPEND t1
INNER JOIN MONTLYSPEND t2
ON t1.Id = t2.Id
AND t1.ActionDate < t2.ActionDate
GROUP BY t1.Id) AS F2
WHERE F1.id = F2.R
;
Turns out there were two ways - Stored Procedure, which takes too much memory, or using CASE WHEN, this pivots the table as well. Hope it's useful to people who have to generate reports showing various activity per user day by day or month by month on the x axis. The main difficulty I had was the fact that Month_1 (first purchased subscription) was a different date for every user. This report can be used to analyse your users behavior in the first 6 months of their subscription.
The report generated by this query looks like this:
+--------+----------+------------------+---------+---------+--------+
| UserId | Currency | FirstSubscrPurch | Month_1 | Month_2 | etc... |
+--------+----------+------------------+---------+---------+--------+
| 123 | GBP | 2010-05-27 | 34.00 | 27.00 | 0.00 |
+--------+----------+------------------+---------+---------+--------+
SELECT F6.USERID, F6.Currency, DATE_FORMAT(F6.FirstSubscrPurch, "%Y-%m-%d") AS FirstSubscrPurch, F6.MONTH_1, F6.MONTH_2,F6.MONTH_3, F6.MONTH_4, F6.MONTH_5, F6.MONTH_6, ROUND(((F6.MONTH_1+F6.MONTH_2+F6.MONTH_3+F6.MONTH_4+F6.MONTH_5+F6.MONTH_6)/6),2) AVERAGE, F6.CURRENCY
FROM (
SELECT
UserId, Currency, FirstSubscrPurch,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 0 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_1,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 1 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_2,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 2 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_3,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 3 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_4,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 4 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_5,
SUM(CASE WHEN YEAR_AND_MONTH_INDEX = 5 THEN TOTAL_AMOUNT_PAID ELSE 0 END) MONTH_6
FROM (
SELECT
hp.UserId, hp.Currency, MIN(hp.Date) AS FirstSubscrPurch,
CONCAT(YEAR(Date),'-',MONTH(Date)) AS YEAR_AND_MONTH,
TIMESTAMPDIFF( MONTH, CONCAT(YEAR(FIRST_PAYMENT_DATE),'-',MONTH(FIRST_PAYMENT_DATE),'-1'), CONCAT(YEAR(Date),'-',MONTH(Date),'-1')) AS YEAR_AND_MONTH_INDEX, -- generates string in format YYYY-M-D
MIN(Date) FIRST_PAYMENT_OF_MONTH,
MAX(Date) LAST_PAYMENT_OF_MONTH,
COUNT(*) NUM_PAYMENTS,
SUM(hp.Amount) TOTAL_AMOUNT_PAID,
SUM(hp.Credits) Credits
FROM payments hp
JOIN (
SELECT UserId, MIN(Date) FIRST_PAYMENT_DATE, ADDDATE(MIN(Date), INTERVAL 6 MONTH) SIX_MONTHS_AFTER_FIRST_PAYMENT
FROM payments hp
GROUP BY UserId
) USER_MIN_ID ON USER_MIN_ID.UserId = hp.UserId
AND hp.Date BETWEEN FIRST_PAYMENT_DATE AND CONCAT(YEAR(SIX_MONTHS_AFTER_FIRST_PAYMENT),'-',MONTH(SIX_MONTHS_AFTER_FIRST_PAYMENT),'-1')
GROUP BY UserId, Currency, YEAR_AND_MONTH
ORDER BY hp.UserId, hp.Date
) F
GROUP BY UserId, Currency
ORDER BY UserId DESC) F6;

SQL Query SELECT and Group by with conditions

I have a table called item table.
Sample contents:
order product plan qty price term base_price
CO0300039921 ZZFEE0000 N 1 0.01 1 25
CO0300039921 ZZFEE0000 N 1 0.01 1 37.13
CO0300039921 ZZFEE0000 N 1 0.02 1 37.13
CO0300039921 ZZFEE0000 Y 1 0 1 1
CO0300039921 ZZFEE0000 Y 1 0 1 1
AO1000301407 VOSVC0002W0 Y 1 0 1 3
AO1000301407 VOACT0101 N 1 0 2 5.99
If plan is ā€˜Nā€™ Then
get SUM(qty) AS 'quantity'
get price AS 'rate'
If plan is ā€˜Yā€™ Then
get SUM(qty) AS 'quantity'
get (term * qty) AS 'rate'
get (base_price) AS 'base'
FROM item
WHERE order = 'CO0300039921'
GROUP BY product, price, base_price
EXPECTED RESULT:
order product plan qty price rate base_price
CO0300039921 ZZFEE0000 N 2 0.01 0.01 -
CO0300039921 ZZFEE0000 N 1 0.02 1 -
CO0300039921 ZZFEE0000 Y 2 - 1 1
Here is what I've tried:
SELECT CASE WHEN p8_plus_plan = 'Y' THEN
(
SUM(qty_ordered),
(p8_contract_term*qty_ordered) AS 'rate',
product_base_price
)
ELSE
(
SUM(qty_ordered),
price AS 'rate'
)
END
FROM ns_order_line
WHERE order_no = ?
GROUP BY product_id, price, p8_plus_plan, product_base_price, order_no;
But I'm having an error. Please help.
You would do this using conditional aggregation. Something like this:
select sum(case when plan = 'N' then qty end) as N_qty,
sum(case when plan = 'N' then price end) as N_rate,
sum(case when plan = 'Y' then qty end) as Y_qty,
sum(case when plan = 'Y' then term*qty end) as Y_rate,
sum(case when plan = 'Y' then base_price end) as Y_base
from item
where order = 'CO0300039921'
group by product;
I'm not sure what you really want to aggregate by. But the key idea where is the case inside the aggregation functions.
Something like this I imagine.
SELECT
`order`
, `product`
, `plan`
, CASE WHEN `plan` = 'Y' THEN base_price END AS base /* edit due to comment */
, SUM(qty) AS qty
, MAX(price) AS price
, SUM(CASE WHEN `plan` = 'N' THEN price
WHEN `plan` = 'Y' THEN (term * qty) END) AS rate
FROM item
WHERE `order` = 'CO0300039921'
GROUP BY
`order`
, `product`
, `plan`
, CASE WHEN `plan` = 'Y' THEN base_price END
btw: The column name "order" is not a good choice as it is a reserved word and very frequently used too. You need to use backticks when referencing that column.
It is possible to use a CASE EXPRESSION without an aggregate function, BUT that case expression then must* also be part of he GROUP BY clause.
*In MySQL (only) you can bypass this because MySQL has an unusual "extension" to group by syntax but this can be changed by server settings and relying on that extension is risky. I strongly urge you to include all columns (including case expressions) into the group by clause that do NOT use an aggregate function (sum/count/avg etc.)