簡體   English   中英

SQL select 子查詢在使用 MAX function 的情況下

[英]SQL select subquery within case when using MAX function

在查詢我的報告時,我需要使用 max function,但我收到一個錯誤

ORA-01427 - 單行子查詢返回多於一行

在創建子查詢時。 我閱讀了它,並嘗試重新排列代碼,但仍然無法正常工作。 你知道如何正確寫嗎? 我有點小學生。 謝謝! 這是查詢的一部分

case when        
(
(select max_day from   
(select max(a.day) as max_day, 
a.id as id_customer
from table a  
group by a.id))
<= '30'

and a.c in ('A','B','C') 
and a.d not in ('1','2')
)
then a.amount

end as amount_30

您的內部查詢為每個“id”返回“max_day”,案例必須得到一行。 把你 select 作為查詢並加入它

select case when b.max_day <= '30' and a.c in ('A','B','C') and ad not in ('1','2') then a.amount end as amount_30 from yourtable a join (select max(a.day) as max_day, a.id as id_customer from table a group by a.id) b on a.id=b.id_customer

DAY 248, 1, 0, 28 ID 1, 1, 2, 1 AMOUNT 2321, 4351, 4261, 325 C A, A, C, C D 1, 4, 5, 5

在此處輸入圖像描述

條件是為每個ID選擇最大天數(ID級別的最大天數),然后使用到期金額。 (同時遵循其他條件 <= '30',a.c 在('A','B','C'),廣告不在('1','2')

從上面的示例中,預期結果是 ID = 1 的金額 325。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM