This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to print “Up” if number is multiples of three and print “Down” if number is multiples of five. If number is multiples of three and five both, print “UpDown”
Expected Output: For top 100 numbers
1 2 3 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
number Result ----------- ------ 1 1 2 2 3 Up 4 4 5 Down 6 Up 7 7 8 8 9 Up 10 Down 11 11 12 Up 13 13 14 14 15 UpDown 16 16 17 17 18 Up 19 19 20 Down 21 Up 22 22 23 23 24 Up 25 Down 26 26 27 Up 28 28 29 29 30 UpDown 31 31 32 32 33 Up 34 34 35 Down 36 Up 37 37 38 38 39 Up 40 Down 41 41 42 Up 43 43 44 44 45 UpDown 46 46 47 47 48 Up 49 49 50 Down 51 Up 52 52 53 53 54 Up 55 Down 56 56 57 Up 58 58 59 59 60 UpDown 61 61 62 62 63 Up 64 64 65 Down 66 Up 67 67 68 68 69 Up 70 Down 71 71 72 Up 73 73 74 74 75 UpDown 76 76 77 77 78 Up 79 79 80 Down 81 Up 82 82 83 83 84 Up 85 Down 86 86 87 Up 88 88 89 89 90 UpDown 91 91 92 92 93 Up 94 94 95 Down 96 Up 97 97 98 98 99 Up 100 Down |
Solution:
1 2 3 4 5 6 7 8 9 |
SELECT DISTINCT number, CASE WHEN number % 15 = 0 THEN 'UpDown' WHEN number % 3 = 0 THEN 'Up' WHEN number % 5 = 0 THEN 'Down' ELSE CAST(number AS VARCHAR(3)) END Result FROM master..spt_values WHERE number BETWEEN 1 AND 100 ORDER BY number |
Please try the different solution for this puzzle and share it via comment...