Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Complex T-SQL Query

$
0
0

Hello Everyone,

I have a table named Measures which has MeasureCode, BeginDate, EndDate and these 3 columns make unique in the table. Along with these columns there is a surrogate key.

Ex:- If measurecode 'F1.1'  start from 2013-05-01 and end on 2013-09-30 and if measurecode restarts from 2013-Nov and end on 2013-Dec, then there will be 2 records one for each period and so on......If MeasureCode is active on first day of month then it is active whole month.

I have a report with parameters of Month & Year based on, it should retrieve data for last months data from Measure table. 

Ex:- if year = 2014 and month = 2, then reporting months are Aug 2013,Sept 2013, Oct 2013, Nov 2013, Dec 2013 & Jan 2014. Now I have to check for each month and return values like below.

MonthMeasureCodeBeginEndId

Aug-2013F1.12013-5-12013-9-301

Sept-2013F1.12013-5-12013-9-301

Oct-2013NULLNULLNULLNULL

Nov-2013F1.12013-11-1 2013-12-312

Dec-2013F1.12013-11-1 2013-12-312

Jan-2014F1.12014-1-12014-6-303

Create table Measures 
(Id Int Identity(1,1),MeasureCode Varchar(100), BeginDate Date, EndDate Date)

insert #T1
values ('F1.1','2013-05-01','2013-09-30')
	  ,('F1.1','2013-11-01','2013-12-31')
	  ,('F1.1','2014-01-01','2014-06-30')
	  ,('F1.2','2013-03-01','2013-04-30')
	  ,('F1.2','2014-01-01','2014-03-31')
Thanks in advance


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>