sum up every 12 months in a table - sql-server

i have a table calculating the installments. in that table i'm saving all the data recording to that. For example if i'm calculating for 60 installments and saving all the data,so it is like 60 months. so now i need to sum up the value of one column for every 12 months. sometimes v start paying the installments from the middle of the year also.
my DB looks like this.the highlighted column must sum up for every 12 months. two images are one table only
suppose i have 30 installments from starting on jun 2012.suppose i started paying installment from jun 2012 then should sum up the installments from jun 2012 to may 2013. v can't use group by year. i must sum up like this ................................................................................‌​
sum jun 2012 to may 2013
sum jun 2013 to may 2014
sum jun 2014 to nov 2014 ( only 6 months left)

You can use ROW_NUMBER to generate a group of 12 months:
WITH Cte AS(
SELECT *,
RN = (ROW_NUMBER() OVER(ORDER BY InstallmentMonth) - 1)/ 12
FROM your_table
)
SELECT
SUM(InteresetPerInstallment)
FROM Cte
GROUP BY RN

Related

how to find the max number of days for each city?

As you see below, there are many cities and days(period of raining days).
I want to find the max days even if days are the same.
For example if maximum days are 3 and there are two 3 days, then i want to print out two rows.
Possible outputs would be:
Auckland 2013-11-30 2013-11-30 5
Christchurch 2013-11-10 2013-11-50 4
If there are only 5 cities, there might be 5 rows to 10 rows depending on the same value of days.
I want to use SELECT, IF or CASE, MAX or Count functions, as this part is one of the complete, complex code.
Thank you.
SQL Version:
Microsoft SQL Server 2016 (RTM-GDR) (KB4019088) - 13.0.1742.0 (X64) Jul 5 2017 23:41:17 Copyright (c) Microsoft Corporation Developer Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 (Build 9600: ) (Hypervisor)
this is the example data:
create TABLE practice10 (
station VARCHAR(50),
start_date DATE,
end_date DATE,
days INT,
)
INSERT INTO practice10 values ('Auckland','2013-10-5','2013-10-10', 5),
('Auckland','2013-10-15','2013-10-17', 2),
('Auckland','2013-10-20','2013-10-23', 3),
('Manchester','2015-9-1','2013-9-4', 3),
('Manchester','2013-10-3','2013-10-3', 0),
('Manchester','2013-10-20','2013-10-29', 9);
Order days using dense_rank(). Then use top 1 to get days with highest values
select
top 1 with ties *
from
myTable
order by dense_rank() over (partition by station order by days desc)

How to generate automatic Rows

I have Below Sample Table
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
I am executing following query
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
I need following output
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
QuarterYTD 2016 85 50
4th record will generate automatically when I execute query
Based on the data given, you could UNION in grouping like the following. However, I assume that your data is more complex and some additional code would need to be added to the query.
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
UNION ALL
select 'QuarterYTD', Year, sum(budget_amt), sum(Actual_Amt) from Table where month in ('Feb','Mar','apr') group by Year

Capture date range of different dateYear , DateMonth columns

I have a table that captures monthly data using a dateYear and dateMonth to record date. I;m having trouble capturing data between two different years.
For example : between dateYear 2015 and dateMonth 09 through dateMonth 02 and dateYear 2016
In english , between September 2015 and Feb 2016
whats the best way to go about this.. Ive tried grouping through the where clause but the months overlap..
is there a date function or concatenation method i should use?>
SELECT
prodDays
,hours
FROM Table
WHERE 1=1
AND date_year BETWEEN 2015 AND 2016
Sounds like you are looking for this where clause:
where (date_year = 2015 and date_month >= 9) or (date_year = 2016 and date_month <= 2)

SQL Query-Multiple date ranges

I want to fetch some data from DB by giving multiple date ranges. Example,in February I want to get weekly report from a table in this order Feb 01 to 07, Feb 07 to 14, Feb 14 to 21, Feb 21 to 28 and Feb 28 to Mar 01. In DB the records are stored in a daily wise not in weekly wise. I want to cluster it as weekly wise and calculate sum then show the result. Please help me if you know this case.
For clear cut view, consider 3 tables & its columns.
Table A:id,timestamp (comment-data is inserted daily)
Table B:id,fruits
Table C:id,fruits_type
Result:
fruits_type count(id) timestamp
apple 3 01-02-2016 to 07-02-2016
orange 5 01-02-2016 to 07-02-2016
pineapple 8 01-02-2016 to 07-02-2016
apple 4 07-02-2016 to 14-02-2016
orange 5 07-02-2016 to 14-02-2016
Conditions:id should match among 3 tables;fetch data by providing group by fruits_type and timestamp should be in weekly wise.
Please help if you know this
To get the sum of all values between two dates you would do it like this:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN '2/1/2016' AND Date1 <'2/7/2016'
If you want to make it more flexible and have the query get the last week's sum you can use the DATEADD function to lag by one week:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN DATEADD(week, -1, GETDATE()) AND Date1 < GETDATE()
If you want the result set to include a row for each week, you can use UNION to merge the queries.

Sql Query for an ssrs report

I have the following report I need to generate . I am new to SSRS.
Number of Stores with June July Aug
0 sales 12 34 32
1 to 9 Sales 12 34 45
10+ sales 15 45 54
The tables I have are as follows :
Store :
YEAR MONTH StoreName
2013 10 ABC
2013 10 DEF
2013 09 JKL
2013 06 FGH
Store Sales : (Contains only stores whose sales are > 0)
YEAR MONTH StoreName NumberOfSales
2013 10 ABC 3
2013 09 JKH 14
2013 10 FRH 9
I am not really sure what I need to do to get the report in the above format ?
I can write a query for sales in a single month , but how do I write a query to
get the report for all 3 months ? Or is there a better way to do these reports using ssrs ?
If you can direct me on what to do it will be great ?
You want to set up your query like this:
SELECT Year, Month, (CASE
WHEN NumberOfSales = 0 THEN '0 Sales'
WHEN NumberOfSales BETWEEN 1 AND 9 THEN '1-9 Sales'
WHEN NumberOfSales > 9 THEN '10+ Sales' END) [SaleGroup]
FROM FactStoreSales
Then you basically want to go into SSRS and just throw it into a table with the month in the columns and the group in the rows and let it pivot the data itself rather than trying to do that in SQL.

Resources