对同一个表中的多个求和列进行 SQL 外连接
我正在使用一个 SQL 库(编程库,符合 ANSI SQL 标准,称为 ABS 数据库,(http://www.componentace.com/help/absdb_manual/absdbmanual_content.htm)。它支持各种联接、CASE 等。 请
我有一个表(appt),它是一个约会表。
ApptKey - Integer - Primary Key
SA_ID - Integer (a person’s ID, think employee number),
Layer - Integer – only 4 different values, 1, 2, 3 or 4 (think Appt Category)
Number_Of_Minutes - Integer – the length of the appointment
注意,某些 SA_ID/层组合可能为空(即该组合没有行)。组合可能有多行(我没有显示其他列,因为它们在这里不相关,我需要一份报告来显示每个人(SA_ID)、每一层、一行的分钟总和……即每个 SA_ID 有他们自己的行,显示 Layer =1 的总和、Layer =2 的总和、Layer =3 的总和以及 Layer = 4 的总和。
示例数据…
SA_ID LAYER Number_of_Minutes
1 1 10
1 1 30
2 1 10
3 2 10
1 4 10
我需要一个给出此结果的查询,
SA_ID LAYER_1 LAYER_2 LAYER_3 LAYER_4
1 40 0 0 10
2 10 0 0 0
3 0 10 0 0
我需要此类型 。格式,因为此查询提供图表/图形组件...
我认为我需要使用多个相关子查询的右连接,但我无法让它工作,这是我下面运行的正确方法,但作为直接连接。我可以(或者如何)重写它以使用右外连接?右外连接是最好的方法吗?
select g.sa_id, l1.totalsum, l2.totalsum, l3.totalsum, l4.totalsum
from (select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 1 group by sa_id, layer) l1,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 2 group by sa_id, layer) l2,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 3 group by sa_id, layer) l3,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 4 group by sa_id, layer) l4,
(select distinct sa_id "sa_id" from appt) g
where
(l1.sa_id = g.sa_id)
and (l2.sa_id = g.sa_id)
and (l3.sa_id = g.sa_id)
and (l4.sa_id = g.sa_id)
I am working with a SQL Library (Programming library, ANSI SQL Compliant, called ABS Database,(http://www.componentace.com/help/absdb_manual/absdbmanual_content.htm). It supports the various joins, CASE, etc...
I have a single table (appt), which is a table of appointments.
ApptKey - Integer - Primary Key
SA_ID - Integer (a person’s ID, think employee number),
Layer - Integer – only 4 different values, 1, 2, 3 or 4 (think Appt Category)
Number_Of_Minutes - Integer – the length of the appointment
Note that some SA_ID/Layer combinations may be empty (i.e. no rows for that combination). Other SA_ID/Layer combinations may have multiple rows (I am not showing other columns as they are not relevant here). I need a report to show the sum of minutes, for each person (SA_ID), for each layer, in one row… i.e. each SA_ID has their own row, showing the sum for Layer =1, the sum for layer =2 , the sum for Layer =3, and the sum for layer = 4.
Example data…
SA_ID LAYER Number_of_Minutes
1 1 10
1 1 30
2 1 10
3 2 10
1 4 10
I need a query that gives this result.
SA_ID LAYER_1 LAYER_2 LAYER_3 LAYER_4
1 40 0 0 10
2 10 0 0 0
3 0 10 0 0
I need this type of format, because this query feeds a Chart/graph component…
I think I need to use a right join a multiple correlated subqueries, but I just can’t get it to work. Is this the right approach. What I have below runs, but as a straight join. Can I (or how do I) rewrite this to use a right outer join? Is a right outer join the best approach?
select g.sa_id, l1.totalsum, l2.totalsum, l3.totalsum, l4.totalsum
from (select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 1 group by sa_id, layer) l1,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 2 group by sa_id, layer) l2,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 3 group by sa_id, layer) l3,
(select sa_id, layer, sum(number_of_minutes) totalsum from appt where layer = 4 group by sa_id, layer) l4,
(select distinct sa_id "sa_id" from appt) g
where
(l1.sa_id = g.sa_id)
and (l2.sa_id = g.sa_id)
and (l3.sa_id = g.sa_id)
and (l4.sa_id = g.sa_id)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:因为它支持
CASE
表达式。EDIT: Because it supports
CASE
expressions.