SQL 将行乘以另一个 table 的值
SQL multiply row times value from another table
我正在尝试编写一个查询来告诉我 SQL table 中所有项目的成本。我有两个 table 包含我的项目以及不同类型工作所需的小时数和完成年份。
另一个 table 告诉我在哪一年完成什么类型的工作,每小时的成本是多少。
project_table:
project_id | year | H1 | H2 | H3
-----------+------+----+----+---
001 | 2017 | 4 | 2 | 0
002 | 2016 | 3 | 5 | 3
003 | 2018 | 6 | 0 | 6
004 | 2018 | 0 | 1 | 9
cost_table:
Year | hour_type | rate
-----+-----------+------
2016 | h1 | 2
2016 | h2 | 2
2016 | h3 | 1
2017 | h1 | 5
2017 | h2 | 1
2017 | h3 | 2
2018 | h1 | 4
2018 | h2 | 3
2018 | h3 | 6
鉴于这些 table,项目 001 的成本将是
(4 * 5) + (2 * 2) + (0 * 2) = 24 cost
是否有查询可以为每个项目提供此信息?
我想要一个看起来像这样的table
project | cost
--------+-------
001 | 24
002 |...
...
我把你的样本数据变成了 table 所以它非常容易使用。
create table project_table
(
project_id varchar(10)
, ProjectYear int
, H1 int
, H2 int
, H3 int
)
insert project_table values
(001, 2017, 4, 2, 0)
, (002, 2016, 3, 5, 3)
, (003, 2018, 6, 0, 6)
, (004, 2018, 0, 1, 9)
create table cost_table
(
ProjectYear int
, hour_type char(2)
, rate int
)
insert cost_table values
(2016, 'h1', 2)
, (2016, 'h2', 2)
, (2016, 'h3', 1)
, (2017, 'h1', 5)
, (2017, 'h2', 1)
, (2017, 'h3', 2)
, (2018, 'h1', 4)
, (2018, 'h2', 3)
, (2018, 'h3', 6)
现在我们有了样本数据,您可以使用条件聚合轻松解决这个问题。我包括了每种费率类型的值以及包含您想要的值的列,以便您可以看到各个值。
select p.project_id
, max(case when c.hour_type = 'h1' then p.H1 * c.rate end) as H1
, max(case when c.hour_type = 'h2' then p.H2 * c.rate end) as H2
, max(case when c.hour_type = 'h3' then p.H3 * c.rate end) as H3
, MyTotalCost = max(case when c.hour_type = 'h1' then p.H1 * c.rate end) + max(case when c.hour_type = 'h2' then p.H2 * c.rate end) + max(case when c.hour_type = 'h3' then p.H3 * c.rate end)
from project_table p
join cost_table c on c.ProjectYear = p.ProjectYear
group by p.project_id
我正在尝试编写一个查询来告诉我 SQL table 中所有项目的成本。我有两个 table 包含我的项目以及不同类型工作所需的小时数和完成年份。
另一个 table 告诉我在哪一年完成什么类型的工作,每小时的成本是多少。
project_table:
project_id | year | H1 | H2 | H3
-----------+------+----+----+---
001 | 2017 | 4 | 2 | 0
002 | 2016 | 3 | 5 | 3
003 | 2018 | 6 | 0 | 6
004 | 2018 | 0 | 1 | 9
cost_table:
Year | hour_type | rate
-----+-----------+------
2016 | h1 | 2
2016 | h2 | 2
2016 | h3 | 1
2017 | h1 | 5
2017 | h2 | 1
2017 | h3 | 2
2018 | h1 | 4
2018 | h2 | 3
2018 | h3 | 6
鉴于这些 table,项目 001 的成本将是
(4 * 5) + (2 * 2) + (0 * 2) = 24 cost
是否有查询可以为每个项目提供此信息?
我想要一个看起来像这样的table
project | cost
--------+-------
001 | 24
002 |...
...
我把你的样本数据变成了 table 所以它非常容易使用。
create table project_table
(
project_id varchar(10)
, ProjectYear int
, H1 int
, H2 int
, H3 int
)
insert project_table values
(001, 2017, 4, 2, 0)
, (002, 2016, 3, 5, 3)
, (003, 2018, 6, 0, 6)
, (004, 2018, 0, 1, 9)
create table cost_table
(
ProjectYear int
, hour_type char(2)
, rate int
)
insert cost_table values
(2016, 'h1', 2)
, (2016, 'h2', 2)
, (2016, 'h3', 1)
, (2017, 'h1', 5)
, (2017, 'h2', 1)
, (2017, 'h3', 2)
, (2018, 'h1', 4)
, (2018, 'h2', 3)
, (2018, 'h3', 6)
现在我们有了样本数据,您可以使用条件聚合轻松解决这个问题。我包括了每种费率类型的值以及包含您想要的值的列,以便您可以看到各个值。
select p.project_id
, max(case when c.hour_type = 'h1' then p.H1 * c.rate end) as H1
, max(case when c.hour_type = 'h2' then p.H2 * c.rate end) as H2
, max(case when c.hour_type = 'h3' then p.H3 * c.rate end) as H3
, MyTotalCost = max(case when c.hour_type = 'h1' then p.H1 * c.rate end) + max(case when c.hour_type = 'h2' then p.H2 * c.rate end) + max(case when c.hour_type = 'h3' then p.H3 * c.rate end)
from project_table p
join cost_table c on c.ProjectYear = p.ProjectYear
group by p.project_id