how to count SUM YTD format - sql-server-2008

I have table payments(Actual there is about 100k records, different categories, clients etc):
Client Dt Amt Category
1 201312 10 Tax
1 201401 10 Tax
1 201405 10 Tax
1 201406 10 Tax
2 201311 10 Tax
And i want to make cumulative sum YTD for category tax for every client. So the result will be like this:
Client Dt Amt Category
1 201312 10 Tax
1 201401 10 Tax
1 201405 20 Tax
1 201406 30 Tax
2 201311 10 Tax
Thank you

Try this:
SELECT a.Client, a.Dt, SUM(b.Amt) AS Amt, a.Category
FROM payments a
JOIN payments b ON b.Client = a.Client
AND b.Category = a.Category
AND b.Dt <= a.Dt
AND YEAR(b.Dt) = YEAR(a.Dt)
WHERE a.Category = 'Tax'
GROUP BY a.Client, a.Dt, a.Category

Related

Get user who had no activity in from last 12 months

I want to make SQL that will return users who not performed any points/redeem activity in last 12 months
user_data
userId email create_date
1 steve#gmail.com 2017-01-05 12:55:00
2 mark_nel#gmail.com 2019-05-15 12:13:00
3 les.born#gmail.com 2018-04-05 03:15:00
points_data
id user_id activity_id activity_points create_date
1 1 1 10 2017-01-05 11:09:00
2 2 1 15 2019-06-12 09:17:00
3 2 2 10 2019-08-05 02:55:00
4 3 1 10 2019-01-05 07:15:00
redeem_data
id user_id redeem_points create_date
1 2 20 2019-09-11 02:55:00
Result
email
steve#gmail.com
Use LEFT JOIN concept to include non matching row from table user_data and only select non matching row by removing matching rows using where id of both tables(points_data,redeem_data) is null.
SELECT u.email
FROM user_data u
LEFT JOIN points_data p ON u.userId = p.user_id AND p.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
LEFT JOIN redeem_data r ON u.userId = r.user_id AND r.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
WHERE p.id IS NULL AND r.id IS NULL;

I need two different column values based on different condition from same table

Heading
i want to get result sum of 1st record of each 'tank id' per day wise and sum of 'receipt' per day wise and whole data will be limited for particular month
SELECT
DATE(t.DATE) AS 'Date',
SUM(t.in_stock) AS 'Opening Stock',
SUM(receipt) AS 'Receipt',
SUM(in_stock + receipt)AS 'Total Stock'
FROM diesel_tank_details t
WHERE NOT EXISTS (
SELECT
1
FROM diesel_tank_details t2
WHERE
DATE(t2.DATE) = DATE(t.DATE)
AND t2.tank_id = t.tank_id
AND t2.dt_id < t.dt_id
)
AND YEAR(DATE) = 2019
AND MONTH(DATE) = 7
GROUP BY DATE(DATE)
Tank_ID In_Stock Receipt Date
1 1000 1000 2019-07-10 00:28:33
2 2000 2000 2019-07-10 00:28:40
3 3000 3000 2019-07-10 00:28:47
1 300 0 2019-07-10 00:32:40
1 250 0 2019-07-15 15:15:06
3 2500 0 2019-07-15 15:15:37
2 1800 0 2019-07-15 15:35:49
3 2200 0 2019-07-15 16:13:17
1 500 250 2019-07-15 16:13:51
2 2000 200 2019-07-15 16:13:57
3 3000 800 2019-07-15 16:14:03
i need this Output from above table
Date Opening Stock Receipt Total Stock
2019-07-10 6000 6000 12000
2019-07-15 4550 1250 4550
Since you want different conditions for stock and receipt you must sum different rows for each case:
select s.day, s.`Opening Stock`, r.Receipt,
(s.`Opening Stock` + s.Receipt) `Total Stock`
from (
select g.day, sum(t.in_stock) `Opening Stock`, sum(t.receipt) receipt
from diesel_tank_details t inner join (
select date(date) day, tank_id, min(date) mindate
from diesel_tank_details
where in_stock <> 0
group by date(date), tank_id
) g on g.tank_id = t.tank_id and g.mindate = t.date
group by g.day
) s inner join (
select g.day, sum(t.receipt) Receipt
from diesel_tank_details t inner join (
select date(date) day, tank_id, min(date) mindate
from diesel_tank_details
where receipt <> 0
group by date(date), tank_id
) g on g.tank_id = t.tank_id and g.mindate = t.date
group by g.day
) r on r.day = s.day
See the demo.
Results:
| day | Opening Stock | Receipt | Total Stock |
| ---------- | ------------- | ------- | ----------- |
| 2019-07-10 | 6000 | 6000 | 12000 |
| 2019-07-15 | 4550 | 1250 | 4550 |
Query:
SELECT DATE(t.Date) as 'Date',
SUM(t.In_Stock) as 'Opening Stock',
SUM(t.Receipt) as 'Receipt',
SUM(t.In_Stock + t.Receipt) as 'Total Stock'
FROM diesel_tank_details t
WHERE AND YEAR(t.Date) = 2019
AND MONTH(t.Date) = 7
GROUP BY DATE(t.Date)
Just include your additional filter. I don't know what DT_ID does so i just removed it. but basically you just wanted to group it by day.

Two where "date" clause to have different column

I'm trying to have 2 different columns with different date in mysql
For example I have this table
id name amount date
1 Jane 20.00 2015-08-14
2 Joe 10.00 2015-08-15
3 Joe 20.00 2015-08-21
4 Jane 30.00 2015-09-21
5 Joe 20.00 2015-09-25
6 Jane 10.00 2015-09-25
I want to get the sum of amount groupby name and display two columns for different date one is
where date <= '2015-08-31' and the other one is
where date <= '2015-09-30'
My desired output is
id name amount_aug amount_sep
1 Jane 20.00 60.00
2 Joe 30.00 50.00
My query so far is select name, SUM(amount) amount_sum from table_name groupby name which will simply output
name amount_sum
Jane 60.00
Joe 50.00
SELECT
name,
SUM(
IF( date <= '2015-08-31', amount, 0 )
) amount_aug,
SUM(
IF( date <= '2015-09-30', amount, 0 )
) amount_sep
FROM table_name
GROUP BY name
Here is an example:
select
name,
sum(case when date <= '2015-08-31' then amount else 0 end) amount_on_or_before_aug,
sum(case when date <= '2015-09-30' then amount else 0 end) amount_on_or_before_sept
from table_name
group by name
SELECT a.id, a.name, sum(b.amount) AS 'amount_aug', sum(a.amount) AS 'amount_sep'
FROM table_name a
LEFT JOIN table_name b ON a.id = b.id AND b.date <= '2015-08-31'
WHERE a.date <= '2015-09-30'
GROUP BY a.name

How to combine many to many join?

Tables:
cust table:
cust_id, name, etc
bill table:
bill_id, cust_id, amt, due_date, status (open/closed)
payment table:
payment_id, bill_id, amt etc
Customer can settle a single bill by paying multiple installments. So, one bill_id may relate to payment_ids.
I am unable to generate this recordset:
cust_id | due amt
'due amt' is the sum of all bill.amts - sum of all payment.amts and having status open.
Bill table
bill_id cust_id amt status
1 18 200 open
2 18 200 open
3 17 200 open
4 17 200 open
5 17 200 open
6 17 200 closed
Payment table
payment_id bill_id cust_id amt
1 1 18 50
2 2 18 40
3 3 17 10
Expected output
cust_id due_amt hint/how
17 590 (600-10) .. 600 -> because one is closed
18 310 (400-(50+40))
select c.cust_id, sum(b.amt) - sum(p.amt) as due_amt
from cust c
left join bill b on b.cust_id = c.cust_id and b.status = 'open'
left join payment p on p.bill_id = b.bill_id
group by c.cust_id
SQLFiddle demo

self-join mysql table

payday
id day employee income expenses tax
1 7 3 600 100 30
2 14 3 650 150 35
3 14 2 680 200 38
SELECT p.income, p.tax, ps.expenses
FROM paydays p
LEFT JOIN paydays ps ON ps.day+7 = p.day
WHERE p.day = 14 AND p.employee = 3
this gives what i want, 650 income and 35 tax from row with day 14,
and 100 expenses from row with day 7
now the problem,
SELECT SUM(p.income), SUM(p.tax), SUM(ps.expenses)
FROM paydays p
LEFT JOIN paydays ps ON ps.day+7 = p.day
WHERE p.day = 14
it should give
income = 1330
tax = 73
expenses = 100
but it multiplies fields by 2, income = 2660, tax = 146.....
Maybe I am missing something in your explanation but can't you use a subquery to get the expenses, similar to this:
SELECT SUM(p.income) TotalIncome,
SUM(p.tax) TotalTax,
ps.expenses Expenses
FROM paydays p
INNER JOIN
(
select sum(expenses) expenses, day
from paydays
where day = 7
group by day
) ps
ON ps.day+7 = p.day
WHERE p.day = 14
GROUP BY ps.expenses
See SQL Fiddle with Demo
The result is:
| TOTALINCOME | TOTALTAX | EXPENSES |
-------------------------------------
| 1330 | 73 | 100 |
SELECT
SUM(payday.income) as Income ,
SUM(payday.tax) as Tax,
p.expenses as Expanses
FROM payday
LEFT JOIN payday as p ON p.id = payday.id - 1
WHERE day = 14