gpt4 book ai didi

sql - 多个语句之间

转载 作者:行者123 更新时间:2023-12-04 13:14:07 26 4
gpt4 key购买 nike

我是一名正在学习 SQL 类(class)的学生开发人员。我目前正在尝试从表中检索几组行的总和。我对 SQL 开发很陌生,我不太确定以下场景中的问题是什么。我有一个大表,我想从中选择特定的行,然后添加余额列。

以下不会在结果中显示任何数据,我不太确定为什么。我有一种预感,每个表只能使用一次 between 语句,但也许我错了?

Select Description, SUM(Balance) AS Total_Balance
FROM Chart_Of_Accounts
WHERE (Account BETWEEN ('400401') AND ('400407'))
AND (Account BETWEEN ('440094') AND ('440100'))
AND (Account BETWEEN ('450094') AND ('450100'))
GROUP BY Description, Balance

编辑 10.1.2012 根据(Richard aka Cyber​​kiwi)的要求

我有什么
Set 1
|Account |Description |Balance
|4004xx |Red Wine |$2361.23
|4004xx |White Wine |$3620.23
|4004xx |Rice Wine |$1223.23

Set 2
|Account |Description |Balance
|4400xx |Red Wine |$4361.23
|4400xx |White Wine |$3260.23
|4400xx |Rice Wine |$223.23

Set 3
|Account |Description |Balance
|4500xx |Red Wine |$1361.23
|4500xx |White Wine |$1620.23
|4500xx |Rice Wine |$1223.23

我正在努力实现的结果
Total
|Description |Total_Balance
|Red Wine |$8083.69
|White Wine |$8500.69
|Rice Wine |$2669.69

感谢您阅读我的帖子!我也愿意接受 SQL 开发中的任何建议,非常感谢任何反馈。

最佳答案

使用 OR 代替 AND!
你想表达的是,在A和B之间给我结果,or它在C和D之间等等。

  SELECT Description, SUM(Balance) AS Total_Balance
FROM Chart_Of_Accounts
WHERE (Account BETWEEN '400401' AND '400407')
OR (Account BETWEEN '440094' AND '440100')
OR (Account BETWEEN '450094' AND '450100')
GROUP BY Description

我去掉了无关的括号,但你甚至可以去掉剩下的括号,因为 OR 在 AND 之后得到处理。见: SQL Server Operator Precedence

关于sql - 多个语句之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666461/

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