SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Tally generator


Tally generator

Author
Message
Sergiy
Sergiy
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49465 Visits: 12745
Comments posted to this topic are about the item Tally generator
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)

Group: General Forum Members
Points: 425136 Visits: 43411
Can't believe whomever the editor was didn't catch the missing "y" in the title. They can fix it for you, Sergiy.

Good to see you've finally picked up the pen on SSC.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sergiy
Sergiy
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49465 Visits: 12745
I had status "Commited" for 2 years in a busy Production system.
Not a soul complained.

Till I spotted and fixed it myself.
:-)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)

Group: General Forum Members
Points: 425136 Visits: 43411
Sergiy - Thursday, February 16, 2017 10:36 PM
I had status "Commited" for 2 years in a busy Production system.Not a soul complained.Till I spotted and fixed it myself.:-)

I'm more concerned that people won't find your script if they do a site search for the word "Tally".


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sergiy
Sergiy
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49465 Visits: 12745
I posted it only because it's used in the article scripts, so if it gets published I can refer to this post.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)SSC Guru (425K reputation)

Group: General Forum Members
Points: 425136 Visits: 43411
Oh yeah... I got that from the beginning. Still, it's useful by itself and it would be nice if people could find it. But, it's your script. I'm just making a suggestion.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)SSC Guru (81K reputation)

Group: General Forum Members
Points: 81438 Visits: 20465
Nice, Sergiy, and very useful.
I'm guessing that you've checked out all of the alternatives to get rid of the row count spool. I've only had a little time to play today and came up with this:

[code language="sql"]

DECLARE

@StartValue bigint = null, -- -32768,

@EndValue bigint = null, -- 32767,

@Rows INT = 980, -- number of rows to bereturned. Used only when either @StartValue or @EndValue is not supplied

@Increment smallint=3;

WITH BaseNum (N) as (

select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all

select 1 union all select 1 union all select 1 union all select 1 union all select 1

),

L1 (N) as (SELECT 1 FROM BaseNum bn1 CROSS JOIN BaseNum bn2),

L2 (N) as (SELECT 1 FROM L1 a1 CROSS JOIN L1 a2),

_Tally (N) as (

SELECT TOP(ISNULL(@EndValue - @StartValue + @Increment,@Rows)/@Increment)

rn= ISNULL(@StartValue,0) + ((ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1)*@Increment)

FROM L2 a1 CROSS JOIN L2 a2 CROSS JOIN L2 a3

)

SELECT * FROM _Tally

[/code]


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
George W (no relation)
George W (no relation)
Mr or Mrs. 500
Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)

Group: General Forum Members
Points: 560 Visits: 427
I actually came to the article because I wanted to find out what a "Tall Generator" was Smile.
Sergiy
Sergiy
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49465 Visits: 12745
ChrisM@Work - Friday, February 17, 2017 9:38 AM
Nice, Sergiy, and very useful.
I'm guessing that you've checked out all of the alternatives to get rid of the row count spool. I've only had a little time to play today and came up with this:

[code language="sql"]

DECLARE

@StartValue bigint = null, -- -32768,

@EndValue bigint = null, -- 32767,

@Rows INT = 980, -- number of rows to bereturned. Used only when either @StartValue or @EndValue is not supplied

@Increment smallint=3;

WITH BaseNum (N) as (

select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all

select 1 union all select 1 union all select 1 union all select 1 union all select 1

),

L1 (N) as (SELECT 1 FROM BaseNum bn1 CROSS JOIN BaseNum bn2),

L2 (N) as (SELECT 1 FROM L1 a1 CROSS JOIN L1 a2),

_Tally (N) as (

SELECT TOP(ISNULL(@EndValue - @StartValue + @Increment,@Rows)/@Increment)

rn= ISNULL(@StartValue,0) + ((ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1)*@Increment)

FROM L2 a1 CROSS JOIN L2 a2 CROSS JOIN L2 a3

)

SELECT * FROM _Tally

[/code]

I have an unfinished "v.2." script which takes better care of spooling, and is also meant to work with odd combinations of input parameters: negatives, opposite signs, nulls here and there, etc.
I'm still don't feel like I collected all the test cases.
:-)

o103452
o103452
SSC-Addicted
SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)SSC-Addicted (427 reputation)

Group: General Forum Members
Points: 427 Visits: 4522
Hi Sergiy, thanks for the script. Please keep us posted on your updated "V2" script. Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search