根据 Teradata 中表 2 的值更新表 1

发布于 2024-11-29 09:41:45 字数 280 浏览 3 评论 0原文

我有两个像这样的表

在此处输入图像描述

我想在此处从 Table1 插入到 Table2 。这就是我想要的。

取MOU = 10。它有num1和hour1在同一行。 我想将它插入到与 num1 同一行、与 hour1 同一列的单元格中。

我怎么能这么做呢?

免责声明:我在这里不提供任何代码,因为我不确定如何编写此查询。我确实知道写一个简单的更新。我是特拉科塔新手。

I have two tables like this

enter image description here

I would like to insert from Table1 to Table2 here. This is how I want it.

Take MOU = 10. It has num1 and hour1 in the same row.
I would like to insert it into the cell that is at the same row as that of num1 and same column as that of hour1.

How could I do that?

Disclaimer: I am not offering any code here because I am unsure of how to write this query. I sure do know to write a simple update. I am a teracota newbie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-12-06 09:41:45

这有效。

UPDATE a
FROM table2 a, table1 b
SET hour1=b.mou
WHERE a.access_method_id=b.access_method_id
AND hour='hour1'

每个小时都做同样的事情。不是很优雅。但这就是我所能得到的一切。

This worked.

UPDATE a
FROM table2 a, table1 b
SET hour1=b.mou
WHERE a.access_method_id=b.access_method_id
AND hour='hour1'

Did the same for each hours. Not very elegant. But this is all I could get.

我很OK 2024-12-06 09:41:45

下面是一些应该可以完成工作的通用 SQL。

insert into table2(access_method_id, hour1, hour2, ...)
select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1
group by
  access_method_id

Here is some generic SQL that should get the job done.

insert into table2(access_method_id, hour1, hour2, ...)
select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1
group by
  access_method_id
遇见了你 2024-12-06 09:41:45

试试这个!

update table2 t2
from (select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1) t1
set
t2.hour1=t1.hour1,
t2.hour2=t1.hour2,
t2.hour3=t1.hour3,
...etc
where t2.access_method_id=t1.access_method_id;

try this!

update table2 t2
from (select 
  access_method_id, 
  sum(case when hour='HOUR1' then MOU else 0 end) as hour1,
  sum(case when hour='HOUR2' then MOU else 0 end) as hour2,
  ...etc
from
  table1) t1
set
t2.hour1=t1.hour1,
t2.hour2=t1.hour2,
t2.hour3=t1.hour3,
...etc
where t2.access_method_id=t1.access_method_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文