在teradata sql 中划分两条记录并打印一条记录
Divide two records in teradata sql and print one record
我需要从 sql 查询中的 table 中划分两条记录
我在 table
中有下一条记录
表 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
字段 3 中日期为 20161101 的字段 1 ÷ 字段 3 中日期为 20161001 的字段 1 - id 为 1 的记录的 1
你能帮忙查询一下以获得所需的结果吗
类似这样。
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
我认为以下解决了问题:
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;
这是使用条件聚合来获取值。
甲骨文
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 |
+----+----------+
天睿
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 |
+----+----------+
我需要从 sql 查询中的 table 中划分两条记录 我在 table
中有下一条记录表 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
字段 3 中日期为 20161101 的字段 1 ÷ 字段 3 中日期为 20161001 的字段 1 - id 为 1 的记录的 1
你能帮忙查询一下以获得所需的结果吗
类似这样。
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
我认为以下解决了问题:
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;
这是使用条件聚合来获取值。
甲骨文
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 |
+----+----------+
天睿
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 |
+----+----------+