Advertisement
elena1234

Switch Case in T-SQL

Dec 21st, 2021 (edited)
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.05 KB | None | 0 0
  1. --24.*Countries and Currency (Euro / Not Euro)
  2. SELECT CountryName, CountryCode, Currency =
  3. Case CurrencyCode
  4.       When 'EUR' THEN 'EURO'
  5.       ELSE 'NOT EURO'
  6. END
  7. FROM Countries
  8.  ORDER BY CountryName;
  9.  
  10. --
  11. SELECT [Name],
  12. [Part of the Day] =
  13. CASE
  14.     WHEN DATEPART(hour, [Start]) BETWEEN 0 AND 11 THEN 'Morning'
  15.     WHEN DATEPART(hour, [Start]) BETWEEN 12 AND 17 THEN 'Afternoon'
  16.     ELSE 'Evening'
  17. END,
  18. Duration =
  19. CASE
  20.     WHEN Duration <= 3 THEN 'Extra Short'
  21.     WHEN Duration <= 6 THEN 'Short'
  22.     WHEN Duration > 6 THEN 'Long'
  23.     ELSE 'Extra Long'
  24. END
  25. FROM Games
  26. ORDER BY [Name], Duration, [Part of the Day];
  27.  
  28. --9. Age Groups
  29. WITH CTE AS
  30. (
  31. SELECT
  32. (CASE
  33. WHEN Age BETWEEN 0 AND 10 THEN  '[0-10]'
  34. WHEN Age BETWEEN 11 AND 20 THEN '[11-20]'
  35. WHEN Age BETWEEN 21 AND 30 THEN '[21-30]'
  36. WHEN Age BETWEEN 31 AND 40  THEN '[31-40]'
  37. WHEN Age BETWEEN 41 AND 50  THEN '[41-50]'
  38. WHEN Age BETWEEN 51 AND 60  THEN '[51-60]'
  39. ELSE '[61+]'
  40. END) AS AgeGroup, Id
  41. FROM WizzardDeposits
  42. )
  43. SELECT AgeGroup, COUNT(Id) AS WizardCount
  44. FROM CTE
  45. GROUP BY AgeGroup;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement