久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL Server 窗口函數子句中的條件排序順序

Conditional sort order in SQL Server windowed function clauses(SQL Server 窗口函數子句中的條件排序順序)
本文介紹了SQL Server 窗口函數子句中的條件排序順序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

所以,這不是一般的條件排序"問題……我在這里遇到了一個相當棘手的問題.:-) 我想讓我的存儲過程為結果提供條件排序.通常,這可以通過以下方式完成:

So, this is not your average 'conditional sort by' question... I have a rather tricky problem here. :-) I want to allow my stored procedure to offer a conditional sort order for the results. Normally this can be done in the following manner:

SELECT *
INTO #ResultsBeforeSubset
FROM
    MyTable
ORDER BY
    CASE WHEN @SortAscending=1 THEN 'SortColumn' END ASC,
    CASE WHEN @SortAscending=0 THEN 'SortColumn' END DESC

我想圍繞實際的 ASC/DESC 做一個 CASE 語句,但這不起作用.上述方法起作用的原因是,當@SortAscending 不等于給定值時,SQL 服務器將CASE 語句轉換為常量NULL.所以,如果 @SortAscending 是 0,你實際上有:

I'd like to do a CASE statement around the actual ASC/DESC, but that doesn't work. The reason the above method works is because, when @SortAscending isn't equal to the given value, SQL server translates the CASE statement into the constant NULL. So, if @SortAscending is 0, you effectively have:

ORDER BY
    NULL ASC,
    SortColumn DESC

然后,第一個排序表達式什么也不做.這是有效的,因為在常規 SELECT 語句中,您可以在 ORDER BY 子句中使用常量.

The first sort expression, then, just does nothing. This works because in a regular SELECT statement you can use constant in an ORDER BY clause.

問題是,我在存儲過程中排序的時間是在包含窗口函數 ROW_NUMBER()SELECT 語句期間.因此,我想將 CASE 語句放在其 OVER 子句中,如下所示:

Trouble is, the time that I'm sorting in my stored proc is during a SELECT statement which contains a windowed function ROW_NUMBER(). I therefore want to put the CASE statement inside its OVER clause, like so:

SELECT *
INTO #ResultsBeforeSubset
FROM (
    SELECT
        ROW_NUMBER() OVER (
            ORDER BY
                CASE WHEN @SortAscending=1 THEN rowValues.[SortColumn] END ASC,
                CASE WHEN @SortAscending=0 THEN rowValues.[SortColumn] END DESC
        ) AS RowNumber,
        *
    FROM (
        -- UNIONed SELECTs returning rows go here...
    ) rowValues
) rowValuesWithRowNum

不幸的是,當您運行存儲過程時,這會導致以下錯誤:

Unfortunately, this causes the following error when you run the stored procedure:

Windowed functions do not support constants as ORDER BY clause expressions.

因為這是一個窗口函數的子句,所以CASE語句到常量NULL的轉換是無效的.

Because this is the clause of a windowed function, the conversion of the CASE statement to the constant NULL is invalid.

誰能想出一種方法,我可以有條件地改變 UNIONed SELECT 的排序順序,并為這些排序結果產生的每一行分配行號?我知道我可以將整個查詢構造為一個字符串并作為完全動態的 SQL 執行它,但如果可能的話,我寧愿避免這種情況.

Can anyone think of a way that I can conditionally vary the sort order of UNIONed SELECTs, and assign row numbers to each row resulting from these sorted results? I know I could resort to constructing the entire query as a string and execute that as fully dynamic SQL, but I'd rather avoid that if possible.

更新: 看起來問題不是由 CASE 語句本身引起的,而是因為我只使用了CASE 語句的條件子句中的常量值.我對這種奇怪的行為提出了一個新問題 這里.

UPDATE: Looks like the problem wasn't caused by the CASE statement per se, but by the fact that I was using only constant values in the CASE statement's conditional clause. I've started up a new question on this curious behaviour here.

推薦答案

您可以在兩個方向分配行號,并在外部 order by 中選擇一個:

You could assign row numbers in two directions, and pick one in an outer order by:

select  *
from    (
        select  row_number() over (order by SortColumn) rn1
        ,       row_number() over (order by SortColumn) rn2
        ,       *
        from    @t
        ) as SubQueryAlias
order by
        case when @asc=1 then rn1 end
,       case when @asc=0 then rn2 end desc

SE 數據的工作示例.

這篇關于SQL Server 窗口函數子句中的條件排序順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 色999视频 | 久草资源| 国产精品国产a | av国产精品 | 日本成人久久 | 欧美aaaaaaaaaa| 99久久99| 日韩免费av | av国产精品| 欧美一区二区在线 | 精品少妇一区二区三区在线播放 | 欧美日韩一区二区三区四区五区 | cao在线| 999久久久 | 三级成人片 | 天天操网 | 亚洲狠狠丁香婷婷综合久久久 | 久久人人网 | 久久区二区 | 在线播放一区二区三区 | 国产欧美在线一区 | 中文字幕1区2区3区 日韩在线视频免费观看 | 精品欧美一区二区久久久伦 | 亚洲精品国产综合区久久久久久久 | 久久久不卡网国产精品一区 | 玖玖视频国产 | 日韩精品一二三 | 久久久噜噜噜久久中文字幕色伊伊 | 国产黑丝在线 | 国产一区二区日韩 | 久久欧美精品 | 国产欧美精品区一区二区三区 | 五月婷婷在线视频 | 欧美人妖网站 | 九九免费 | 久久久av | 久久国产精品视频 | 香蕉婷婷| 精品国产一区二区三区久久 | 国产精品久久久久久久久免费桃花 | 久久精品视频一区二区三区 |