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 123»»»

Update query will not run? Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 2:00 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:30 AM
Points: 62, Visits: 155
The following query gives me a random date between 1 and 28 days after the arrival date:

SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay

However when I use the update query below it only gives me between 1 and 2 days after the arrival date

USE Occupancy
Update B
Set DepartureDate = DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5 * L.LengthofStay, B.ArrivalDate)
FROM LengthOfStay L, Bookings B

Does anyone know why, and if so how do I change it?

Thanks

Wayne
Post #1421984
Posted Wednesday, February 20, 2013 3:38 AM


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 @ 5:45 PM
Points: 3,617, Visits: 5,237
wafw1971 (2/20/2013)
The following query gives me a random date between 1 and 28 days after the arrival date:

SELECT ArrivalDate, DATEADD(day, 1 + RAND(checksum(NEWID()))
* LengthOfStay.LengthofStay, ArrivalDate) AS DepartureDate
FROM Bookings, LengthOfStay

However when I use the update query below it only gives me between 1 and 2 days after the arrival date

USE Occupancy
Update B
Set DepartureDate = DATEADD(day, 1 + RAND(checksum(NEWID()))*1.5 * L.LengthofStay, B.ArrivalDate)
FROM LengthOfStay L, Bookings B

Does anyone know why, and if so how do I change it?

Thanks

Wayne


If you run this multiple times:

SELECT 1 + RAND(checksum(NEWID()))

You should see that the number returned is always between 1 and 2.

If you want it to return a number between 1 and 28, use this:

SELECT 1 + ABS(checksum(NEWID())) % 28




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 #1422013
Posted Wednesday, February 20, 2013 7:01 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:30 AM
Points: 62, Visits: 155
I have just been told I have done it completely wrong by my boss and the query above hasn't randomised anything on our data, I have got 48000 records with a 2 night stay, 48000 records for a 27 night stay etc.

What I need is 30% of the departure dates to be 2 days in length, 10% to be 3 Days and the rest to be randomised amongst 1,4 to 28.

Can anyone help?
Post #1422065
Posted Wednesday, February 20, 2013 7:09 AM


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 @ 5:45 PM
Points: 3,617, Visits: 5,237
wafw1971 (2/20/2013)
I have just been told I have done it completely wrong by my boss and the query above hasn't randomised anything on our data, I have got 48000 records with a 2 night stay, 48000 records for a 27 night stay etc.

What I need is 30% of the departure dates to be 2 days in length, 10% to be 3 Days and the rest to be randomised amongst 1,4 to 28.

Can anyone help?


Possibly. What you need is to generate random numbers based on a multinomial distribution. See the second article in my signature links (about random number generators in SQL) for a function that will do this.



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 #1422067
Posted Wednesday, February 20, 2013 7:20 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:30 AM
Points: 62, Visits: 155
Hi Dwain

Thanks for the link but to be honest its way above where I am in my training (I'm 4 weeks in) and to be honest that looks like a different language.

My boss said I should be able to find a case statement along the lines if a Random number genrte between 0 and 1 then between 0 and 0.3 is a 2 day stay and between 0.3 to 0.4 is a 3 day stay else 0.4 to 1 will all other days between 1 and 28.

But I don't know how to write the code.

Can you help or point me in the right direction.

Ta

Wayne
Post #1422076
Posted Wednesday, February 20, 2013 8:11 AM


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 @ 5:45 PM
Points: 3,617, Visits: 5,237
OK. Give me a few minutes and I'll code up an example.


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 #1422105
Posted Wednesday, February 20, 2013 8:27 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:30 AM
Points: 62, Visits: 155
Hi Dwain

I have tried to breakdown what I need below, I hope this helps.

Thanks

Wayne

Step 1

Arrival Date (Already generated) – 1.35 Million Times

Step 2

Randomise a number between 0 and 1

Step 3

Use the Randomised number produced above to create the script below

UPDATE BOOKINGS
SET DepartureDate
CASE WHEN RAND() Result = Between 0 and 0.3 = Departure Date will be 2 Nights Later
CASE WHEN RAND() Result = Between 0.3 and 0.4 = Departure Date will be 3 Nights Later
CASE WHEN RAND ()Result >0.4 = Departure Date will be either 1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 Nights Later
Post #1422117
Posted Wednesday, February 20, 2013 8:39 AM


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 @ 5:45 PM
Points: 3,617, Visits: 5,237
Here is how to generate a sample set of multinomially distributed random numbers. First, you need to create a TYPE and a FUNCTION by running this script:

CREATE TYPE Distribution AS TABLE (EventID INT, EventProb FLOAT, CumProb FLOAT) 
GO

CREATE FUNCTION dbo.RN_MULTINOMIAL
(@Multinomial Distribution READONLY, @URN FLOAT)
RETURNS INT --Cannot use WITH SCHEMABINDING
AS
BEGIN
RETURN
ISNULL(
( SELECT TOP 1 EventID
FROM @Multinomial
WHERE @URN < CumProb
ORDER BY CumProb)
-- Handle unlikely case where URN = exactly 1.0
,( SELECT MAX(EventID)
FROM @Multinomial))
END


Next, you need to set up your multinomial probability distribution table as follows:

DECLARE @MultinomialProbabilities Distribution

;WITH Tally (n) AS (
SELECT TOP 28 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM sys.all_columns)
INSERT INTO @MultinomialProbabilities
SELECT n
,CASE n WHEN 1 THEN .6/26. WHEN 2 THEN .3 WHEN 3 THEN .1 ELSE .6/26. END
,CASE n WHEN 1 THEN .6*1./26. WHEN 2 THEN .3+.6*1./26. WHEN 3 THEN .4+.6*1./26. ELSE .4+.6*(n-2)/26. END
FROM Tally

SELECT * FROM @MultinomialProbabilities


Note how the EventProb column shows .3 for event 2 and .1 for event 3. The rest are all the remaining probability (.6) divided by the number of events (26). The last column is the cumulative probability for all previous events (last row should show 1).

The hard part is now behind us.

Now, within the same SQL batch as the above, this test harness tests the generated random numbers so you can compare to the distribution's expected frequency.

DECLARE @TestNums INT = 1000
;WITH Tally (n) AS (
SELECT TOP (@TestNums) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM sys.all_columns a CROSS JOIN sys.all_columns b)
SELECT MNRN, CountOfMNRNs=COUNT(MNRN), ActualProbability=COUNT(MNRN)/(1.*@TestNums)
FROM (
SELECT MNRN=dbo.RN_MULTINOMIAL(@MultinomialProbabilities, URN)
FROM Tally
CROSS APPLY (SELECT URN=RAND(CHECKSUM(NEWID()))) a

) a
INNER JOIN @MultinomialProbabilities ON EventID=MNRN
GROUP BY MNRN


The key to generating a group of random numbers is the part I highlighted in bold/ This generates a sample set based on the value of @TestNums. The rest of it just groups by EventID and calculates the actual probability. This should center around 0.23 for all events except 2 and 3, which should be close to .3 and .1. The more numbers you generate, the closer they should be to the actual distribution.

Hope this helps.



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 #1422122
Posted Wednesday, February 20, 2013 8:48 AM


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 @ 5:45 PM
Points: 3,617, Visits: 5,237
Perhaps one additional clarification.

To generate a single multinomial random number, you do it like this:

SELECT MNRN=dbo.RN_MULTINOMIAL(@MultinomialProbabilities, URN)
FROM (SELECT URN=RAND(CHECKSUM(NEWID()))) a


Provided because I wasn't sure if when you referring to your skill level you meant in SQL or statistics.



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 #1422129
Posted Wednesday, February 20, 2013 9:23 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, May 7, 2013 3:30 AM
Points: 62, Visits: 155
Thank you for that I will try and get my head around it tomorrow.

Post #1422148
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse