将 t-sql 中的行相乘
- 作者: 燕20115230
- 来源: 51数据库
- 2022-10-21
问题描述
我有下表
ID Number ---------------- 1 41.5 2 42.5 3 43.5 2 44.5 2 45.5 1 46.5 1 47.5
我需要编写一个查询,该查询将返回不同的 ID 和相应的 Number 列值相乘.对于给定的表结果应该是这样的
I need to write a query which will return distinct ID's and corresponding Number column values multiplied. For the given table result should be like this
ID Result ----------------- 1 41.5 * 46.5 * 47.5 2 42.5 * 44.5 * 45.5 3 etc...
(不使用游标)
推荐答案
SELECT Id, EXP(SUM(LOG(Number))) as Result FROM Scores GROUP BY id
这适用于正数,也可以乘以负数,您可以使用 ABS() 函数使用绝对(正)值,但最终结果将是正数而不是负数:
This will work for positive numbers, to multiply negative numbers as well you can use ABS() function to use absolute (positive) value but final result will be positive rather than negative number:
SELECT Id, EXP(SUM(LOG(ABS(Number)))) as Result FROM Scores GROUP BY id
<小时>
添加了测试脚本
DECLARE @data TABLE(id int, number float) INSERT INTO @data VALUES (1, 2.2), (1, 10), (2, -5.5), (2, 10) SELECT Id, EXP(SUM(LOG(ABS(Number)))) as Result FROM @data GROUP BY id
输出:
1 22 2 55
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
