0

在 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;

将不胜感激任何帮助或建议。

谢谢

4

1 回答 1

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

一个用于测试的 SQLfiddle

您的更新使事情有些复杂化,这将非现有区域作为零行加入并使用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

另一个 SQLfiddle

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

于 2013-05-13T19:35:29.273 回答