2017-08-14 86 views
0

我正在嘗試查詢具有多個組的表以獲得滿足特定條件的每個組中10%的系統。返回符合特定條件的每個組的記錄的10%

這裏的數據:

create table tablename as 
select '4FH6V1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select '0GLYQ1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select 'P191R1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select '7CMJ02' as ComputerName, 'AZ3' as Location, '3392' as Rating from dual union all 
select '8W2QS1' as ComputerName, 'AZ4' as Location, '3093' as Rating from dual union all 
select 'K9CHX1' as ComputerName, 'AZ7' as Location, '3192' as Rating from dual union all 
select '3XZNS1' as ComputerName, 'AZ7' as Location, '3093' as Rating from dual union all 
select '79RGX1' as ComputerName, 'AZ9' as Location, '3192' as Rating from dual union all 
select '02BR22' as ComputerName, 'AZ3' as Location, '2593' as Rating from dual 
; 

| ComputerName | Location | Rating | 
|--------------|----------|--------| 
| 4FH6V1  | AZ1  | 3093 | 
| 0GLYQ1  | AZ1  | 3093 | 
| P191R1  | AZ1  | 3093 | 
| 7CMJ02  | AZ3  | 3392 | 
| 8W2QS1  | AZ4  | 3093 | 
| K9CHX1  | AZ7  | 3192 | 
| 3XZNS1  | AZ7  | 3093 | 
| 79RGX1  | AZ9  | 3192 | 
| 02BR22  | AZ3  | 2593 | 

還有更多的記錄是這樣的表格。例如,我需要找到計算機名稱的10%,在有3093.

+1

如果您甚至沒有提供足夠的信息,您如何期待別人提供幫助?你在用什麼SQL?不同的dbms之間的語法不同。 – Eric

回答

2

差餉每個位置如果您使用的是SQL服務器,你可以在你的select語句中使用PERCENT

SELECT TOP 10 PERCENT * FROM tablename WHERE RATING=3093; 
+0

我認爲這將只返回所有地點合併的10%,而不是分別爲每個地點的10%。所以我需要計算機名稱按位置分開,只有10%的計算機名稱的分級爲3093.我使用的是PERCENT,但無法找出其餘的。謝謝。 –

0

如果您正在使用SQL Server,您可以使用apply

select tt.* 
from (select distinct location from t where t.rating = 3093) l apply 
    (select top (10) PERCENT t.* 
     from t 
     where t.location = l.location and t.rating = 3093 
    ) tt 
2

這應該在Oracle工作,雖然樣本數據是不是足夠大,以獲得10%的樣本...

create table tablename as 
select '4FH6V1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select '0GLYQ1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select 'P191R1' as ComputerName, 'AZ1' as Location, '3093' as Rating from dual union all 
select '7CMJ02' as ComputerName, 'AZ3' as Location, '3392' as Rating from dual union all 
select '8W2QS1' as ComputerName, 'AZ4' as Location, '3093' as Rating from dual union all 
select 'K9CHX1' as ComputerName, 'AZ7' as Location, '3192' as Rating from dual union all 
select '3XZNS1' as ComputerName, 'AZ7' as Location, '3093' as Rating from dual union all 
select '79RGX1' as ComputerName, 'AZ9' as Location, '3192' as Rating from dual union all 
select '02BR22' as ComputerName, 'AZ3' as Location, '2593' as Rating from dual 
; 

with cte1 as (
select 
    x.ComputerName 
    ,x.Location 
    ,x.Rating 
    ,count(1) over (partition by Location) as location_total 
    ,row_number() over (partition by Location order by ComputerName) as location_position 
    ,row_number() over (partition by Location order by ComputerName)/count(1) over (partition by Location) as location_pct 
from tablename x 
where x.Rating = 3093 
) 
select * 
from cte1 
where location_pct <= 0.1 
+0

不錯的一個。僅供參考:從'dual'移除'',它也可以在MSSQL上工作(或者創建一個稱爲dual的1行虛表)。 '創建表雙(x位);插入雙選擇1'。 – JohnLBevan

+0

這太好了。我需要修改它,因爲它由於某種原因沒有得到確切的結果。當我手動計數時,我有184臺計算機符合x.Rating = 3093條件,但查詢只返回9,當它應該是18.我是一個noob,所以我沒有完全理解over/partition語句。 –

+0

如果您可以發佈更大的數據樣本,則可以更容易地找出問題所在。 –

相關問題