分区依据不适用于具有时间戳的日期

时间:2011-07-13 08:20:41

标签: sql oracle subquery

WITH abc AS
(     
    SELECT p_id    
        ,  t1.amount as amount
        ,  SUM(amount) OVER (PARTITION BY '|| pqr_rec.p_type ||' ) AS sum_amount
    FROM  table1 t1, temp_table tt1t
    WHERE t1.activity_type = 'pqr_rec.p_activity_type'
)     
SELECT p_id
    ,  sum_amount
    ,  amount
FROM abc
WHERE sum_amount > '||pqr_rec.p_amount

t2表:

xyz_id                p_type          p_amount             p_activity_type
============================================================================
p_1                 p_id,date                100000             sell

t1表:

T_id            p_id    amount    date                    p_activity_type
=====================================================================
1               E1      100       1984-10-27 00:02:00       sell
2               E1      200       1984-10-27 00:04:00       sell
3               E1      200       1984-10-27 00:05:00       sell
4               E1      300       1984-10-27 00:06:00       sell
5               E1      100       1984-10-27 00:07:00       sell

现在这里的表t2第一条记录是由游标pqr获取的,上面的查询是运行的,所以分区依据p_id和date完成,但问题是日期包含时间戳,所以group by不工作。我需要在没有时间戳的情况下按日期分组。

我们如何处理这种情况。 我正在使用ORACLE

2 个答案:

答案 0 :(得分:2)

您可以创建截断日期的视图:

create view t1_trunc_date as
select p_id, trunc(date) as date, ...
from table1;

然后在主查询中使用它代替t1。

如果您愿意,可以使用内嵌视图:

WITH abc AS
(     
    SELECT p_id    
        ,  t1.amount as amount
        ,  SUM(amount) OVER (PARTITION BY '|| pqr_rec.p_type ||' ) AS sum_amount
    FROM  (select p_id, trunc(date) as date, ...
           from table1) t1
        , temp_table tt1t
    WHERE t1.activity_type = 'pqr_rec.p_activity_type'
)  
...

答案 1 :(得分:1)

Oracle中的所有日期都有一个时间组件。使用trunc将允许您按天分区:

PARTITION BY trunc(your_date)