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

时间:2016-02-23 17:06:20

标签: sql sql-server

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

我几乎让这个工作,但我似乎无法在底部获得 Grand Totals 行。

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首先显示数据并在底部显示Totals行?

2 个答案:

答案 0 :(得分:8)

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

undefined

答案 1 :(得分:3)

使用UNION ALL

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
UNION ALL
select
'Totals',
NULL,
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