【问题标题】:partition by is not working with date having timestamppartition by 不适用于具有时间戳的日期
【发布时间】:2011-07-13 08:20:40
【问题描述】:
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 和日期完成的,但问题是日期包含时间戳,所以group by 不起作用。我需要根据没有时间戳的日期进行 group by。

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

【问题讨论】:

    标签: sql oracle subquery


    【解决方案1】:

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

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

    【讨论】:

    • :除了使用视图还有其他选择吗?
    【解决方案2】:

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

    PARTITION BY trunc(your_date)
    

    【讨论】:

    • 不可能,我不知道哪一列来自表 t2,我们不会使用 p_type 作为 p1,trunc(date)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多