How to maintain a running balance in a month wise report - sql

SELECT *
FROM
(SELECT
YEAR (DateOfTransaction) AS year,
LEFT(DATENAME(MONTH, DateOfTransaction), 3) AS month,
SUM(CASE WHEN TransTypeName LIKE 'credit%' THEN amount ELSE 0 END) -
SUM(CASE WHEN TransTypeName LIKE 'Debit%' THEN amount ELSE 0 END) AS Balance
FROM
.............) AS t
PIVOT (SUM(balance) FOR month IN (jan, feb, march, ...., Dec)) AS pvt
This query returns a month-wise report account balance. I want a result is running balance.
Example:
January month I credit 5000, February month I credit 2000
My query result is
year jan feb march...dec
2014 5000 2000 null ..null
I want a result like this:
year jan feb march...dec
2014 5000 7000 null ..null
(5000+2000)

Try this
SELECT year,Jan = Jan, Feb = isnull(Jan,0)+isnull(Feb,0),....
FROM
(SELECT
YEAR (DateOfTransaction) AS year,
LEFT(DATENAME(MONTH, DateOfTransaction), 3) AS month,
SUM(CASE WHEN TransTypeName LIKE 'credit%' THEN amount ELSE 0 END) -
SUM(CASE WHEN TransTypeName LIKE 'Debit%' THEN amount ELSE 0 END) AS Balance
FROM
.............) AS t
PIVOT (SUM(balance) FOR month IN (jan, feb, march, ...., Dec)) AS pvt)t
Or you can simply add a temp table which stores numbers from 1 to 12
inner join #temp on n>=datepart(mm,DateofTransaction) group by year(transaction), n

Related

How to compare two date values in SQL Server?

I have a table with the date 2018 and date 2019 and here is my data.
select amount, date
from TABLE1
where date in (2018, 2019) and empid = 21120
It didn't work for me as the query says case when date =2019 then amount as my data above when date=2019 it will just show me the amount. This is what my result is:
Amount Data
9.67 2019
21 2019
6 2019
9.56 2018
42 2018
7 2018
What is want it: the difference between the Amount for the two dates 2018 and 2019.
The above query gives me only one row of the amount for those dates.
My expected result is to find the difference in the amount between 2018 and 2019. Any ideas on how I could get the difference. I am trying self joining but any help is appreciated.
Ex: 2019 amount 9.67 - 2018 amount 9.56.
You seem to be looking for conditional aggregation:
select
sum(case when date = 2019 then amount else 0 end)
- sum(case when date = 2018 then amount else 0 end) diff
from mytable
where empid = 21 and date in (2018, 2019)
The query gives you the difference between the total amount of 2019 and that of 2018 for the given employee.
This can be shortened a little:
select sum(case when date = 2019 then amount else -amount end) diff
from mytable
where empid = 21 and date in (2018, 2019)
You can do as
SELECT ABS(SUM(CASE WHEN Data = '2019' THEN Amount ELSE -Amount END)) Diff
FROM
(
VALUES
(9.67 , '2019'),
(21 , '2019'),
(6 , '2019'),
(9.56 , '2018'),
(42 , '2018'),
(7 , '2018')
) T(Amount, Data)
WHERE Data IN ('2018', '2019')
-- AND empid = 21120; Uncomment this when you run it against your table

Display 12 months of data from the past 5 years

I am currently creating a script that will pull 5 years of invoice data and will summarize the invoice amounts by month of that year for a specific customer. Example
Year jan feb mar
2011 800 900 700
2012 700 800 900, and so forth
I am having issues getting my output to be like this though. My current code
select MAX(cust) as customer,year(invoicedate) as y, month(invoicedate) as m, sum(amount) as summary
from #tquery
group by year(dinvoice), month(dinvoice)
having MAX(ccustno) ='WILLAMETTE'
order by y asc,m asc
select * from #tquery
gives me this. which i just need to find a way to reformat it.
customer year month amount
WILLAMETTE 2012 11 500
WILLAMETTE 2012 12 600
WILLAMETTE 2013 1 600
No need to go through a Pivot. It is only 12 columns. A conditional aggregation would be more efficient
Select Customer = cust
,Year = year(invoicedate)
,Jan = sum(case when month(invoicedate) = 1 then amount else 0 end)
,Feb = sum(case when month(invoicedate) = 2 then amount else 0 end)
...
,Dec = sum(case when month(invoicedate) =12 then amount else 0 end)
From #tquery
Group by ccustno,year(dinvoice)
Order By 1,2
You must using PIVOT to reformat rows to column
select customer
,y
,"1","2","3","4","5","6","7","8","9","10","11","12"
from (select cust as customer,year(invoicedate) as y, month(invoicedate) as m,amount
from #tquery
where ccustno ='WILLAMETTE'
)
t
pivot (sum (amount) for m in ("1","2","3","4","5","6","7","8","9","10","11","12")) p
order by y
;

How to calculate a growing or decreasing percentage between rows usign group by

Let's suposse that I have this table_1:
Year Item Qty_sold
2013 1 3
2013 2 2
2013 3 5
2014 1 2
2014 2 3
I'll perform something like this
select year , sum(Qty_sold) as Quantity
from table_1 inner join table_2 on .... inner join table_n
where Year = 2014
The final result depends mostly on the filter by year, but there are other tables involved.
But as a result I need something like this:
Year Quantity Diff_Percentage
2014 5 0.5
Because during the previous year (2013) the final quantity of items sold was 10.
Regards
You seem to want something like this:
select year, sum(Qty_sold) as Quantity,
lag(sum(qty_sold)) over (order by year) as prev_Quantity,
(1 - Quantity / lag(sum(qty_sold)) over (order by year)) as diff_percentage
from table
group by Year;
Of course, this returns the info for all years. If you really just want the year 2014 and 2013 then use conditional aggregation:
select year,
sum(case when year = 2014 then Qty_sold end) as Quantity_2014,
sum(case when year = 2013 then Qty_sold end) as Quantity_2013,
(1 - sum(case when year = 2014 then Qty_sold end)/
sum(case when year = 2013 then Qty_sold end)
) as diff_percentage
from table
where Year in (2013, 2014);
I'm sort of guessing on the formula for diff_percentage, but I think that's what you want.
As per your requirement, please try this
select t.year,qty, (1-qty/prev_qty)
from
(select year, sum(Qty_sold) as qty,
lag(sum(qty_sold)) over (order by year) as prev_qty
from tbl group by Year) t where t.year=in_year --in_year whichever year record you want.
Hope it works for you.

Calculate contribution margin

I'd like to use subqueries and calculation at the same time for my sql project
I have a table A where has the following information:
Table A
Month_revenue Income Cost
-------------------------
Jan 100 50
Feb 90 60
Mar 80 40
And I'd like to find the contribution margin for Jan, Feb and the difference of the contribution margins between Jan and Feb. Can I do that in one query and how?
The display should have the following format:
Jan Feb Mar Jan/Feb Feb/Mar
---------------------------------------------------------------
100-50 90 - 60 80-40 (100-50) - (90-60) (90-60) - (80-40)
Thanks!
You can simply use three selections to the table:
select
jan.income - jan.cost as jan,
feb.income - feb.cost as feb,
mar.income - mar.cost as mar,
(jan.income - jan.cost) - (feb.income - feb.cost) as jan_feb,
(feb.income - feb.cost) - (mar.income - mar.cost) as feb_mar
from
(select * from mytable where Month_revenue = 'Jan') jan
cross join
(select * from mytable where Month_revenue = 'Feb') feb
cross join
(select * from mytable where Month_revenue = 'Mar') mar;
Or you can aggregate conditionally:
select
sum(case when Month_revenue = 'Jan' then income - cost end) as jan,
sum(case when Month_revenue = 'Feb' then income - cost end) as feb,
sum(case when Month_revenue = 'Mar' then income - cost end) as mar,
sum(case when Month_revenue = 'Jan' then income - cost end) -
sum(case when Month_revenue = 'Feb' then income - cost end) as jan_feb,
sum(case when Month_revenue = 'Feb' then income - cost end) -
sum(case when Month_revenue = 'Mar' then income - cost end) as feb_mar
from mytable;

SQL Table UPDATE by row with days per month

SQL Server 2005:
I'm attempting to create a table that looks like this:
JAN | FEB | March .......Dec | YTD
Total Volume:
Days in Month:
Gallons per day AVG:
Three rows with description on left, 13 columns (one for each month and year to date total).
I know how to populate the total volume per month. What would I use for days per month and average? I'd like the days per month to show either the complete number of days if it is a past month or current completed days if its the current month.
You are pivoting the data, so you want the results in columns. You can do this by using direct calculation. Here is an example for the first three months:
select 'Days In Month' as col1,
(case when month(getdate()) < 1 then 0
when month(getdate()) = 1 then day(getdate())
else 31
end) as Jan,
(case when month(getdate()) < 2 then 0
when month(getdate()) = 2 then day(getdate())
when year(getdate()) % 4 = 0 then 29
else 28
end) as Feb,
(case when month(getdate()) < 3 then 0
when month(getdate()) = 3 then day(getdate())
else 31
end) as Mar,