2010-12-22 41 views
3

我希望有人可能能夠指出我要去哪裏錯了,但我一直在尋找這最後30分鐘,並沒有得到任何地方它。選擇所有不在Top'n'中的數據爲'其他'

我有一個填充了數據的臨時表,前端應用程序不能爲我做任何邏輯,所以請原諒表中醜陋的case語句邏輯。

用戶對帶回的結果集感到滿意,因爲我得到前10條記錄。他們現在已經決定他們希望看到一組其他國家(所有排名不在前10位)作爲'其他'。

我試圖創建一個不在前10名的國家的分組,但它不工作,我正計劃將此結果聯合到前10名的結果中。

SELECT c.Country, count(*) AS 'Total_Number_of_customers', COALESCE(ili.new_customers,0) AS 'New_Customers', COALESCE(ilb.existing_first,0) AS 'Existing_First_Trans', COALESCE(ilc.existing_old,0) AS 'Existing_Prev_Trans' 
FROM #customer_tmp c 
LEFT JOIN (SELECT z.country, count(*) AS 'new_customers' FROM #customer_tmp z where z.customer_type='New_Customer' group by z.country)ili ON ili.country = c.country 
LEFT JOIN (SELECT zy.country, count(*) AS 'existing_first' FROM #customer_tmp zy where zy.customer_type='Existing_Customer' AND zy.first_transaction=1 group by zy.country)ilb ON ilb.country = c.country 
LEFT JOIN (SELECT zx.country, count(*) AS 'existing_old' FROM #customer_tmp zx where zx.customer_type='Existing_Customer' AND zx.first_transaction=0 group by zx.country)ilc ON ilc.country = c.country 
GROUP BY c.country, ili.new_customers, ilb.existing_first, ilc.existing_old 
ORDER BY 2 DESC 

這是我用來從我的表中獲取結果的SQL。

作爲參考,我的臨時表中的每一行都包含一個客戶ID,它們的創建日期和客戶類型,具體針對我要實現的目標。

希望這是一個簡單的問題,我只是有點慢..

在進階非常感謝。

回答

2

燁; EXCEPT,或者也許添加行號到您的查詢,然後選擇通過:

SELECT * FROM (

SELECT c.Country, count(*) AS 'Total_Number_of_customers', 
row_number() OVER (ORDER BY COUNT(*) DESC) AS 'r', 
COALESCE(ili.new_customers,0) AS 'New_Customers', COALESCE(ilb.existing_first,0) AS 'Existing_First_Trans', COALESCE(ilc.existing_old,0) AS 'Existing_Prev_Trans' 
FROM #customer_tmp c 
LEFT JOIN (SELECT z.country, count(*) AS 'new_customers' FROM #customer_tmp z where z.customer_type='New_Customer' group by z.country)ili ON ili.country = c.country 
LEFT JOIN (SELECT zy.country, count(*) AS 'existing_first' FROM #customer_tmp zy where zy.customer_type='Existing_Customer' AND zy.first_transaction=1 group by zy.country)ilb ON ilb.country = c.country 
LEFT JOIN (SELECT zx.country, count(*) AS 'existing_old' FROM #customer_tmp zx where zx.customer_type='Existing_Customer' AND zx.first_transaction=0 group by zx.country)ilc ON ilc.country = c.country 
GROUP BY c.country, ili.new_customers, ilb.existing_first, ilc.existing_old 
ORDER BY 2 DESC 

) sub_query WHERE sub_query.r >= 10 

這可能是更靈活,您可以運行一個查詢,然後向上劃分結果爲「十佳」和」休息「很容易。

(這相當於鮑勃的答案,我想我們都在完全相同的時間在做這個!)

+0

這裏有一個方法有一切都很好:-)非常感謝 – 2010-12-22 17:04:25

2

使用公共表表達式(CTE)

WITH CTE AS 
    (
    SELECT c.Country, count(*) AS 'Total_Number_of_customers', COALESCE(ili.new_customers,0) AS 'New_Customers', COALESCE(ilb.existing_first,0) AS 'Existing_First_Trans', COALESCE(ilc.existing_old,0) AS 'Existing_Prev_Trans' 
     , ROW_NUMBER() OVER (ORDER BY count(*) DESC) AS sequence 
    FROM #customer_tmp c 
    LEFT JOIN (SELECT z.country, count(*) AS 'new_customers' FROM #customer_tmp z where z.customer_type='New_Customer' group by z.country)ili ON ili.country = c.country 
    LEFT JOIN (SELECT zy.country, count(*) AS 'existing_first' FROM #customer_tmp zy where zy.customer_type='Existing_Customer' AND zy.first_transaction=1 group by zy.country)ilb ON ilb.country = c.country 
    LEFT JOIN (SELECT zx.country, count(*) AS 'existing_old' FROM #customer_tmp zx where zx.customer_type='Existing_Customer' AND zx.first_transaction=0 group by zx.country)ilc ON ilc.country = c.country 
    GROUP BY c.country, ili.new_customers, ilb.existing_first, ilc.existing_old 
    ) 

SELECT * 
FROM CTE 
WHERE sequence > 10 
ORDER BY sequence 
1
SELECT country, COUNT(*) cnt, SUM(new_customer), SUM(existing_first_trans), SUM(existing_prev_trans) 
FROM (
     SELECT CASE 
       WHEN country IN 
       (
       SELECT TOP 10 country 
       FROM #customer_tmp 
       ORDER BY 
         COUNT(*) DESC 
       ) THEN 
         country 
       ELSE 'Others' 
       END AS country, 
       CASE WHEN customer_type = 'New_Customer' THEN 1 END AS new_customer, 
       CASE WHEN customer_type = 'Existing_Customer' AND first_transaction = 1 THEN 1 AS existing_first_trans, 
       CASE WHEN customer_type = 'Existing_Customer' AND first_transaction = 0 THEN 1 AS existing_prev_trans, 
     FROM #customer_tmp 
     ) 
GROUP BY 
     country 
ORDER BY 
     CASE country WHEN 'Others' THEN 2 ELSE 1 END, cnt DESC 
相關問題