Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Generate Rows of data from table

Author  Topic 

Shelly1
Starting Member

25 Posts

Posted - 2013-01-18 : 09:07:38
Hi All, i need to create rows of data from the table of results below:

AnswerDelay DelayTime
2 1
4 5
6 6
8 28
10 27
12 12
14 20
16 25

I would need to see the following:

AnswerDelay
2
4
4
4
4
6
6
6
6
6
6

Basically i need to know the number of times a time delay of say 2 occured but in a list format

Any ideas? cant seem to find much on the internet to guide me
thanks





Mich

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-18 : 13:24:22
If you have a numbers table in your database, use that instead of the master..spt_values that I am showing below:
SELECT
y.AnswerDelay,
y.DelayTime
FROM
YourTable y
CROSS JOIN MASTER..spt_values s
WHERE
s.type = 'P' AND s.number BETWEEN 1 AND AnswerDelay
I am assuming that 2 is shown only once in your results is a typo.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-18 : 13:49:10
logic independt of any internal/system tables

;With DelayTimes
AS
(
SELECT AnswerDelay,CAST(1 AS int) AS Counter,DelayTime
FROM table
UNION ALL
SELECT AnswerDelay,Counter+1,DelayTime
FROM DelayTimes
WHERE Counter+1<= DelayTime
)
SELECT AnswerDelay
FROM DelayTimes

OPTION (MAXRECURSION 0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Shelly1
Starting Member

25 Posts

Posted - 2013-01-21 : 06:09:06
Hi guys thank you very much both excellent so pleased :)

Mich
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-21 : 06:54:01
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -