繁体   English   中英

我在结合 `with rollup` 和 `GROUPING` 时遇到问题。 如何纠正?

[英]I am having issues combining `with rollup` and `GROUPING`. How to correct?

我最近了解了一个叫做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);

如果您希望对汇总列值使用'Total' ,则需要将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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM