Hi I have a Table as below
Month Year Rate
1 2013 100
2 2013 101
8 2014 105
The rate is the value for the Month(1)January and year 2013.
I wana have a query which need to get rate value between 3rd month 2013 and 5the month 2014.
The Output has to be
3 2013 101
4 2013 101
5 2013 101
to
4 2014 105
5 2014 105
Loading

RobbinsonPosted May 30, 2014, 6:16 AM
Get the result of query which I have given in last post into table variable/temp table.
Select * from [tablename]
where month <=3 and Month >=5 and (year=2013 || year=2014)
Which would help you to get the rows which are exists in a table matches the criteria. Now you need to insert records for months which are not exists and sort it by month and year.
To get what months are not in physical table, you just have table variable consists of 12 month number and you can have query against the result of above query to find missing months and insert it to the result. You can write like below scripts for that.
DECLARE @months table(
Month int NOT NULL
);
insert into @months
select 1 union
select 2 union
select 3 union
select 4 union
select 5 union
select 6 union
select 7 union
select 8 union
select 9 union
select 10 union
select 11 union
select 12
Hope, this will help you write further scripts for this issue.
Please don't forget to mark as answer if this post helps you.
Umesh KumarPosted May 30, 2014, 4:30 AM
My Senario is bit different.
Hi ,
I will explain the scenario. I have the below table with me
Month
Year
Rate
1
2013
100
2
2013
101
8
2013
105
1
2014
109
First column is the Month and 2nd column is the year and 3rd column is the rate for that month and year.
Here my intention is that I am making a query which returns me the Rate value between 1st month 2013 to 1st month of 2014.
The query should return me something as following.
Month
Year
Rate
1
2013
100
2
2013
101
3
2013
101
4
2013
101
5
2013
101
6
2013
101
7
2013
101
8
2013
105
9
2013
105
10
2013
105
11
2013
105
12
2013
105
1
2014
109
Thanks
Umesh K
RobbinsonPosted May 30, 2014, 1:02 AM
The query would be
Select * from [tablename]
where month <=3 and Month >=5 and (year=2013 || year=2014)
Hope this would help you out.
Please Don't forget to mark as answer if this post helps you in anyway.