我有两个表CustomersOperations;我想将操作表中的数据WithdrawDeposit与客户表中的客户名称相加.

我已经创建了一个存储过程:

select 
    customer_id, customer_fullname, 
    amount_withdraw, amount_deposit, entry_date 
from 
    Operations b 
join 
    Customers a on b.customer_id2 = a.customer_id
where  
    customer_id = 19

这是我得到的结果:

enter image description here

但是我想要的结果应该是这样的

enter image description here

我需要做两个手术吗?

推荐答案

您可以使用UNION来执行此操作,从而有效地将两个查询连接到同一个结果集中:

select customer_id ,customer_fullname , amount_withdraw ,amount_deposit ,entry_date 
from Operations b join Customers a
on b.customer_id2 = a.customer_id
where customer_id = 19

UNION

SELECT customer_id ,customer_fullname , SUM(amount_withdraw) - SUM(amount_deposit) ,null, null 
FROM Operations b 
JOIN join Customers a on b.customer_id2 = a.customer_id
WHERE customer_id = 19
GROUP BY customer_id ,customer_fullname

UNION 

SELECT customer_id ,customer_fullname , SUM(amount_withdraw) ,SUM(amount_deposit) ,null 
FROM Operations b 
JOIN join Customers a on b.customer_id2 = a.customer_id
WHERE customer_id = 19
GROUP BY customer_id ,customer_fullname

这样做好吗...通常不会像这样的查询和聚合最好留给应用程序或报告层.上述查询的第一个问题是结果的顺序,第二个问题是您使用同一列返回不同的信息集.

我们可以根据Date列的可空性来确定顺序,但是语法看起来有点奇怪.我们不能对UNION个查询使用ORDER BY子句中的函数,因此要做到这一点,我们必须将汇总数据包装到子查询或CTE中

http://sqlfiddle.com/#!18/7022a/2

SELECT * FROM (
  select customer_id ,customer_fullname , amount_withdraw ,amount_deposit ,entry_date 
from Operations b 
JOIN Customers a on b.customer_id2 = a.customer_id
where customer_id = 19

UNION

SELECT customer_id ,customer_fullname , SUM(amount_withdraw) - SUM(amount_deposit) ,null, null 
FROM Operations b 
JOIN Customers a on b.customer_id2 = a.customer_id
WHERE customer_id = 19
GROUP BY customer_id ,customer_fullname

UNION 

SELECT customer_id ,customer_fullname , SUM(amount_withdraw) ,SUM(amount_deposit) ,null 
FROM Operations b 
JOIN Customers a on b.customer_id2 = a.customer_id
WHERE customer_id = 19
GROUP BY customer_id ,customer_fullname
) RollupData
ORDER BY ISNULL(entry_date,'2099-12-31'), ISNULL(amount_deposit, 999999999)

排序可以通过添加任意列而不是依赖于entry_date的空性以不同的方式修复,这使得它变得更加明显,如果您直接使用结果,我们可以从最终输出中删除该列,但我们经常会保留它,以便可以在报告层中重新确认排序.下面显示了如何删除它:

http://sqlfiddle.com/#!18/7022a/5

SELECT customer_id ,customer_fullname , amount_withdraw ,amount_deposit ,entry_date 
FROM
(
    select 1 as [order], customer_id ,customer_fullname , amount_withdraw ,amount_deposit ,entry_date 
    from Operations b 
    JOIN Customers a on b.customer_id2 = a.customer_id
    where customer_id = 19

    UNION

    SELECT 2 as [order], customer_id ,customer_fullname , SUM(amount_withdraw) - SUM(amount_deposit) ,null, null 
    FROM Operations b 
    JOIN Customers a on b.customer_id2 = a.customer_id
    WHERE customer_id = 19
    GROUP BY customer_id ,customer_fullname

    UNION 

    SELECT 3 as [order], customer_id ,customer_fullname , SUM(amount_withdraw) ,SUM(amount_deposit) ,null 
    FROM Operations b 
    JOIN Customers a on b.customer_id2 = a.customer_id
    WHERE customer_id = 19
    GROUP BY customer_id ,customer_fullname
  
) RollupData
ORDER BY [order]
customer_id customer_fullname amount_withdraw amount_deposit entry_date
19 Ali Ahmed Omar 0 32000 2023-11-05
19 Ali Ahmed Omar 0 65000 2023-09-04
19 Ali Ahmed Omar 0 78200 2023-10-25
19 Ali Ahmed Omar 32000 0 2023-06-02
19 Ali Ahmed Omar 56000 0 2023-01-02
19 Ali Ahmed Omar 64000 0 2023-04-08
19 Ali Ahmed Omar -23200 (null) (null)
19 Ali Ahmed Omar 152000 175200 (null)

更标准的解决方案是将聚合移动到其他列,您也可以使用窗口查询,但这些查询将具有与您的请求不同的布局.

Sql相关问答推荐

Postgresql -如何计算刷卡和刷卡时间

如何将资源密集型自连接转换为更快的查询?

如何从多行数据中获取一行合并数据?

如何在幂函数中正确使用Power()和Exp()

为表中每个缺少的引用创建新行

从原始表列中经过JSON字符串化的对象数组构建视图

如何查询jsonb列是一个对象数组?

在Netezza SQL中将字符DataType转换为整型DataType

MS Access问题查询中的自定义字段

用VB.NET在Dapper中实现MS Access数据库顺序透视

SQL仅返回第一个字母在A-Z之间的值

postgres中的条件索引和触发器

删除对 JSON 数据的未解析引用的 SQL71502 警告

具有分组条件的不同计数 (DAX)

当我返回 sql 列时,有没有办法只反转数字? ( hebrew )

验证某个日期前后的连续代码

SQL ORACLE - 查找连续天数

奇怪的甲骨文行为

如何使用子查询锁定此查询中的选定行?

在时态表和非时态表之间使用 EXCEPT 的 SQL 子查询给出表达式错误数