est-il faisable en une seule requête de générer une grille, par exemple celle de l’Euromillion composée de 5 chiffres aléatoires différentes entre 1 et 50 et de 2 étoiles différentes et aléatoires entre 1 et 11 en une seule requête ? La réponses est…
OUI !
WITH
T50 AS (SELECT 1 AS N
UNION ALL
SELECT N + 1
FROM T50
WHERE N < 50),
T05 AS (SELECT N, ROW_NUMBER() OVER(ORDER BY NEWID()) AS ALEA
FROM T50),
T02 AS (SELECT N, ROW_NUMBER() OVER(ORDER BY NEWID()) AS ALEA
FROM T50
WHERE N < 12)
SELECT 'NUMERO' AS NATURE, N
FROM T05
WHERE ALEA <= 5
UNION ALL
SELECT 'ETOILE', N
FROM T02
WHERE ALEA <= 2
ORDER BY 1 DESC, 2;
T50 AS (SELECT 1 AS N
UNION ALL
SELECT N + 1
FROM T50
WHERE N < 50),
T05 AS (SELECT N, ROW_NUMBER() OVER(ORDER BY NEWID()) AS ALEA
FROM T50),
T02 AS (SELECT N, ROW_NUMBER() OVER(ORDER BY NEWID()) AS ALEA
FROM T50
WHERE N < 12)
SELECT 'NUMERO' AS NATURE, N
FROM T05
WHERE ALEA <= 5
UNION ALL
SELECT 'ETOILE', N
FROM T02
WHERE ALEA <= 2
ORDER BY 1 DESC, 2;
Et voila !
Une autre forme :
CASE_ETOILE AS
(
SELECT -12 AS N, NEWID() AS UUID
UNION ALL
SELECT N + 1, NEWID()
FROM CASE_ETOILE
WHERE N < 50
),
CHOIX_CASE AS
(
SELECT TOP 5 'CASE' AS NATURE, N
FROM CASE_ETOILE
WHERE N > 0
ORDER BY UUID
),
CHOIX_ETOILE AS
(
SELECT TOP 2 'ETOILE' AS NATURE, ABS(N) AS N
FROM CASE_ETOILE
WHERE N < 0
ORDER BY UUID
)
SELECT *
FROM CHOIX_CASE
UNION ALL
SELECT *
FROM CHOIX_ETOILE
ORDER BY 1, 2;
Je vous dirai si j’ai gagné les 190M€ de ce soir avec votre requête.