Solved

TSQL split?

Posted on 2014-11-17
2
161 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 13

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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