小编典典

SQL查询中的摘要和总发票数据

sql

我需要一个完整的查询,可以将其放入报表显示工具(如Crystal Reports或Report Writer)中。

我几乎可以完成这项工作,但似乎无法将“ 总计” 行放在最下面。

SQL Fiddle不会建立该表,表示内存不足。(???)因此,我所能做的就是将其粘贴在这里。

这是我的示例表:

CREATE TABLE [dbo].[jp2Invoice](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [InvoiceNumber] [varchar](20) NOT NULL,
    [Subtotal] [decimal](9, 4) NOT NULL,
    [Taxable] [varchar](1) NULL,
    [TaxRate] [decimal](9, 4) NULL,
    [TaxableAmount] [decimal](9, 4) NOT NULL,
    [NontaxableAmount] [decimal](9, 4) NOT NULL,
    [Tax] [decimal](9, 4) NOT NULL,
    [Total] [decimal](9, 4) NOT NULL,
    [AmountTendered] [decimal](9, 4) NOT NULL,
    [ChangeAmount] [decimal](9, 4) NOT NULL,
    [AmountDue] [decimal](9, 4) NOT NULL,
    [ChargeAmount] [decimal](9, 4) NOT NULL,
 CONSTRAINT [PK_jp2Invoice] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY]

这是一些示例数据:

insert into jp2Invoice(
  InvoiceNumber, Subtotal, Taxable, TaxRate, TaxableAmount, NontaxableAmount, Tax, Total, AmountTendered, ChangeAmount, AmountDue, ChargeAmount
) values 
('ABC001',  19.9500, 'Y', 8.2500,  19.9500,   0.0000,  1.6458,  21.5958,  40.0000, 18.4041,   0.0000,   0.0000),
('ABC002', 558.8300, 'Y', 6.0000, 335.3000, 223.5300, 20.1180, 355.4180,   0.0000,  0.0000,   0.0000, 355.4180),
('ABC003', 281.4700, 'Y', 8.2500, 281.4700,   0.0000, 23.2212, 304.6912,   0.0000,  0.0000,   0.0000, 304.6913),
('ABC004',  95.9800, 'Y', 8.2500,  95.9800,   0.0000,  7.9183, 103.8983,   0.0000,  0.0000,   0.0000, 103.8983),
('ABC005',  73.1900, 'Y', 8.2500,  73.1900,   0.0000,  6.0381,  79.2281, 100.0000, 20.7718,   0.0000,   0.0000),
('ABC006', 215.6500, 'N', 0.0000,   0.0000, 215.6500,  0.0000, 215.6500,   0.0000,  0.0000,   0.0000, 215.6500),
('ABC007', 146.0200, 'Y', 8.2500, 146.0200,   0.0000, 12.0466, 158.0666,   0.0000,  0.0000,   0.0000, 158.0667);

有了这些数据,我创建了以下查询:

select
 case when ChargeAmount=0 then 'Cash' else 'Charge' end as 'TxType',
 TaxRate,
 sum(Subtotal) as 'SubTotal',
 sum(TaxableAmount) as 'TaxAmt',
 sum(AmountTendered) as 'Tendered',
 sum(ChangeAmount) as 'Change',
 sum(ChargeAmount) as 'Charged',
 sum(Total) as 'Total'
from jp2Invoice
group by case when ChargeAmount=0 then 'Cash' else 'Charge' end,
 TaxRate

该查询产生下表:

TxType TaxRate  SubTotal      TaxAmt    Tendered     Change  Charged    Total
Charge  0.0000  215.6500      0.0000      0.0000     0.0000 215.6500    215.6500
Charge  6.0000  558.8300    335.3000      0.0000     0.0000 355.4180    355.4180
Cash    8.2500   93.1400     93.1400    140.0000    39.1759   0.0000    100.8239
Charge  8.2500  523.4700    523.4700      0.0000     0.0000 566.6563    566.6561

首先:看起来该表首先按TaxRate分组,然后按“ TxType” 分组 ,即使 group by 子句显示“
TxType”计算排在最前面。我该如何强制“现金”排在第一位,而“收费”排在第二位呢?

第二:如何在底部写一个 “总计” 行,并用计算结果显示总计?我在下面的示例表中手动计算了一个:

TxType TaxRate  SubTotal      TaxAmt    Tendered     Change  Charged    Total
Charge  0.0000  215.6500      0.0000      0.0000     0.0000  215.6500    215.6500
Charge  6.0000  558.8300    335.3000      0.0000     0.0000  355.4180    355.4180
Cash    8.2500   93.1400     93.1400    140.0000    39.1759    0.0000    100.8239
Charge  8.2500  523.4700    523.4700      0.0000     0.0000  566.6563    566.6561
Totals         1391.0900    951.91      140.0000    39.1759 1137.7243   1238.5480

可能会有一些错别字,因为它们都是手工编辑的。

我如何获取数据首先通过TxType显示并在底部显示总计行?


阅读 226

收藏
2021-05-30

共1个答案

小编典典

在SQL Server中,可以使用WITH ROLLUP获取总计行,并使用ORDER BY来对行进行排序。您也可以使用GROUPING()添加“
TOTALS”标签。

select
 CASE WHEN GROUPING(case when ChargeAmount=0 then 'Cash' else 'Charge' end) = 1 THEN 'Total' ELSE case when ChargeAmount=0 then 'Cash' else 'Charge' end END as 'TxType',
 TaxRate,
 sum(Subtotal) as 'SubTotal',
 sum(TaxableAmount) as 'TaxAmt',
 sum(AmountTendered) as 'Tendered',
 sum(ChangeAmount) as 'Change',
 sum(ChargeAmount) as 'Charged',
 sum(Total) as 'Total'
from jp2Invoice
group by case when ChargeAmount=0 then 'Cash' else 'Charge' end,
 TaxRate WITH ROLLUP

ORDER BY case when ChargeAmount=0 then 'Cash' else 'Charge' end
2021-05-30