从相同的列获得总和,但在不同的条件下

时间:2017-10-16 06:44:06

标签: sql oracle

样本数据

create table to_delete ( otsn number, dtsn number,total_js number)  
insert into to_delete values (1,5,2) ;  
insert into to_delete values (2,4,3) ;  
insert into to_delete values (3,3,5) ;  
insert into to_delete values (4,2,7) ;  
insert into to_delete values (5,1,10) ;  

我需要根据otsn和dtsn获得total_js总和

SELECT  
    j.otsn,  
    SUM(j.total_js),  
    j1.dtsn,  
    SUM(j1.total_js)  
FROM  
    to_delete j,  
    to_delete j1  
WHERE  
    j.otsn = j1.dtsn  
GROUP BY  
    j.otsn,  
    j1.dtsn; 

但是在实际的表中我没有得到预期的结果,要么得到otsn或dtsn的正确值,要么得到两者都没有。

我想要的输出是

TSN, sum(total_js) of otsn, sum(total_js) of dtsn
1, 2 , 10
2, 3

,7

tsn代表公交站点,otsn代表原点公交站点,dtsn代表公交站点。 所以有一个表格,其中orgin和dest都有总计数。

如果我必须计算当时有多少人进入otsn otsn,count(total_js) 如果我必须计算有多少人从dtsn下车 dtsn,计数(total_js)

因此,在最终输出中,我需要一个带有传入总数和传出总数的TSN。

3 个答案:

答案 0 :(得分:1)

在自我加入之前获取otsn和dtsn的总和:

SQL> create table to_delete ( otsn number, dtsn number,total_js number)  ;
Table created
SQL> insert into to_delete values (1,5,2) ;
1 row inserted
SQL> insert into to_delete values (2,4,3) ;
1 row inserted
SQL> insert into to_delete values (3,3,5) ;
1 row inserted
SQL> insert into to_delete values (4,2,7) ;
1 row inserted
SQL> insert into to_delete values (5,1,10) ;
1 row inserted
SQL> WITH cte_otsn AS
  2   (SELECT otsn,
  3           SUM(total_js) o_total
  4      FROM to_delete
  5     GROUP BY otsn),
  6  cte_dtsn AS
  7   (SELECT dtsn,
  8           SUM(total_js) d_total
  9      FROM to_delete
 10     GROUP BY dtsn)
 11  SELECT d.dtsn,
 12         o.o_total,
 13         d.d_total
 14    FROM cte_otsn o
 15    JOIN cte_dtsn d
 16      ON d.dtsn = o.otsn;
      DTSN    O_TOTAL    D_TOTAL
---------- ---------- ----------
         1          2         10
         2          3          7
         5         10          2
         4          7          3
         3          5          5

SQL> 

更新:

SQL> create table to_delete (OTSN number,DTSN number,OHR number,DHR number,TOTAL_JS number);
Table created
SQL> INSERT INTO to_delete VALUES (1,5,2,3,2);
1 row inserted
SQL> INSERT INTO to_delete VALUES (2,4,2,4,3);
1 row inserted
SQL> INSERT INTO to_delete VALUES (3,3,3,3,5);
1 row inserted
SQL> INSERT INTO to_delete VALUES (4,2,1,4,7);
1 row inserted
SQL> INSERT INTO to_delete VALUES (5,1,4,1,10);
1 row inserted
SQL> WITH cte_data(tsn,hr,total,tsn_type) AS
  2   (SELECT otsn,
  3           ohr,
  4           total_js,
  5           'o'
  6      FROM to_delete
  7    UNION ALL
  8    SELECT dtsn,
  9           dhr,
 10           total_js,
 11           'd'
 12      FROM to_delete)
 13  SELECT tsn, hr, COALESCE(OCNT, 0) AS ocnt, COALESCE(dcnt, 0) AS dcnt
 14    FROM cte_data
 15   PIVOT (SUM(total) FOR tsn_type IN ('o' AS "OCNT", 'd' AS "DCNT"));

       TSN         HR       OCNT       DCNT
---------- ---------- ---------- ----------
         4          1          7          0
         2          4          0          7
         4          4          0          3
         1          2          2          0
         2          2          3          0
         3          3          5          5
         1          1          0         10
         5          4         10          0
         5          3          0          2
9 rows selected

SQL> 

答案 1 :(得分:0)

Oracle解决方案将是..

    SELECT a.otsn, SUM (a.total_js) OTSN_TOTAL, SUM (b.total_js) DTSN_TOTAL
    FROM to_delete a, to_delete b
   WHERE a.otsn = b.dtsn(+)
   GROUP BY a.otsn;

由于 快乐编码:)

答案 2 :(得分:0)

您的真实表可能包含NULL个值且缺少otsn/dtsn值,因为SUM会为您提供不正确的值。如图所示,更好地使用FULL OUTER JOIN并相应处理NULL

SELECT  
    NVL(j.otsn,j1.dtsn),  
    SUM( CASE WHEN j.total_js  IS NULL THEN 0 ELSE j.total_js END ) OTSN_TOTAL,  
    SUM( CASE WHEN j1.total_js IS NULL THEN 0 ELSE j1.total_js END ) DTSN_TOTAL  
FROM  
    to_delete j FULL OUTER JOIN  
    to_delete j1  
ON  
    j.otsn = j1.dtsn  
GROUP BY  
    j.otsn,  
    j1.dtsn;