在teradata sql中划分两条记录并打印一条记录

时间:2016-12-16 02:29:37

标签: sql teradata

我需要在sql查询中从表中划分两条记录 我在表格中有下一条记录

表1

Id    field1  field3
1     20      20161001  
1     35      20161101
2     20      20161001
2     30      20161101
5     19      20161001 
5     33      20161101

(请注意,示例中的日期为2016- OCT -01和2016- NOV -01)

我需要一个sql查询来调整下一个结果:

Id   fieldnew
1    0.75
2    0.5
5    0.7368

为了计算“fieldnew”,规则是: Id 1示例

(35 ÷ 20) -1 = 0.75

field1 from field3 with date 20161101÷field1 from field3 with date 20161001 - 1 of record with id 1

你能帮忙查询以获得所需的结果吗

3 个答案:

答案 0 :(得分:0)

像这样。

with
--  start of test data (not part of the solution)
     table1 ( id, field1, field3 ) as (
       select 1, 20, 20161001 from dual union all 
       select 1, 35, 20161101 from dual union all 
       select 2, 20, 20161001 from dual union all 
       select 2, 30, 20161101 from dual union all 
       select 5, 19, 20161001 from dual union all 
       select 5, 33, 20161101 from dual
     ),
--  end of test data (not part of the solution)
     prep ( id, field1, lag_field1, field3 ) as (
       select id, field1, lag(field1) over (partition by id order by field3), field3
       from   table1
       where  field3 in (20161001, 20161101)
     )
select id, round(field1 / lag_field1, 4) - 1 as fieldnew
from   prep
where  field3 = 20161101
;

ID   FIELDNEW
--   --------
 1       0.75
 2        0.5
 5     0.7368

答案 1 :(得分:0)

我认为以下解决了这个问题:

select id,
       (max(case when field3 = '20161101' then field1 end) /
        max(case when field3 = '20161001' then field1 end)
       ) - 1 as fieldnew
from table1 t1
group by id;

这是使用条件聚合来获取值。

答案 2 :(得分:0)

的Oracle

select      id
           ,round
            (
                            min (field1) keep (dense_rank last  order by field3)
                /   nullif (min (field1) keep (dense_rank first order by field3),0)
                -   1   
               ,4
            )      as fieldnew

from        mytable

where       field3 in 
            (
                add_months (trunc (current_date,'mm'),-2)  
               ,add_months (trunc (current_date,'mm'),-1)
            ) 

group by    id
;

select      id
           ,round
            (
                            min (case when field3 = add_months (trunc (current_date,'mm'),-1) then field1 end)
                /   nullif (min (case when field3 = add_months (trunc (current_date,'mm'),-2) then field1 end),0)
                -   1   
               ,4
            )      as fieldnew

from        mytable

where       field3 in 
            (
                add_months (trunc (current_date,'mm'),-2)  
               ,add_months (trunc (current_date,'mm'),-1)
            ) 

group by    id
;
+----+----------+
| ID | FIELDNEW |
+----+----------+
| 1  | 0.75     |
+----+----------+
| 2  | 0.5      |
+----+----------+
| 5  | 0.7368   |
+----+----------+

Teradata的

select          id

               ,1.0000

            *   last_value (field1) over
                (
                    partition by    id
                    order by        field3
                    rows between    unbounded preceding 
                                and unbounded following
                )

            /   nullifzero (field1)

            -   1   as fieldnew

from        mytable

where       field3 = td_month_begin (current_date) - interval '2' month
         or field3 = td_month_begin (current_date) - interval '1' month 

qualify     field3 = min (field3) over (partition by id)
;

select          id

               ,1.0000

            *   min (field1) over
                (
                    partition by    id
                    order by        field3
                    rows between    1 following
                                and 1 following
                )

            /   nullifzero (field1)

            -   1   as fieldnew

where       field3 = td_month_begin (current_date) - interval '2' month
         or field3 = td_month_begin (current_date) - interval '1' month 

from        mytable

qualify     field3 = min (field3) over (partition by id)
;
+----+----------+
| Id | fieldnew |
+----+----------+
| 1  | 0.7500   |
+----+----------+
| 2  | 0.5000   |
+----+----------+
| 5  | 0.7368   |
+----+----------+