|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 4:35 PM
Points: 6,369,
Visits: 8,232
|
|
Great article Jeff.
One thing to mention... in the article, you pointed out that you cannot directly add / subtract a number to the new DATE data time. This applies to all of the new date data types: DATETIME2, DATETIMEOFFSET and TIME as well as DATE.
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 5:59 AM
Points: 18,
Visits: 180
|
|
Jeff, thanks for the great articles (part 1 and part 2 and look forward to part3).
Is there a way to pick a random value (like a color) from a subquery? here is what I tried but getting the same value on every run:
SELECT TOP (100) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as ID, --Sequential number from 1 to .. (SELECT top 1 color FROM ( VALUES (0,'Red'),(1,'Green'),(2,'Yellow') ) colors(id,color) ORDER BY NEWID()) as RandomColor, FROM sys.all_columns ac1 CROSS JOIN sys.all_columns ac2
but if I run this piece alone, I am getting different colors: SELECT top 1 color FROM ( VALUES (0,'Red'),(1,'Green'),(2,'Yellow') ) colors(id,color) ORDER BY NEWID()
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
|
|
No... not from a sub-query. At least I haven't figured out a way to do it with a TOP 1/ORDER BY like that.
You could do it using a CASE function based on ABS(CHECKSUM(NEWID()))%3 but that will actually come up with 4 values because, since NEWID() isn't deterministic, the CASE function will recalculate each and every WHEN even if you use CASE ABS(CHECKSUM(NEWID()))%3. You could then change the formula to ABS(CHECKSUM(NEWID()))%2 and use ELSE but that will give you an uneven distribution.
So, the only thing to do is to gen the numbers that control the color separately and then CASE that number. Here's one way of doing that in a single query.
DROP TABLE #MyHead; WITH cteRandom AS ( --=== Generate the random number first SELECT TOP (1000) ID = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), RandomColor# = ABS(CHECKSUM(NEWID()))%3 FROM sys.all_columns ac1 CROSS JOIN sys.all_columns ac2 ) --=== Now, pick the color according to the generated number SELECT ID, RandomColor = CASE RandomColor# WHEN 1 THEN 'Red' WHEN 2 THEN 'Green' ELSE 'Yellow' END INTO #MyHead FROM cteRandom ; --===== Show the distribution SELECT RandomColor, COUNT(*) FROM #MyHead GROUP BY RandomColor ORDER BY RandomColor
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
|
|
Here's another way... same idea, though.
DROP TABLE #MyHead;
SELECT TOP (1000) ID = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), RandomColor = CASE RandomColor# WHEN 1 THEN 'Red' WHEN 2 THEN 'Green' ELSE 'Yellow' END INTO #MyHead FROM sys.all_columns ac1 CROSS JOIN sys.all_columns ac2 CROSS APPLY (SELECT ABS(CHECKSUM(NEWID()))%3) ca (RandomColor#) ; --===== Show the distribution SELECT RandomColor, COUNT(*) FROM #MyHead GROUP BY RandomColor ORDER BY RandomColor ;
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 5:59 AM
Points: 18,
Visits: 180
|
|
Jeff Moden (12/16/2012) Here's another way... same idea, though.
awesome, it will work! thanks for the quick response, Jeff! and I am going to use this opportunity to say BIG THANK YOU for all your articles and knowledge you shared with us!
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 32,906,
Visits: 26,792
|
|
Thanks, Boriskey I really appreciate the feedback. I aim to please... I sometimes miss but I'm always aiming.
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|