簡體   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