对同一个表中的多个求和列进行 SQL 外连接

发布于 2024-12-26 04:06:07 字数 1939 浏览 4 评论 0原文

我正在使用一个 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 技术交流群。

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

发布评论

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

评论(2

清醇 2025-01-02 04:06:07
SELECT SA_ID,
SUM(CASE WHEN LAYER = 1 THEN Number_of_Minutes ELSE 0 END) AS LAYER_1, 
SUM(CASE WHEN LAYER = 2 THEN Number_of_Minutes ELSE 0 END) AS LAYER_2,
SUM(CASE WHEN LAYER = 3 THEN Number_of_Minutes ELSE 0 END) AS LAYER_3,
SUM(CASE WHEN LAYER = 4 THEN Number_of_Minutes ELSE 0 END) AS LAYER_4
FROM Appt
GROUP BY SA_ID

编辑:因为它支持 CASE 表达式。

SELECT SA_ID,
SUM(CASE WHEN LAYER = 1 THEN Number_of_Minutes ELSE 0 END) AS LAYER_1, 
SUM(CASE WHEN LAYER = 2 THEN Number_of_Minutes ELSE 0 END) AS LAYER_2,
SUM(CASE WHEN LAYER = 3 THEN Number_of_Minutes ELSE 0 END) AS LAYER_3,
SUM(CASE WHEN LAYER = 4 THEN Number_of_Minutes ELSE 0 END) AS LAYER_4
FROM Appt
GROUP BY SA_ID

EDIT: Because it supports CASE expressions.

债姬 2025-01-02 04:06:07
SELECT
    t.sa_id,
    Layer1=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 1),
    Layer2=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 2),
    Layer3=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 3),
    Layer4=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 4)

FROM
(
    SELECT DISTINCT
        sa_id
    FROM appt
) t
SELECT
    t.sa_id,
    Layer1=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 1),
    Layer2=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 2),
    Layer3=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 3),
    Layer4=(SELECT SUM(Number_of_Minutes) FROM appt WHERE sa_id = t.sa_id AND Layer = 4)

FROM
(
    SELECT DISTINCT
        sa_id
    FROM appt
) t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文