SQL如何使用JOIN创建小计

Posted

技术标签:

【中文标题】SQL如何使用JOIN创建小计【英文标题】:SQL how to create subtotal using JOIN 【发布时间】:2013-11-19 22:56:04 【问题描述】:

我想做一个类似这样的查询:

Cash x
Inventory x
TOTAL CURRENT ASSET x
PPE x
TOTAL ASSET x
LIST OF LIABILTIES X
LIST OF OWNER'S EQUITY X
TOTAL LIABILITIES AND OWNER'S EQUITY X

X= 数量

我已经设法使用 UNION ALL 获得小计和总计,但它显示在最底部而不是中间。

有没有办法通过递归 JOIN 来做到这一点?因为所有数据都来自一张表。

这是我目前所拥有的:

SQL> SELECT Acctname AS "Account Name", debit, credit
  2  FROM chart_of_accounts_ngo_p
  3  WHERE acctno BETWEEN 1010 AND 3200
  4  UNION ALL
  5  SELECT 'TOTAL CURRENT ASSETS', SUM(debit) - SUM(credit), null
  6  FROM chart_of_accounts_ngo_p
  7  WHERE acctno BETWEEN 1010 AND 1040
  8  UNION ALL
  9  SELECT 'TOTAL ASSETS', SUM(debit) - SUM(credit), null
 10  FROM chart_of_accounts_ngo_p
 11  WHERE acctno BETWEEN 1010 AND 1131
 12  UNION ALL
 13  SELECT 'TOTAL LIABILITIES AND OE', null, SUM(credit) - SUM(debit)
 14  FROM chart_of_accounts_ngo_p
 15  WHERE acctno BETWEEN 2020 AND 3200;

报告会这样出来:

ACCOUNT NAME                        DEBIT     CREDIT                            
------------------------------ ---------- ----------                            
CASH                                    6                                       
ACCOUNTS RECEIVABLE                     1                                       
ALLOWANCE FOR DOUBTFUL ACCTS                       1                            
INVENTORY                               1                                       
SUPPLIES                                1                                       
LAND                                    1                                       
BUILDING                                1                                       
ACCUMULATED DEPR BUILDING                          1                            
MACHINERY                               1                                       
ACCUMULATED DEPR MACHINERY                         1                            
ACCOUNTS PAYABLE                                   1                                                     
WAGES PAYABLE                                      2                            
TAX PAYABLE                                        2                            
NOTES PAYABLE                                      2                            
COMMON STOCK                                       1                            
RETAINED EARNINGS                                  1                            
TOTAL CURRENT ASSETS                    8                                       
TOTAL ASSETS                            9                                       
TOTAL LIABILITIES AND OE                           9   

【问题讨论】:

您想要每行的结果?还是所有结果都在一行中? 什么是“显示在最底部而不是中间”? 【参考方案1】:

你仍然可以用你的 union select 语句来做到这一点。您必须为订单创建一个伪列,如下所示:

SELECT "Account Name", debit, credit FROM
(SELECT Acctname AS "Account Name", debit, credit, 
  CASE WHEN acctno BETWEEN 1010 AND 1040 THEN 10
       WHEN acctno BETWEEN 1041 AND 1131 THEN 20
       ELSE 30 END AS acctOrder
  FROM chart_of_accounts_ngo_p
  WHERE acctno BETWEEN 1010 AND 3200
  UNION ALL
  SELECT 'TOTAL CURRENT ASSETS', SUM(debit) - SUM(credit) AS debit, null AS credit, 15 AS acctOrder
  FROM chart_of_accounts_ngo_p
  WHERE acctno BETWEEN 1010 AND 1040
  UNION ALL
  SELECT 'TOTAL ASSETS', SUM(debit) - SUM(credit) AS debit, null AS credit, 25 AS acctOrder
  FROM chart_of_accounts_ngo_p
  WHERE acctno BETWEEN 1010 AND 1131
  UNION ALL
  SELECT 'TOTAL LIABILITIES AND OE', null AS debit, SUM(credit) - SUM(debit) AS credit, 35 AS acctOrder
  FROM chart_of_accounts_ngo_p
  WHERE acctno BETWEEN 2020 AND 3200)
ORDER BY acctOrder

【讨论】:

以上是关于SQL如何使用JOIN创建小计的主要内容,如果未能解决你的问题,请参考以下文章

如何按小计创建 SQL 组?

SQL Server 2008 如何对分组进行小计

SQL如何查询总计和小计

如何在 SQL 中获取时间数据类型的小计?

SQL SEVER分类汇总后如何让小计和总计放到下面

如何使用 ChatGPT 编写 SQL JOIN 查询