我遇到了`与汇总`和`GROUPING'相结合的问题。如何纠正?

时间:2015-07-30 14:35:32

标签: sql tsql sap sapb1

我最近介绍了一个名为with rollup的功能。我正在添加到我在Excel中用于趋势的报告中。问题是,我希望从最早到最新的日期订购t0.DocDate

我读了一篇关于http://mangalpardeshi.blogspot.com/2009/08/rollup-and-order-by.html

的文章

我将代码格式化为与文章中的示例相似,但是在执行此操作后收到此错误消息:Conversion failed when converting date and/or time from character string.

无论如何都要在查询中更正此错误?如果是这样,任何能够帮助我的人都会非常感激!

我的代码是:

select  CASE WHEN GROUPING(t0.DocDate) = 1 THEN 'Total' ELSE T0.DocDate END AS 'DocDate',
        COUNT(T1.Itemcode) [# of Cross Sell],
        SUM(T1.Price * t1.Quantity) [Cross Sell $]

from ORDR t0 inner join RDR1 t1 on t0.DocEntry=t1.DocEntry

where t0.CANCELED <> 'Y'
and t1.U_SII_XSell = 'Y'

group by t0.DocDate WITH ROLLUP
order by GROUPING(t0.docdate);

1 个答案:

答案 0 :(得分:0)

如果您希望将DocDate用于汇总列值,则需要将VARCHAR转换为'Total'。该列不能同时为两种类型。

要按日期排序,并将“总计”列放在最后,您只需将t0.DocDate添加到ORDER BY子句中。

select  CASE WHEN GROUPING(t0.DocDate) = 1 THEN 'Total' ELSE Cast(T0.DocDate As Varchar(15)) END AS 'DocDate',
        COUNT(T1.Itemcode) [# of Cross Sell],
        SUM(T1.Price * t1.Quantity) [Cross Sell $]

from ORDR t0 inner join RDR1 t1 on t0.DocEntry=t1.DocEntry

where t0.CANCELED <> 'Y'
and t1.U_SII_XSell = 'Y'

group by t0.DocDate WITH ROLLUP
order by GROUPING(t0.docdate), t0.docdate;