|
我需要一个完整的查询,可以将其放入报表显示工具(如Crystal Reports或Report Writer)中。( K9 s3 w+ u0 A
我几乎可以完成这项工作,但我似乎不能 总计” 放在底部。% ^- o" Y# ^7 [8 O
SQL Fiddle表格不会建立,表示内存不足。所以我能做的就是把它贴在这里。1 G9 w+ y9 m2 U. ]3 Y. g+ q
这是我的例子:
6 Q: ^4 O" T1 W: c) yCREATE 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]这是一些示例数据:" y9 G0 U) l7 u* j1 m5 C
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);有了这些数据,我创建了以下查询:
$ y! E) M/ ?% X4 v+ Vselect 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 jp2Invoicegroup by case when ChargeAmount=0 then 'Cash' else 'Charge' end,TaxRate查询生成下表:
5 [% w" t9 A! H0 y, Z" P6 QTxType TaxRate SubTotal TaxAmt Tendered Change Charged TotalCharge 0.0000 215.6500 .00000 .0000 0.0000 215.6500 215.6500Charge 6.0000 558.8300 335.30000 .0000 0.0000 355.4180 355.4180Cash 8.2500 93.1400 93.1400 140.0000 39.1759 0.0000 100.8239Charge 8.2500 523.4700 523. .0000 0.0000 566.6563 566.6561首先:看起来这个表首先按下:TaxRate分组,然后按 TxType” 分组 ,即使 group by 子句显示
) Q% } [- K5 f$ p0 ETxType计算排名第一。如何强迫现金排名第一,收费排名第二?
+ I$ {/ `0 M. _# L/ b" h, f2 d u第二,如何在底部写一个 “总计” 行,用计算结果显示总计?我手动计算了以下示例表中的一个:
, |) ^4 I a$ V. pTxType TaxRate SubTotal TaxAmt Tendered Change Charged TotalCharge 0.0000 215.6500 .0000 0.0000 0.0000 215.6500 215.6500Charge 6.0000 558.8300 335.30000 .0000 0.0000 355.4180 355.4180Cash 8.2500 93.1400 93.1400 140.0000 39.1759 0.0000 100.8239Charge 8.2500 523.4700 523. .0000 0.0000 566.6563 566.6561Totals .0900 951.91 140.0000 39.1759 1137.7243 1238.5480可能会有一些错别字,因为都是手工编辑的。# t7 h. G' w8 G, W- o; ]
首先,我如何获取数据?TxType总计行显示并显示在底部?
( Q# L& A/ O% o$ u) ]
. o( N! ^5 n) e: L6 p+ ` 解决方案:
], J: z) u8 W( L7 q 在SQL Server中,可用WITH ROLLUP获取总计行并使用ORDER BY对行进行排序。您也可以使用它GROUPING()添加“
8 e7 h) w5 W5 ITOTALS”标签。
+ S7 e6 Q0 c5 vTaxRate WITH ROLLUPORDER BY case when ChargeAmount=0 then 'Cash' else 'Charge' end TaxRate WITH ROLLUPORDER BY case when ChargeAmount=0 then 'Cash' else 'Charge' end |
|