SQL calculations based on column value - mysql

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

Related

Join not summing CASE WHEN

What I'm looking to do is show my current performance for this month, compared with expected scheduled wins to come in and then display the total expected amount, by product type.
For clarity, I have two sub-products that I'm grouping under the same name.
My issue is that for my 'Charged' amount, it's keeping the two sub-products separate, where as the 'Scheduled' amount is working fine.
The table should look like:
Type | Charged | Scheduled | Expected
A 3 2 5
B 1 1 2
What's actually showing is:
Type | Charged | Scheduled | Expected
A 2 1 3
A 1 1 2
B 1 1 2
The code is as follows:
select
t2.product,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
SUM(charged) as 'Scheduled'
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
sum(charged) as 'Charged'
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY user_type
ORDER BY user_type ASC) as t1 on t1.type = t2.type
I appreciate I might not be explaining this incredibly well (and that my code is probably quite clunky - I'm still fairly new!) so any help/direction would be appreciated.
Thanks!
Just some suggestion
you have a column product in main select but you have type in subquery and not product
you should not use sigle quote around column name
ad you have group by user_type but you need group by type for charged
select
t2.type,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
SUM(charged) as Scheduled
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
sum(charged) as Charged
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY Type
ORDER BY Type ASC) as t1 on t1.type = t2.type

sql server 2008 running totals between 2 dates

I need to get running totals between 2 dates in my sql server table and update the records simultaneoulsy. My data is as below and ordered by date,voucher_no
DATE VOUCHER_NO OPEN_BAL DEBITS CREDITS CLOS_BAL
-------------------------------------------------------------------
10/10/2017 1 100 10 110
12/10/2017 2 110 5 105
13/10/2017 3 105 20 125
Now if i insert a record with voucher_no 4 on 12/10/2017 the output should be like
DATE VOUCHER_NO OPEN_BAL DEBITS CREDITS CLOS_BAL
------------------------------------------------------------------
10/10/2017 1 100 10 110
12/10/2017 2 110 5 105
12/10/2017 4 105 4 109
13/10/2017 3 109 20 129
I have seen several examples which find running totals upto a certain date but not between 2 dates or from a particular date to end of file
You should consider changing your database structure. I think it will be better to keep DATE, VOUCHER_NO, DEBITS, CREDITS in one table. And create view to calculate balances. In that case you will not have to update table after each insert. In this case your table will look like
create table myTable (
DATE date
, VOUCHER_NO int
, DEBITS int
, CREDITS int
)
insert into myTable values
('20171010', 1, 10, null),( '20171012', 2, null, 5)
, ('20171013', 3, 20, null), ('20171012', 4, 4, null)
And view will be
;with cte as (
select
DATE, VOUCHER_NO, DEBITS, CREDITS, bal = isnull(DEBITS, CREDITS) * case when DEBITS is null then -1 else 1 end
, rn = row_number() over (order by DATE, VOUCHER_NO)
from
myTable
)
select
a.DATE, a.VOUCHER_NO, a.DEBITS, a.CREDITS
, OPEN_BAL = sum(b.bal + case when b.rn = 1 then 100 else 0 end) - a.bal
, CLOS_BAL = sum(b.bal + case when b.rn = 1 then 100 else 0 end)
from
cte a
join cte b on a.rn >= b.rn
group by a.DATE, a.VOUCHER_NO, a.rn, a.bal, a.DEBITS, a.CREDITS
Here's another solution if you can not change your db structure. In this case you must run update statement each time after inserts. In both cases I assume that initial balance is 100 while recalculation
create table myTable (
DATE date
, VOUCHER_NO int
, OPEN_BAL int
, DEBITS int
, CREDITS int
, CLOS_BAL int
)
insert into myTable values
('20171010', 1, 100, 10, null, 110)
,( '20171012', 2, 110, null, 5, 105)
, ('20171013', 3, 105, 20, null, 125)
, ('20171012', 4, null, 4, null, null)
;with cte as (
select
DATE, VOUCHER_NO, DEBITS, CREDITS, bal = isnull(DEBITS, CREDITS) * case when DEBITS is null then -1 else 1 end
, rn = row_number() over (order by DATE, VOUCHER_NO)
from
myTable
)
, cte2 as (
select
a.DATE, a.VOUCHER_NO
, OPEN_BAL = sum(b.bal + case when b.rn = 1 then 100 else 0 end) - a.bal
, CLOS_BAL = sum(b.bal + case when b.rn = 1 then 100 else 0 end)
from
cte a
join cte b on a.rn >= b.rn
group by a.DATE, a.VOUCHER_NO, a.rn, a.bal
)
update a
set a.OPEN_BAL = b.OPEN_BAL, a.CLOS_BAL = b.CLOS_BAL
from
myTable a
join cte2 b on a.DATE = b.DATE and a.VOUCHER_NO = b.VOUCHER_NO

Select with count

So.. here is the thing.. Im trying to get a report based on this table on Mysql.
Table have these columns
statusvalue submitdate employeeNumber
2 2016-11-17 17:49:05 2
1 2016-11-17 17:49:11 4
3 2016-11-17 17:49:16 4
4 2016-11-17 17:50:58 2
5 2016-11-17 17:51:07 5
Status 0 = Pending
1= Started
2=inprogress
3= cancelled
4= terminated
5=unknown
now i need to select data as below
date day pending started inprogress cancelled terminated total
2017-04-17 Monday 0 1 4 0 1 6
2017-04-16 Sunday 0 1 4 0 1 6
so far i can get the date and the day
select date(submitdate) as subdate, case weekday(date(submitdate))
WHEN 0 then 'PENDING'
WHEN 1 then 'STARTED'
WHEN 2 then 'INPROGRESS'
WHEN 3 then 'CANCELLED'
WHEN 4 then 'TERMINATED'
WHEN 5 then 'UNKNOWN' END as submitdate
from tb_status t1 where employeenumber=15 group by subdate order by subdate ;
however not quite sure how to do a count based on status.. any help would be greatly appreciated.
You just need a basic pivot query here. Aggregate over the submission date, and then tally the various types of status as you go along.
SELECT
DATE(submitdate) AS subdate,
WEEKDAY(submitdate) AS day,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 0 THEN 1 END) AS PENDING,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 1 THEN 1 END) AS STARTED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 2 THEN 1 END) AS INPROGRESS,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 3 THEN 1 END) AS CANCELLED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 4 THEN 1 END) AS TERMINATED,
SUM(CASE WHEN WEEKDAY(DATE(submitdate)) = 5 THEN 1 END) AS UNKNOWN
FROM tb_sattus t1
WHERE employeenumber = 15
GROUP BY DATE(submitdate)
ORDER BY subdate
Note that your current query is not correct, because you are selecting string literals based on the status. Instead, in my query the various status types appear as column names, with the counts you want instead being selected.
Give this a try:
SELECT
`submitDate` as `date`,
DATE_FORMAT(`submitDate`,'%a') as `day`,
SUM(if(`statusvalue` = 0,1,0)) as `pending`,
SUM(if(`statusvalue` = 1,1,0)) as `started`,
SUM(if(`statusvalue` = 2,1,0)) as `inprogress`,
SUM(if(`statusvalue` = 3,1,0)) as `cancelled`,
SUM(if(`statusvalue` = 4,1,0)) as `terminated`,
SUM(if(`statusvalue` = 5,1,0)) as `unknown`,
SUM(if(NOT `statusvalue` IN (0,1,2,3,4,5),1,0)) as `invalid`,
COUNT(`statusvalue`) as `total`
FROM `tbstatus`
GROUP BY `submitDate`
ORDER BY `submitDate`;

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.)

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

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;