2016-11-16 81 views
1

請告訴我什麼是錯的:SQL - 數超過分區

select 
p.date, 
(SELECT COUNT (*) OVER (PARTITION BY p.ID_code) 
FROM POOL_AREA.F_volume_GCOA p 
WHERE p.end_volume_KN <> 0 
AND p.date    = '31-OCT-16' 
AND (p.local_grade_MB IS NULL OR p.local_grade_MB ='0' OR   p.local_grade_MB ='N')) AS ERROR_CNT 
FROM POOL_AREA.F_volume_GCOA p 
where p.date    = '31-OCT-16'; 

在分區的部分是問題,如果沒有它,它的工作原理。

錯誤的詳細信息:
ORA-01427:單行子查詢返回不止一行
01427. 00000 - 「單行子查詢返回多個行」
*原因:
*行動:

+1

首先,你有一個'Oracle'錯誤信息,但是你把問題標記爲'MySQL'和'SQL Server'。 – Siyual

+1

通過分區你會得到更多的結果。在您選擇時,它可能只是一個單一的值。你可以加入子查詢而不是將它添加到你的SELECT中 – Johan

+2

爲什麼你在這裏有一個子查詢? 'date'也是保留字,不是有效的列名;和'31-OCT-16'是一個不是日期的字符串。如果您使用正確的數據類型,生活會更輕鬆。有一列存儲'N'和'0'的列也很奇怪。 –

回答

0
在評論中提到

,像這樣(未測試):

select 
p.date, 
t.val AS ERROR_CNT  

FROM POOL_AREA.F_volume_GCOA p 
JOIN (SELECT p.ID_code, COUNT (*) as val OVER (PARTITION BY p2.ID_code) 
FROM POOL_AREA.F_volume_GCOA p2 
WHERE p2.end_volume_KN <> 0 
AND p2.date    = '31-OCT-16' 
AND (p2.local_grade_MB IS NULL OR p2.local_grade_MB ='0' 
    OR p.local_grade_MB ='N')) as t ON t.ID_Code = p.ID_code 
where p.date    = '31-OCT-16'; 
1

我想你實際上是後一個條件計數 - 是這樣的:

select p.dt, 
     count(case when p.end_volume_kn != 0 
         and (p.local_grade_mb is null 
           or p.local_grade_mb in ('0', 'N')) 
         then 1 
       end) error_cnt 
FROM  POOL_AREA.F_volume_GCOA p 
where p.dt = to_date('31/10/2016', 'dd/mm/yyyy') 
group by p.dt; 
0

這工作!

select 
p.date, 
(SELECT COUNT (*) FROM 
(SELECT DISTINCT p.id_code 
FROM POOL_AREA.F_volume_GCOA p 
WHERE p.end_volume_KN <> 0 
AND p.date    = '31-OCT-16' 
AND (p.local_grade_MB IS NULL OR p.local_grade_MB ='0' OR    p.local_grade_MB ='N'))) AS ERROR_CNT 
FROM POOL_AREA.F_volume_GCOA p 
where p.date    = '31-OCT-16';