gpt4 book ai didi

SQL Server : How to calculate different sums in a single query

转载 作者:行者123 更新时间:2023-12-05 00:42:16 27 4
gpt4 key购买 nike

一位 friend 建议我在这里发帖,因为我需要一些帮助!

数据库布局:

**salestable**
salesorder [primary, unique] (sales order number)
salesman (salesperson id)

**salesline**
salesorder [many sales line to single salestable relationship]
saleprice (line amount)
isaccessory (0 or 1)

我想在单个选择中,将销售价格作为每个销售员的总和,但也将销售价格与附件 = 1 相加。

分组和连接不是我所坚持的,而是我是否可以做双重和。

在我的脑海里它看起来像
...salesman,sum(amount) as totalsales,sum(amount where accessory=1) as accessorysales...

但显然那是行不通的!

提前致谢! (PS,我是新来的,温柔点...)

最佳答案

SELECT  salesman, SUM(amount), SUM(CASE WHEN accessory = 1 THEN amount ELSE 0 END)
FROM salestable t
JOIN salesorder o
ON o.salesorder = t.salesorder
GROUP BY
salesman

关于SQL Server : How to calculate different sums in a single query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2267526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com