定义最大值的错误结果

时间:2015-11-25 11:16:02

标签: sql oracle plsql

当我想从代码中选择最大值时(下面的代码)。它显示了2个最大行结果。但我希望得到一个最大值。 代码如下:

select 
distinct(c.msisdn), 
max(adj_date),
6144 - (round(t.volume /1024/1024,2))as остаток_мб 
from 
subscriber_discount_threads t, 
client_balance c, 
subs_packs p 
where 
t.dctr_dctr_id = 1283 
and p.pack_pack_id in (877,874) 
and c.msisdn = '550350057' 
and t.adj_date >= '30.10.2015' 
and sysdate between p.start_date and p.end_date 
and sysdate between t.start_thread and t.end_thread  
and c.subs_subs_id = t.subs_subs_id 
and p.subs_subs_id = c.subs_subs_id 
group by c.msisdn, t.volume

结果如下(2行,但我希望它只显示我的最大日期)

25.11.2015 13:08:06
03.11.2015 11:42:06

可能是什么问题?

3 个答案:

答案 0 :(得分:1)

编辑:根据OP

更新了我的答案
'N/A'

答案 1 :(得分:0)

你可以尝试这个:

select 
distinct c.msisdn, 
max(adj_date) over (order by null),
6144 - (round(t.volume /1024/1024,2))as остаток_мб 
from 
subscriber_discount_threads t, 
client_balance c, 
subs_packs p 
where 
t.dctr_dctr_id = 1283 
and p.pack_pack_id in (877,874) 
and c.msisdn = '550350057' 
and t.adj_date >= '30.10.2015' 
and sysdate between p.start_date and p.end_date 
and sysdate between t.start_thread and t.end_thread  
and c.subs_subs_id = t.subs_subs_id 
and p.subs_subs_id = c.subs_subs_id 

所有行的最大值都相同。它是表格中的最大日期。不确定这是不是你想要的,但这就是我理解你的方式。

答案 2 :(得分:0)

我认为您需要使用analytic functions来获取信息

select distinct c.msisdn, t.volume, adj_date,
max(adj_date) over (partition by c.msisdn, t.volume) as max_adj_date,
6144 - (round(t.volume /1024/1024,2))as остаток_мб 
from 
subscriber_discount_threads t, 
client_balance c, 
subs_packs p 
where 
t.dctr_dctr_id = 1283 
and p.pack_pack_id in (877,874) 
and c.msisdn = '550350057' 
and t.adj_date >= '30.10.2015' 
and sysdate between p.start_date and p.end_date 
and sysdate between t.start_thread and t.end_thread  
and c.subs_subs_id = t.subs_subs_id 
and p.subs_subs_id = c.subs_subs_id