简体   繁体   English

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

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

I recently was introduced to a feature called with rollup .我最近了解了一个叫做with rollup的功能。 I am adding into a report I use in Excel for trends.我将添加到我在 Excel 中使用的趋势报告中。 Issue is, I want to have the t0.DocDate ordered on date from oldest to newest date.问题是,我想让t0.DocDate按从最旧到最新的日期排序。

I read an article on: http://mangalpardeshi.blogspot.com/2009/08/rollup-and-order-by.html我读了一篇文章: http : //mangalpardeshi.blogspot.com/2009/08/rollup-and-order-by.html

I formatted my code to be similar to the example in the article but am getting this error message after doing so: Conversion failed when converting date and/or time from character string.我将我的代码格式化为与文章中的示例类似,但在执行此操作后收到此错误消息: Conversion failed when converting date and/or time from character string.

Is there anyway to correct this error in the query?无论如何要纠正查询中的这个错误? If so, it would be very much appreciated by anyone who could assist me!如果是这样,任何可以帮助我的人都会非常感激!

My code is:我的代码是:

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);

You need to convert your DocDate to a VARCHAR if you wish to use 'Total' for the rollup column value.如果您希望对汇总列值使用'Total' ,则需要将DocDate转换为VARCHAR The column cannot be both types.列不能同时是这两种类型。

To then order it by date, and have your Total column last, you need to just add t0.DocDate to your ORDER BY clause.然后按日期对其进行排序,并将您的 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