2012-12-18 78 views
0
select 
    case location_id 
     when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
     when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
     when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
    end as CITY, 
    count(*) as Total 
from #tmptab1 
group by CITY 

我得到錯誤組由錯誤的SQL Server

消息207,級別16,狀態1,行12
無效列名 'CITY'

如何糾正這個 ?請幫忙。

回答

5

你不能使用列別名GROUP BY條款。請參閱Conceptual Order of Evaluation of a Select Statement,您將看到以下順序評估(邏輯)子句:FROM, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, UNION, ORDER BY

即發動機不完全是如何執行的操作,但它是你爲什麼不能GROUP BY子句中使用的東西從SELECT語句紮實的實踐認識一個有用的啓發 - 這在邏輯上不是可用到GROUP BY條款,因爲它在稍後評估。

有幾種方法可以解決此問題:

  1. 重複GROUP BY子句中的整個表達式:

    SELECT 
        City = 
         CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
         when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
         when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
         END, 
        Total = Count(*) 
    FROM #tmptab1 
    GROUP BY 
        CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
        when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
        when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
        END 
    
  2. 使用派生表:

    SELECT 
        City, 
        Total = Count(*) 
    FROM 
        (
         SELECT 
         City = 
          CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
          when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
          when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
          END 
         FROM #tmptab1 
        ) Cities 
    GROUP BY City; 
    
  3. 使用公用表表達式(CTE),SQL Server 2005及更高版本:

    WITH Cities AS (
        SELECT 
         City = 
          CASE location_id 
          when 1 then 'DELHI' 
          when 2 then 'AHMEDABAD' 
          when 4 then 'HYDERABAD' 
          when 5 then 'KOLKATA' 
          when 6 then 'BANGALORE' 
          when 7 then 'MUMBAI' 
          when 8 then 'CHENNAI' 
          END 
        FROM #tmptab1 
    ) 
    SELECT 
        City, 
        Total = Count(*) 
    FROM Cities 
    GROUP BY City; 
    
  4. 使用CROSS應用,SQL Server 2005和了起來:

    SELECT 
        City, 
        Total = Count(*) 
    FROM 
        #tmptab1 
        CROSS APPLY (
         SELECT 
         City = 
          CASE location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
          when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
          when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
          END 
        ) C 
    GROUP BY City; 
    

因爲你的表情是確定性的,這是可能的,你可以簡單地做GROUP BY location_id,然而,這是不正常的的情況下,你不應該期望能夠通過選擇單個列來繞過正常的聚合分組邏輯,因爲大多數情況下這種CASE表達式增加了非確定性的值。事實上,因爲信息不僅是確定性的,而是關於真實世界(而不是業務規則),我建議您不要在查詢中編碼這些信息!創建一個Location表並加入到它。將可更改的用戶數據直接放入查詢並不是最佳做法 - 查詢應該記錄過程,而不是內容,以及如果添加新的location_id,該怎麼辦?所有使用它的查詢都必須更改。此外,如果多個location_id可以指向同一個城市,則location_id的分組將無法正常工作。

+0

謝謝你的詳細解釋,真的很感激.. –

2

你不能使用aliasses由語句組 你會

select case location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
when 7 then 'MUMBAI' when 8 then 'CHENNAI' end as CITY,count(1) as Total 
from #tmptab1 
group by case location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
when 7 then 'MUMBAI' when 8 then 'CHENNAI' end 

上進行分組LOCATION_ID本身也可能工作需要整個零件從組中選擇

+0

謝謝...上LOCATION_ID分組的工作! –

+1

歡迎您使用,在某些情況下,結果可能與原始價值不同。如果1和2給了'DELHI',你將需要整個陳述或結果將是錯誤的 – ufosnowcat

1

關注中(改變您的查詢只是group by location_id)作用於Sql-server 2008 or above(不低於確定)

select 
    case location_id 
     when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
     when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
     when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
    end as CITY, 
    count(*) as Total 
from #tmptab1 
group by location_id --NOTE 

Or可以使用

select case location_id when 1 then 'DELHI' when 2 then 'AHMEDABAD' 
      when 4 then 'HYDERABAD' when 5 then 'KOLKATA' when 6 then 'BANGALORE' 
      when 7 then 'MUMBAI' when 8 then 'CHENNAI' 
      end as CITY, total 
from (
    select location_id, count(*) total 
    from #tmptab1 
    group by location_id) A