Solved

TSQL split?

Posted on 2014-11-17
2
157 Views
Last Modified: 2014-11-17
Hello,
There is a table with [periodGain] metric and that has a start and end date. I want to convert that data in such a way so that periodGain metric and its info are repeated for several rows at a single date level – where the date is between the start and the end. Please see attached excel. Let me know how I can do it.
Thank you
R
tsql-date-everyday.xlsx
0
Comment
Question by:Rayne
2 Comments
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 500 total points
ID: 40448940
This should do the trick:

--Test data

Create table #PeriodGain
(	periodGain		nvarchar(12)
,	category		nvarchar(12)
,	sub_group		nvarchar(12)
,	[start_DATE]	datetime
,	end_DATE		datetime
)

insert into #PeriodGain
values	('p0045', 'cat12', 's1', '20140101', '20140109')
	,	('p0098', 'cat19', 's2', '20140305', '20140321');
	
-- Insert the record with the start date in a CTE
-- then add a recursive part to add all the 
-- required dates between the start and end date.
With A
	(	periodGain
	,	category
	,	sub_group
	,	date_everyday	)
as
(
Select		periodGain
		,	category
		,	sub_group
		,	[start_DATE]
from	#PeriodGain

union all

Select		p.periodGain
		,	p.category
		,	p.sub_group
		,	DATEADD(day, 1, A.date_everyday)
from	A
		inner join #PeriodGain p
			on a.periodGain = p.periodGain
			and a.category = p.category
			and a.sub_group = p.sub_group
Where	A.date_everyday < p.end_DATE
)

-- Select final data set

select *
from A
order by	a.periodGain
		,	a.category
		,	a.sub_group
		,	a.date_everyday

-- Drop the temp table
drop table #PeriodGain

Open in new window


Using a recursive query you can keep adding the dates to the dataset until the condition A.date_everyday < p.end_DATE is met.
0
 

Author Comment

by:Rayne
ID: 40449132
Thank you Koen, this is AWESOME :)
I may ask question later if any

Thank you friend
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now