我使用以下SQL从表变量返回单一颜色。我必须这样做,而不是使用order by newid()
,因为此查询将作为列值存在于另一个查询中,并且我希望每行都有不同的值。
为异常长的表变量道歉,它有746种颜色!
declare @socks table
(
RowID int identity(1,1),
Colour nvarchar(70)
);
insert into @socks(Colour)
values('Air Force blue'), ('Alice blue'), ('Alizarin crimson'), ('Almond'), ('Amaranth'), ('Amber'), ('American rose'), ('Amethyst'), ('Android Green'), ('Anti-flash white'), ('Antique brass'), ('Antique fuchsia'), ('Antique white'), ('Ao'), ('Apple green'), ('Apricot'), ('Aqua'), ('Aquamarine'), ('Army green'), ('Arylide yellow'), ('Ash grey'), ('Asparagus'), ('Atomic tangerine'), ('Auburn'), ('Aureolin'), ('AuroMetalSaurus'), ('Awesome'), ('Azure'), ('Azure mist/web'), ('Baby blue'), ('Baby blue eyes'), ('Baby pink'), ('Ball Blue'), ('Banana Mania'), ('Banana yellow'), ('Battleship grey'), ('Bazaar'), ('Beau blue'), ('Beaver'), ('Beige'), ('Bisque'), ('Bistre'), ('Bittersweet'), ('Black'), ('Blanched Almond'), ('Bleu de France'), ('Blizzard Blue'), ('Blond'), ('Blue'), ('Blue Bell'), ('Blue Gray'), ('Blue green'), ('Blue purple'), ('Blue violet'), ('Blush'), ('Bole'), ('Bondi blue'), ('Bone'), ('Boston University Red'), ('Bottle green'), ('Boysenberry'), ('Brandeis blue'), ('Brass'), ('Brick red'), ('Bright cerulean'), ('Bright green'), ('Bright lavender'), ('Bright maroon'), ('Bright pink'), ('Bright turquoise'), ('Bright ube'), ('Brilliant lavender'), ('Brilliant rose'), ('Brink pink'), ('British racing green'), ('Bronze'), ('Brown'), ('Bubble gum'), ('Bubbles'), ('Buff'), ('Bulgarian rose'), ('Burgundy'), ('Burlywood'), ('Burnt orange'), ('Burnt sienna'), ('Burnt umber'), ('Byzantine'), ('Byzantium'), ('CG Blue'), ('CG Red'), ('Cadet'), ('Cadet blue'), ('Cadet grey'), ('Cadmium green'), ('Cadmium orange'), ('Cadmium red'), ('Cadmium yellow'), ('Café au lait'), ('Café noir'), ('Cal Poly Pomona green'), ('Cambridge Blue'), ('Camel'), ('Camouflage green'), ('Canary'), ('Canary yellow'), ('Candy apple red'), ('Candy pink'), ('Capri'), ('Caput mortuum'), ('Cardinal'), ('Caribbean green'), ('Carmine'), ('Carmine pink'), ('Carmine red'), ('Carnation pink'), ('Carnelian'), ('Carolina blue'), ('Carrot orange'), ('Celadon'), ('Celeste'), ('Celestial blue'), ('Cerise'), ('Cerise pink'), ('Cerulean'), ('Cerulean blue'), ('Chamoisee'), ('Champagne'), ('Charcoal'), ('Chartreuse'), ('Cherry'), ('Cherry blossom pink'), ('Chestnut'), ('Chocolate'), ('Chrome yellow'), ('Cinereous'), ('Cinnabar'), ('Cinnamon'), ('Citrine'), ('Classic rose'), ('Cobalt'), ('Cocoa brown'), ('Coffee'), ('Columbia blue'), ('Cool black'), ('Cool grey'), ('Copper'), ('Copper rose'), ('Coquelicot'), ('Coral'), ('Coral pink'), ('Coral red'), ('Cordovan'), ('Corn'), ('Cornell Red'), ('Cornflower'), ('Cornflower blue'), ('Cornsilk'), ('Cosmic latte'), ('Cotton candy'), ('Cream'), ('Crimson'), ('Crimson Red'), ('Crimson glory'), ('Cyan'), ('Daffodil'), ('Dandelion'), ('Dark blue'), ('Dark brown'), ('Dark byzantium'), ('Dark candy apple red'), ('Dark cerulean'), ('Dark chestnut'), ('Dark coral'), ('Dark cyan'), ('Dark electric blue'), ('Dark goldenrod'), ('Dark gray'), ('Dark green'), ('Dark jungle green'), ('Dark khaki'), ('Dark lava'), ('Dark lavender'), ('Dark magenta'), ('Dark midnight blue'), ('Dark olive green'), ('Dark orange'), ('Dark orchid'), ('Dark pastel blue'), ('Dark pastel green'), ('Dark pastel purple'), ('Dark pastel red'), ('Dark pink'), ('Dark powder blue'), ('Dark raspberry'), ('Dark red'), ('Dark salmon'), ('Dark scarlet'), ('Dark sea green'), ('Dark sienna'), ('Dark slate blue'), ('Dark slate gray'), ('Dark spring green'), ('Dark tan'), ('Dark tangerine'), ('Dark taupe'), ('Dark terra cotta'), ('Dark turquoise'), ('Dark violet'), ('Dartmouth green'), ('Davy grey'), ('Debian red'), ('Deep carmine'), ('Deep carmine pink'), ('Deep carrot orange'), ('Deep cerise'), ('Deep champagne'), ('Deep chestnut'), ('Deep coffee'), ('Deep fuchsia'), ('Deep jungle green'), ('Deep lilac'), ('Deep magenta'), ('Deep peach'), ('Deep pink'), ('Deep saffron'), ('Deep sky blue'), ('Denim'), ('Desert'), ('Desert sand'), ('Dim gray'), ('Dodger blue'), ('Dogwood rose'), ('Dollar bill'), ('Drab'), ('Duke blue'), ('Earth yellow'), ('Ecru'), ('Eggplant'), ('Eggshell'), ('Egyptian blue'), ('Electric blue'), ('Electric crimson'), ('Electric cyan'), ('Electric green'), ('Electric indigo'), ('Electric lavender'), ('Electric lime'), ('Electric purple'), ('Electric ultramarine'), ('Electric violet'), ('Electric yellow'), ('Emerald'), ('Eton blue'), ('Fallow'), ('Falu red'), ('Famous'), ('Fandango'), ('Fashion fuchsia'), ('Fawn'), ('Feldgrau'), ('Fern'), ('Fern green'), ('Ferrari Red'), ('Field drab'), ('Fire engine red'), ('Firebrick'), ('Flame'), ('Flamingo pink'), ('Flavescent'), ('Flax'), ('Floral white'), ('Fluorescent orange'), ('Fluorescent pink'), ('Fluorescent yellow'), ('Folly'), ('Forest green'), ('French beige'), ('French blue'), ('French lilac'), ('French rose'), ('Fuchsia'), ('Fuchsia pink'), ('Fulvous'), ('Fuzzy Wuzzy'), ('Gainsboro'), ('Gamboge'), ('Ghost white'), ('Ginger'), ('Glaucous'), ('Glitter'), ('Gold'), ('Golden brown'), ('Golden poppy'), ('Golden yellow'), ('Goldenrod'), ('Granny Smith Apple'), ('Gray'), ('Gray asparagus'), ('Green'), ('Green Blue'), ('Green yellow'), ('Grullo'), ('Guppie green'), ('Halayà úbe'), ('Han blue'), ('Han purple'), ('Hansa yellow'), ('Harlequin'), ('Harvard crimson'), ('Harvest Gold'), ('Heart Gold'), ('Heliotrope'), ('Hollywood cerise'), ('Honeydew'), ('Hooker green'), ('Hot magenta'), ('Hot pink'), ('Hunter green'), ('Icterine'), ('Inchworm'), ('India green'), ('Indian red'), ('Indian yellow'), ('Indigo'), ('International Klein Blue'), ('International orange'), ('Iris'), ('Isabelline'), ('Islamic green'), ('Ivory'), ('Jade'), ('Jasmine'), ('Jasper'), ('Jazzberry jam'), ('Jonquil'), ('June bud'), ('Jungle green'), ('KU Crimson'), ('Kelly green'), ('Khaki'), ('La Salle Green'), ('Languid lavender'), ('Lapis lazuli'), ('Laser Lemon'), ('Laurel green'), ('Lava'), ('Lavender'), ('Lavender blue'), ('Lavender blush'), ('Lavender gray'), ('Lavender indigo'), ('Lavender magenta'), ('Lavender mist'), ('Lavender pink'), ('Lavender purple'), ('Lavender rose'), ('Lawn green'), ('Lemon'), ('Lemon Yellow'), ('Lemon chiffon'), ('Lemon lime'), ('Light Crimson'), ('Light Thulian pink'), ('Light apricot'), ('Light blue'), ('Light brown'), ('Light carmine pink'), ('Light coral'), ('Light cornflower blue'), ('Light cyan'), ('Light fuchsia pink'), ('Light goldenrod yellow'), ('Light gray'), ('Light green'), ('Light khaki'), ('Light pastel purple'), ('Light pink'), ('Light salmon'), ('Light salmon pink'), ('Light sea green'), ('Light sky blue'), ('Light slate gray'), ('Light taupe'), ('Light yellow'), ('Lilac'), ('Lime'), ('Lime green'), ('Lincoln green'), ('Linen'), ('Lion'), ('Liver'), ('Lust'), ('MSU Green'), ('Macaroni and Cheese'), ('Magenta'), ('Magic mint'), ('Magnolia'), ('Mahogany'), ('Maize'), ('Majorelle Blue'), ('Malachite'), ('Manatee'), ('Mango Tango'), ('Mantis'), ('Maroon'), ('Mauve'), ('Mauve taupe'), ('Mauvelous'), ('Maya blue'), ('Meat brown'), ('Medium Persian blue'), ('Medium aquamarine'), ('Medium blue'), ('Medium candy apple red'), ('Medium carmine'), ('Medium champagne'), ('Medium electric blue'), ('Medium jungle green'), ('Medium lavender magenta'), ('Medium orchid'), ('Medium purple'), ('Medium red violet'), ('Medium sea green'), ('Medium slate blue'), ('Medium spring bud'), ('Medium spring green'), ('Medium taupe'), ('Medium teal blue'), ('Medium turquoise'), ('Medium violet red'), ('Melon'), ('Midnight blue'), ('Midnight green'), ('Mikado yellow'), ('Mint'), ('Mint cream'), ('Mint green'), ('Misty rose'), ('Moccasin'), ('Mode beige'), ('Moonstone blue'), ('Mordant red 19'), ('Moss green'), ('Mountain Meadow'), ('Mountbatten pink'), ('Mulberry'), ('Munsell'), ('Mustard'), ('Myrtle'), ('Nadeshiko pink'), ('Napier green'), ('Naples yellow'), ('Navajo white'), ('Navy blue'), ('Neon Carrot'), ('Neon fuchsia'), ('Neon green'), ('Non-photo blue'), ('North Texas Green'), ('Ocean Boat Blue'), ('Ochre'), ('Office green'), ('Old gold'), ('Old lace'), ('Old lavender'), ('Old mauve'), ('Old rose'), ('Olive'), ('Olive Drab'), ('Olive Green'), ('Olivine'), ('Onyx'), ('Opera mauve'), ('Orange'), ('Orange Yellow'), ('Orange peel'), ('Orange red'), ('Orchid'), ('Otter brown'), ('Outer Space'), ('Outrageous Orange'), ('Oxford Blue'), ('Pacific Blue'), ('Pakistan green'), ('Palatinate blue'), ('Palatinate purple'), ('Pale aqua'), ('Pale blue'), ('Pale brown'), ('Pale carmine'), ('Pale cerulean'), ('Pale chestnut'), ('Pale copper'), ('Pale cornflower blue'), ('Pale gold'), ('Pale goldenrod'), ('Pale green'), ('Pale lavender'), ('Pale magenta'), ('Pale pink'), ('Pale plum'), ('Pale red violet'), ('Pale robin egg blue'), ('Pale silver'), ('Pale spring bud'), ('Pale taupe'), ('Pale violet red'), ('Pansy purple'), ('Papaya whip'), ('Paris Green'), ('Pastel blue'), ('Pastel brown'), ('Pastel gray'), ('Pastel green'), ('Pastel magenta'), ('Pastel orange'), ('Pastel pink'), ('Pastel purple'), ('Pastel red'), ('Pastel violet'), ('Pastel yellow'), ('Patriarch'), ('Payne grey'), ('Peach'), ('Peach puff'), ('Peach yellow'), ('Pear'), ('Pearl'), ('Pearl Aqua'), ('Peridot'), ('Periwinkle'), ('Persian blue'), ('Persian indigo'), ('Persian orange'), ('Persian pink'), ('Persian plum'), ('Persian red'), ('Persian rose'), ('Phlox'), ('Phthalo blue'), ('Phthalo green'), ('Piggy pink'), ('Pine green'), ('Pink'), ('Pink Flamingo'), ('Pink Sherbet'), ('Pink pearl'), ('Pistachio'), ('Platinum'), ('Plum'), ('Portland Orange'), ('Powder blue'), ('Princeton orange'), ('Prussian blue'), ('Psychedelic purple'), ('Puce'), ('Pumpkin'), ('Purple'), ('Purple Heart'), ('Purple Mountain''s Majesty'), ('Purple mountain majesty'), ('Purple pizzazz'), ('Purple taupe'), ('Rackley'), ('Radical Red'), ('Raspberry'), ('Raspberry glace'), ('Raspberry pink'), ('Raspberry rose'), ('Raw Sienna'), ('Razzle dazzle rose'), ('Razzmatazz'), ('Red'), ('Red Orange'), ('Red brown'), ('Red violet'), ('Rich black'), ('Rich carmine'), ('Rich electric blue'), ('Rich lilac'), ('Rich maroon'), ('Rifle green'), ('Robin''s Egg Blue'), ('Rose'), ('Rose bonbon'), ('Rose ebony'), ('Rose gold'), ('Rose madder'), ('Rose pink'), ('Rose quartz'), ('Rose taupe'), ('Rose vale'), ('Rosewood'), ('Rosso corsa'), ('Rosy brown'), ('Royal azure'), ('Royal blue'), ('Royal fuchsia'), ('Royal purple'), ('Ruby'), ('Ruddy'), ('Ruddy brown'), ('Ruddy pink'), ('Rufous'), ('Russet'), ('Rust'), ('Sacramento State green'), ('Saddle brown'), ('Safety orange'), ('Saffron'), ('Saint Patrick Blue'), ('Salmon'), ('Salmon pink'), ('Sand'), ('Sand dune'), ('Sandstorm'), ('Sandy brown'), ('Sandy taupe'), ('Sap green'), ('Sapphire'), ('Satin sheen gold'), ('Scarlet'), ('School bus yellow'), ('Screamin Green'), ('Sea blue'), ('Sea green'), ('Seal brown'), ('Seashell'), ('Selective yellow'), ('Sepia'), ('Shadow'), ('Shamrock'), ('Shamrock green'), ('Shocking pink'), ('Sienna'), ('Silver'), ('Sinopia'), ('Skobeloff'), ('Sky blue'), ('Sky magenta'), ('Slate blue'), ('Slate gray'), ('Smalt'), ('Smokey topaz'), ('Smoky black'), ('Snow'), ('Spiro Disco Ball'), ('Spring bud'), ('Spring green'), ('Steel blue'), ('Stil de grain yellow'), ('Stizza'), ('Stormcloud'), ('Straw'), ('Sunglow'), ('Sunset'), ('Sunset Orange'), ('Tan'), ('Tangelo'), ('Tangerine'), ('Tangerine yellow'), ('Taupe'), ('Taupe gray'), ('Tawny'), ('Tea green'), ('Tea rose'), ('Teal'), ('Teal blue'), ('Teal green'), ('Terra cotta'), ('Thistle'), ('Thulian pink'), ('Tickle Me Pink'), ('Tiffany Blue'), ('Tiger eye'), ('Timberwolf'), ('Titanium yellow'), ('Tomato'), ('Toolbox'), ('Topaz'), ('Tractor red'), ('Trolley Grey'), ('Tropical rain forest'), ('True Blue'), ('Tufts Blue'), ('Tumbleweed'), ('Turkish rose'), ('Turquoise'), ('Turquoise blue'), ('Turquoise green'), ('Tuscan red'), ('Twilight lavender'), ('Tyrian purple'), ('UA blue'), ('UA red'), ('UCLA Blue'), ('UCLA Gold'), ('UFO Green'), ('UP Forest green'), ('UP Maroon'), ('USC Cardinal'), ('USC Gold'), ('Ube'), ('Ultra pink'), ('Ultramarine'), ('Ultramarine blue'), ('Umber'), ('United Nations blue'), ('University of California Gold'), ('Unmellow Yellow'), ('Upsdell red'), ('Urobilin'), ('Utah Crimson'), ('Vanilla'), ('Vegas gold'), ('Venetian red'), ('Verdigris'), ('Vermilion'), ('Veronica'), ('Violet'), ('Violet Blue'), ('Violet Red'), ('Viridian'), ('Vivid auburn'), ('Vivid burgundy'), ('Vivid cerise'), ('Vivid tangerine'), ('Vivid violet'), ('Warm black'), ('Waterspout'), ('Wenge'), ('Wheat'), ('White'), ('White smoke'), ('Wild Strawberry'), ('Wild Watermelon'), ('Wild blue yonder'), ('Wine'), ('Wisteria'), ('Xanadu'), ('Yale Blue'), ('Yellow'), ('Yellow Orange'), ('Yellow green'), ('Zaffre'), ('Zinnwaldite brown');
select s.Colour
from @socks s
where s.RowID = floor(cast(746 * rand(checksum(newid())) + 1 as int))
随机数元素,当单独运行时,返回一个有效范围为1 - 746的数字。但是当它用于过滤查询时,它将在大约25%的时间内没有返回任何行 - 也许更多。
我也试过......
select top 1 s.Colour
from @socks s
where s.RowID between floor(cast(746 * rand(checksum(newid())) + 1 as int))
and floor(cast(746 * rand(checksum(newid())) + 1 as int))
但这只会从表变量中返回值RowID
介于1到99之间。
关于我可以去哪里的任何建议?
修改
之前不包括这部分的道歉 - 我错误地认为解决上述问题也将解决主要问题。 我需要在另一个查询中使用它,而不是使用连接,而是作为子查询,为每行返回不同的值。例如:
Select 'Socks' as [Clothes],
(select s.Colour from @socks s where s.RowID = floor(cast(746 * rand(checksum(newid())) + 1 as int))) as [Colour],
U.Name,
From do.Users u
这是我尝试做的简化版本 - 就像我说的那样,我认为解决我的随机选择问题会在主查询中解决它。抱歉格式不佳,我在手机上!
答案 0 :(得分:1)
将为每一行评估用于生成随机ID的函数。基本上,您将每行的RowId
与新的随机数进行比较。对于746行和可能的id,那是(745/746)^ 746 = 37%没有结果的可能性。
您需要生成一次id,并对所有行使用相同的值:
declare @randomId int = floor(cast(746 * rand(checksum(newid())) + 1 as int))
select s.Colour from @socks s where s.RowID = @randomId
修改强>
根据评论,此解决方案不能在需要查询的上下文中使用。
实际上这个问题有一个非常简单的解决方案。来自问题的查询需要表扫描+过滤来获得结果,这就是为每行计算函数的原因。
我们可以通过将RowId
作为主键轻松地使其成为索引搜索。在这种情况下,查询将只执行一次索引搜索,因此该函数将仅被评估一次。它也会表现得更好。
declare @socks table
(
RowID int identity(1,1) primary key,
Colour nvarchar(70)
);
select s.Colour
from @socks s
where s.RowID = floor(cast(746 * rand(checksum(newid())) + 1 as int)) -- no changes here