Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

Random Number Generator Expand / Collapse
Author
Message
Posted Wednesday, May 01, 2013 9:47 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:56 PM
Points: 59, Visits: 120
Actually I am having to wait until my support desk can download the SQL development tools to my desktop on Thursday morning. Once they do I will be testing it... so I will let you know how it works... Thx Again!!!!
Post #1448498
Posted Wednesday, May 01, 2013 9:48 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:56 PM
Points: 59, Visits: 120
We are currently using Access 2007 but the powers that be want us to move to a SQL Server backend and have access 2007 basically lay over the top of it as a front end option... so Im sure I will be posting alot on here cause I dont know SQL yet...
Post #1448504
Posted Tuesday, May 07, 2013 7:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 11, 2013 10:36 PM
Points: 31, Visits: 335
really a nice solution
Post #1450145
Posted Wednesday, May 22, 2013 11:42 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:56 PM
Points: 59, Visits: 120
Well,

I have finally had a chance to test this and I keep getting an error message saying:

Msg 102, Level 15, State 1, Line 53
Incorrect syntax near ')'.

I don't see anything wrong with the code though
Post #1455627
Posted Wednesday, May 22, 2013 6:47 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 6:03 PM
Points: 3,590, Visits: 5,098
ChrisM@Work (4/30/2013)
todd.ayers (4/30/2013)
I am trying to create a random number generator that would populate a number between 8-48 to 9 different lanes with the only criteria being that each lane number cannot be within 5 of any lane within 3 spaces of it. So what I mean is if lane 4 has a number of 30 then lane 1-2-3 and lane 5-6-7 cannot be within 5 of lane 4's number. Hope this makes sense and any help would be sincerely appreciated....


No problem. Folks here love this sort of question! Here's a solution for openers:
;WITH SemiRandomNumbers AS (
SELECT Lane = 8 + ABS(CHECKSUM(NEWID()))%41
FROM syscolumns a
)
SELECT *
FROM (SELECT ID = 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) s
CROSS APPLY (
SELECT TOP 1 Lane1 = Lane FROM SemiRandomNumbers --ORDER BY (SELECT NULL)
) l1
CROSS APPLY (
SELECT TOP 1 Lane2 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5)
) l2
CROSS APPLY (
SELECT TOP 1 Lane3 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5)
AND NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5)
) l3
CROSS APPLY (
SELECT TOP 1 Lane4 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5)
AND NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5)
AND NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5)
) l4
CROSS APPLY (
SELECT TOP 1 Lane5 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5)
AND NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5)
AND NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5)
) l5
CROSS APPLY (
SELECT TOP 1 Lane6 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5)
AND NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5)
AND NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5)
) l6
CROSS APPLY (
SELECT TOP 1 Lane7 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5)
AND NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5)
AND NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5)
) l7
CROSS APPLY (
SELECT TOP 1 Lane8 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5)
AND NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5)
AND NOT (r.Lane BETWEEN l7.Lane7-5 AND l7.Lane7+5)
) l8
CROSS APPLY (
SELECT TOP 1 Lane9 = Lane FROM SemiRandomNumbers r
WHERE NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5)
AND NOT (r.Lane BETWEEN l7.Lane7-5 AND l7.Lane7+5)
AND NOT (r.Lane BETWEEN l8.Lane8-5 AND l8.Lane8+5)
) l9



That is a brilliant solution Chris!

And here I thought when I pulled up this thread I could make a shameless plug for my random numbers article (in my signature).

Thanks by the way for the nice plug in your signature for my rCTEs article.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1455775
Posted Thursday, May 23, 2013 12:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:33 AM
Points: 6,754, Visits: 12,854
todd.ayers (5/22/2013)
Well,

I have finally had a chance to test this and I keep getting an error message saying:

Msg 102, Level 15, State 1, Line 53
Incorrect syntax near ')'.

I don't see anything wrong with the code though


What version of SQL Server are you connecting to, Todd?


“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
Post #1455842
Posted Thursday, May 23, 2013 1:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:33 AM
Points: 6,754, Visits: 12,854
dwain.c (5/22/2013)
...
That is a brilliant solution Chris!

And here I thought when I pulled up this thread I could make a shameless plug for my random numbers article (in my signature).

Thanks by the way for the nice plug in your signature for my rCTEs article.


Thanks Dwain! I get a warm fuzzy feeling inside when I write a query which you can tell at a glance what it's supposed to do


“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
Post #1455844
Posted Thursday, May 23, 2013 5:02 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:56 PM
Points: 59, Visits: 120
I am running SQL Server 2012
Post #1455943
Posted Thursday, May 23, 2013 5:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:33 AM
Points: 6,754, Visits: 12,854
todd.ayers (5/22/2013)
Well,

I have finally had a chance to test this and I keep getting an error message saying:

Msg 102, Level 15, State 1, Line 53
Incorrect syntax near ')'.

I don't see anything wrong with the code though


I get the same error message if I comment out the last line of the query:

Msg 102, Level 15, State 1, Line 53
Incorrect syntax near ')'.



“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
Post #1455946
Posted Thursday, May 23, 2013 5:25 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:56 PM
Points: 59, Visits: 120
Well.... It's good to know that I'm not doing something wrong then.....


Btw what do you mean when you say "if I comment out the last line"?
Post #1455949
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse