经典SQL题 1/25/50/100美分,多少种可能拼凑成2美元

如题,这类问题就是看思路,很多面试的时候都会遇到,思路到了,面试官也就默许了。也许不是很复杂,但确实实在。

1
With t1 as( 2 select 200 n union all 3 select n - 25 from t1 where n-25>=0 -- 1 CENT 4 ), 5 t2 as( 6 select 200 n union all 7 select n - 25 from t2 where n-25>=0 -- 25 CENT 8 ), 9 t3 as( 10 select 200 n union all 11 select n - 50 from t3 where n-50>=0 -- 50 CENT 12 ), 13 t4 as( 14 select 200 n union all 15 select n - 100 from t4 where n-100>=0 -- 100 CENT 16 ) 17 select t1.n/1 [1美分], t2.n/25 [25美分], t3.n/50 [50美分], t4.n/100 [100美分] from t1,t2,t3,t4 18 where t1.n + t2.n + t3.n + t4.n = 200 19 order by t1.n desc 20 option(maxrecursion 0)

希望我们能多多讨论研讨这些问题,提高自我sql水平,共同努力加油吧!
原文地址:https://www.cnblogs.com/life512/p/5333953.html