2016-09-30 73 views
0

我有兩列,它們是按另一列中的值計算的。計算列上的自定義排序

CASE 
    WHEN ACT.TotalScore > 850 THEN 'Cok Basarili' 
    WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 'Basarili' 
    ELSE 'Basarisiz' END as Basari, 
    CASE 
    WHEN ACT.TotalScore > 850 THEN 'rgba(123,192,67,1)' 
    WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 'rgba(253,244,152,1)' 
    ELSE 'rgba(243,119,54,1)' END as Renk 

他們計算成功,並根據他們的點實際分配一種顏色。

ON GROUP BY我用同樣的CASE WHEN

總產量(與其他的選擇元素ADN where子句等,並沒有ORDER BY)

+---------------+--------------+---------------------+ 
| DenetimSayisi | Basari |  Renk   | 
+---------------+--------------+---------------------+ 
|   35 | Cok Basarili | rgba(123,192,67,1) | 
|   60 | Basarisiz | rgba(243,119,54,1) | 
|   29 | Basarili  | rgba(253,244,152,1) | 
+---------------+--------------+---------------------+ 

我想根據做出的客戶訂單'伯薩里' 列,並試圖此

ORDER BY CASE 
    WHEN ACT.TotalScore > 850 THEN 2 
    WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 1 
    ELSE 3 END 

給出該錯誤

Msg 8127, Level 16, State 1, Line 36 
Column "Activity.TotalScore" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause. 
Msg 8127, Level 16, State 1, Line 37 
Column "Activity.TotalScore" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause. 
Msg 8127, Level 16, State 1, Line 37 
Column "Activity.TotalScore" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause. 

我不知道還有什麼我可以嘗試或我該如何解決這個問題。

+0

你能顯示你所有的查詢嗎? – gofr1

回答

1

在子查詢中計算您的訂單,並在其之外訂購。

SELECT 
    * 
FROM 
(
    SELECT 
     CASE 
     WHEN ACT.TotalScore > 850 THEN 'Cok Basarili' 
     WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 'Basarili' 
     ELSE 'Basarisiz' END as Basari, 
     CASE 
     WHEN ACT.TotalScore > 850 THEN 'rgba(123,192,67,1)' 
     WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 'rgba(253,244,152,1)' 
     ELSE 'rgba(243,119,54,1)' END as Renk 

     OrderByField=CASE 
     WHEN ACT.TotalScore > 850 THEN 2 
     WHEN ACT.TotalScore <= 850 AND ACT.TotalScore > 700 THEN 1 
     ELSE 3 END 
    FROM 
     Table 
) 
AS A 
ORDER BY 
    A.OrderByField 
+0

謝謝,工作很好 –

1

你必須把你的新編號邏輯放在組中。製作一個新的專欄並按順序排列。

SELECT DenetimSayisi 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 'Cok Basarili' 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 'Basarili' 
     ELSE 'Basarisiz' 
     END AS Basari 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 'rgba(123,192,67,1)' 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 'rgba(253,244,152,1)' 
     ELSE 'rgba(243,119,54,1)' 
     END AS Renk 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 2 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 1 
     ELSE 3 
     END AS newColumn 
FROM yourtable 
ORDER BY CASE 
     WHEN ACT.TotalScore > 850 
      THEN 2 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 1 
     ELSE 3 
     END 

如果你不想竟查詢新的排序列,你可以使用一個CTE掉落。

;WITH cte AS (
SELECT DenetimSayisi 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 'Cok Basarili' 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 'Basarili' 
     ELSE 'Basarisiz' 
     END AS Basari 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 'rgba(123,192,67,1)' 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 'rgba(253,244,152,1)' 
     ELSE 'rgba(243,119,54,1)' 
     END AS Renk 
    , CASE 
     WHEN ACT.TotalScore > 850 
      THEN 2 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 1 
     ELSE 3 
     END AS newColumn 
FROM yourtable 
ORDER BY CASE 
     WHEN ACT.TotalScore > 850 
      THEN 2 
     WHEN ACT.TotalScore <= 850 
      AND ACT.TotalScore > 700 
      THEN 1 
     ELSE 3 
     END 
) 
Select DenetimSayisi, basari, renk from cte