如何解决SQL Server Management Studio-计算正确的总数
我在SQL Server表中有一列,其值如下:
myfunction() {
const iframe = document.querySelector('iframe');
if (iframe) iframe.parentNode.removeChild(iframe);
const i = document.createElement('iframe');
i.style.display = 'none';
i.src = this.study.DocumentUrl;
document.body.appendChild(i);
document.querySelector('iframe').contentWindow.focus();
document.querySelector('iframe').contentWindow.print();
}
如何编写查询以获取正确的值总计?
对于给定的示例,仅应将值用于ID Value Item#
-------------------
101 10 Apples
102 20 Apples
102-01 22 Apples
102-02 23 Apples
102-03 21 Apples
103 20 Apples
103-01 15 Apples
,101
,102-03
。
正确的总和应为103-01
。
我尝试使用like并在where子句中包含关键字。它没有帮助。 分组依据和Max将总数弄乱。
仅使用46
就可以得出所有错误的信息
解决方法
据我所知,您想按记录ID的前缀(前3个字符)对记录进行分组,然后对每组后缀最高的保持记录的值求和(如果有多个记录)。
您可以使用row_number()
,然后过滤并求和:
select sum(value) total_value
from (
select
t.*,row_number() over(partition by left(id,3) order by id desc) rn
from mytable t
) t
where rn = 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。