如何计算单个查询中多列的各个总和

时间:2013-05-13 19:31:05

标签: sql oracle

在Windows 7客户端上使用Oracle 11gR2。我有以下示例表:

Yr     Mnth       Region    City            Handled_Package     Expected_Missing_Package      Actual_Missing_Package

2012   November   Western   San Fransisco   200                 10                            5
2012   November   Western   Los Angeles     400                 20                            15
2012   November   Eastern   New York        300                 15                            20
2012   November   Western   Seattle         100                 5                             7
2012   November   Eastern   Philadelphia    200                 10                            12
2012   November   MidWest   Chicago         300                 15                            15

2012   December   MidWest   Detroit         50                  3                             2
2012   December   Western   Los Angeles     300                 15                            19
2012   December   Eastern   New York        400                 20                            16
2012   December   Eastern   Philadelphia    150                 8                             6
2012   December   MidWest   Chicago         200                 10                            10

2013   January    Eastern   New York        200                 10                            9
2013   January    MidWest   Chicago         100                 5                             7
2013   January    Western   San Fransisco   500                 25                            22
2013   January    Western   Los Angeles     200                 10                            8
2013   January    Europe    London          500                 25                            27
2013   January    Europe    Paris           400                 20                            23

2013   Februray   Europe    London          200                 10                            9
2013   Februray   Eastern   Philadelphia    300                 15                            13
2013   Februray   Eastern   NY              400                 20                            18
2013   February   Western   San Fransisco   400                 20                            12
2013   Februray   Western   LA              200                 10                            8
2013   Februray   MidWest   Detroit         300                 15                            13

我想从中查看将存储所有NUMBER列的总和的视图(即Handled_Package,Expected_Missing_Package,Actual_Missing_Package) 每个地点每年的每个月。即输出应如下所示:

Yr      Mnth        Region      Handled_Package   Expected_Missing_Package  Actual_Missing_Package

2012    November    Eastern     500               25                        32
2012    November    Europe      0                 0                         0
2012    November    MidWest     300               15                        15
2012    November    Western     700               35                        27

2012    December    Eastern     250               13                        12
2012    November    Europe      0                 0                         0
2012    December    MidWest     550               28                        22
2012    December    Western     300               15                        19

2013    January     Eastern     200               10                        9
2013    January     Europe      900               45                        50
2013    January     MidWest     100               5                         7
2013    January     Western     700               35                        30

2013    February    Eastern     700               35                        31
2013    February    Europe      200               10                        9
2013    February    MidWest     300               15                        13
2013    February    Western     600               30                        20

我该怎么做?为方便起见,这里是创建和插入脚本:

CREATE TABLE test
(
    Yr                         VARCHAR2(5),
    Mnth                       VARCHAR2(50),
    Region                     VARCHAR2(50), 
    CITY                       VARCHAR2(50),              
    Handled_Package            NUMBER,        
    Expected_Missing_Package   NUMBER,    
    Actual_Missing_Package     NUMBER
);



Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'Western', 'San Fransisco', 200, 10, 5);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'Western', 'Los Angeles', 400, 20, 15);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'Eastern', 'New York', 300, 15, 20);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'Western', 'Seattle', 100, 5, 7);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'Eastern', 'Philadelphia', 200, 10, 12);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'November', 'MidWest', 'Chicago', 300, 15, 15);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'December', 'MidWest', 'Detroit', 50, 3, 2);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'December', 'Western', 'Los Angeles', 300, 15, 19);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'December', 'Eastern', 'New York', 400, 20, 16);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'December', 'Eastern', 'Philadelphia', 150, 8, 6);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2012', 'December', 'MidWest', 'Chicago', 200, 10, 10);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'Eastern', 'New York', 200, 10, 9);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'MidWest', 'Chicagio', 100, 5, 7);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'Western', 'San Fransisco', 500, 25, 22);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'Western', 'Los Angeles', 200, 10, 8);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'Europe', 'London', 500, 25, 27);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'January', 'Europe', 'Paris', 400, 20, 23);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'Europe', 'London', 200, 10, 9);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'Eastern', 'Philadelphia', 300, 15, 13);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'Eastern', 'NY', 400, 20, 18);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'Western', 'San Fransisco', 400, 20, 12);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'Western', 'LA', 200, 10, 8);

Insert into TEST (YR, MNTH, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
Values ('2013', 'Februray', 'MidWest', 'Detroit', 300, 15, 13);

COMMIT;

感谢任何帮助或建议。

由于

1 个答案:

答案 0 :(得分:1)

看起来像一个简单的GROUP BY;

SELECT Yr,Mnth,Region,
   SUM(Handled_Package) Handled_Package,
   SUM(Expected_Missing_Package) Expected_Missing_Package,
   SUM(Actual_Missing_Package) Actual_Missing_Package
FROM TEST
GROUP BY Yr,Mnth,Region
ORDER BY Yr,Mnth,Region

An SQLfiddle to test with

您的更新会使某些事情变得复杂,这会将非现有区域连接为零行,并使用OVER (PARTITION BY)代替GROUP BY;

SELECT DISTINCT r.Yr,r.Mnth,r.Region,
   COALESCE(SUM(t.Handled_Package)
   OVER (PARTITION BY r.Yr,r.Mnth,r.Region), 0) Handled_Package,
   COALESCE(SUM(t.Expected_Missing_Package) 
   OVER (PARTITION BY r.Yr,r.Mnth,r.Region), 0) Expected_Missing_Package,
   COALESCE(SUM(t.Actual_Missing_Package) 
   OVER (PARTITION BY r.Yr,r.Mnth,r.Region), 0) Actual_Missing_Package 
FROM TEST t
RIGHT JOIN (
  SELECT DISTINCT t1.Region,t2.Yr,t2.Mnth FROM TEST t1 CROSS JOIN TEST t2
) r
ON t.Yr = r.Yr AND t.Mnth=r.Mnth AND t.Region=r.Region
ORDER BY r.Yr,r.Mnth,r.Region

Another SQLfiddle

后者可能会简化,我必须承认Oracle的分析功能不是我的专长:)