我有下表 + v+ e) {! j; L/ o ID Number ---------------- .5 2 422 .5 3 4333 .5 2 44444.55 2 4555.5 1 466 .5 5 1 477 .5我需要编写一个查询,它将返回不同的查询ID和对应的Number列值相乘。给定的表结果应该是这样的 5 h. A' y) e0 {& T) r" x BID Result ----------------- .5 * 46.5 * 47.5 2 .5 * 44.5 * 45.5 3 3 etc...(不使用游标)/ u3 G* X+ q `
9 X! y* O% \0 g) W% w3 m; Y- [解决方案: 8 z1 q! A4 I- y/ g6 {+ n6 s SELECT Id,EXP(SUM(LOG(Number))) as ResultFROM Scores GROUP BY id这将适用于正数、负数或负数ABS()函数使用绝对(正)值,但最终结果将是正数,而不是负数: ! k5 n4 P8 m" N& \SELECT Id,EXP(SUM(LOG(ABS(Number)))) as ResultFROM Scores GROUP BY id编辑:添加测试脚本2 J& i' h2 ^& @8 {# {* O5 f9 _: J0 K
DECLARE @data TABLE(id int,number float)INSERT INTO @data VALUES(1,2.(1,10),(2,-5.5),(2,10)SELECT Id,EXP(SUM(LOG(ABS(Number)))) as Result FROM @data GROUP BY id输出: