1

我最近被介绍到一个名为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);
4

1 回答 1

0

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

然后按日期排序,并将您的 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;
于 2015-07-30T14:42:09.840 回答