我最近介绍了一个名为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);
答案 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;