开发者

partition by is not working with date having timestamp

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 Table:

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

t1 Table:

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

Now here the table t2 1st record is been fetched by a cursor pqr and the above query is run ,so the partition开发者_StackOverflow by is done on the basis of p_id and date ,but the issue is the the date contains timestamp ,so the group by is not working .I need to do group by on the basis of date without timestamp .

How can we handle this situtaion. I am using ORACLE


You could create a view that has the date truncated:

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

Then use that instead of t1 in the main query.

Or an in-line view if you prefer:

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'
)  
...


All dates in Oracle have a time component. Using trunc will allow you to partition by day:

PARTITION BY trunc(your_date)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜