Oracle SQL 中如何获取某列中某些值的总和以及所有行的总和?

问题描述 投票:0回答:1

我有 3 个表,我想在 SSRS 报告中对其进行摘要。我在形成如何执行此操作的存储过程时遇到了一些问题。这是我正在考虑做什么的一个粗略示例。我想循环遍历所有具有与

C.CASE_ID
的前 2 位数字匹配的代码的国家/地区。希望这能给我“01-法国”的格式。

以下是

PERSON.T_PERSON P
表中的列。我在弄清楚如何查找列中众多“红色”或“蓝色”的总和时遇到一些问题。

我的数据库会有这样的东西:

FCB - 红、红、蓝、NA
FCT - 蓝色,蓝色,NA,NA
FCA - 紫色、粉色、NA、粉色

FCB = 婴儿时最喜欢的颜色
FCT = 青少年时最喜欢的颜色
FCA = 成人最喜欢的颜色

FOR X IN (SELECT  M.CDE_KEY , M.DESC COUNTRY
FROM  MASTER.TABLE_VALUES M
WHERE TABLE_NAME IN ('COL1'))
LOOP
SELECT FCB, FCT, FCA FROM PERSON.T_PERSON P, PERSON.T_CASE_PERSON C 
WHERE P.IDN_INDIV = C.IDN_INDIV
AND SUBSTRING((LPAD(C.CASE_ID,6,0),0,2) = X.CDE_KEY;

Cursor - M.CDE_KEY || '-' || M.DESC As COUNTRY, SUM ("Red"), 
SUM("Blue"), SUM("NA") etc..
END LOOP

Image of how my SSRS report should look

sql oracle reporting-services oracle11g ssrs-2008
1个回答
0
投票

假设您的数据可能与此类似:

WITH      --  S a m p l e    D a t a : 
    T_PERSON AS
      ( Select 1 as IDN_INDIV, 'Red' as FCB, 'Blue' as FCT, 'Purple' as FCA From Dual Union All 
        Select 2,              'Red',        'Blue',        'Pink'          From Dual Union All 
        Select 3,              'Blue',       'NA',          'NA'            From Dual Union All 
        Select 4,              'NA',         'Red',         'Pink'          From Dual Union All
        --
        Select 1,              'NA',         'White',       'Pink'          From Dual Union All 
        Select 2,              'Blue',       'Green',       'NA'            From Dual Union All 
        Select 3,              'NA',         'NY',          'Black'         From Dual Union All
        --
        Select 2,              'Red',        'Blue',        'NA'            From Dual Union All 
        Select 3,              'Red',        'NA',          'Green'         From Dual Union All 
        Select 4,              'Brown',      'Green',       'NA'            From Dual Union All
        --
        Select 1,              'Pink',       'Blue',        'Blue'          From Dual Union All 
        Select 3,              'Brown',      'NA',          'NA'            From Dual Union All 
        Select 4,              'NA',         'Red',         'Pink'          From Dual Union All
        --
        Select 1,              'Red',        'Blue',        'Pink'          From Dual Union All 
        Select 2,              'Blue',       'Green',       'NA'            From Dual Union All 
        Select 4,              'White',      'Na',          'Black'         From Dual
      ),
    T_CASE_PERSON AS
      ( Select 1 as IDN_INDIV, '01-aaa' as CASE_ID From Dual Union All 
        Select 2,              '02-bbb'            From Dual Union All 
        Select 3,              '03-ccc'            From Dual Union All
        Select 4,              '04-ddd'            From Dual 
      ), 
    TABLE_VALUES  AS
      ( Select 'COL1' as TABLE_NAME, '01' as CDE_KEY, 'France' as DESCRIPT From Dual Union All
        Select 'COL1',               '02',            'America'            From Dual Union All
        Select 'COL1',               '03',            'India'              From Dual Union All
        Select 'COL1',               '04',            'Wales'              From Dual
      ), 

...一种选择可能是使用 Oracle MODEL 子句来获取通过求和准备的三组中的所有颜色...

    grid AS
      ( Select    *
        From    ( Select     Row_Number() Over(Partition By m.CDE_KEY Order By p.FCB, p.FCT, p.FCA) as RN_COUNTRY,
                             m.CDE_KEY || '-' || m.DESCRIPT as COUNTRY,
                             p.FCB, p.FCT, p.FCA 
                  From       TABLE_VALUES m
                  Inner Join T_CASE_PERSON c ON(m.CDE_KEY = SubStr(c.CASE_ID, 1, 2))
                  Left  Join T_PERSON p ON(p.IDN_INDIV = c.IDN_INDIV)
                  Where      m.TABLE_NAME = 'COL1'
                  Order By   m.CDE_KEY || '-' || m.DESCRIPT
                )
          MODEL    Partition By ( COUNTRY )
                   Dimension By ( RN_COUNTRY, FCB, FCT, FCA )
                   Measures  ( 0 as RED_FCB, 0 as BLUE_FCB, 0 as GREEN_FCB, 0 as PINK_FCB, 0 as OTHER_FCB, 0 as NA_FCB, 
                               0 as RED_FCT, 0 as BLUE_FCT, 0 as GREEN_FCT, 0 as PINK_FCT, 0 as OTHER_FCT, 0 as NA_FCT, 
                               0 as RED_FCA, 0 as BLUE_FCA, 0 as GREEN_FCA, 0 as PINK_FCA, 0 as OTHER_FCA, 0 as NA_FCA )
            RULES ( RED_FCB[ANY, 'Red', ANY, ANY] = 1, 
                    BLUE_FCB[ANY, 'Blue', ANY, ANY] = 1,
                    GREEN_FCB[ANY, 'Green', ANY, ANY] = 1,
                    PINK_FCB[ANY, 'Pink', ANY, ANY] = 1,
                    OTHER_FCB[ANY, ANY, ANY, ANY] = Case When CV(FCB) Not In('Red', 'Blue', 'Green', 'Pink', 'NY') Then 1 Else 0 End,
                    NA_FCB[ANY, 'NA', ANY, ANY] = 1,
                    --
                    RED_FCT[ANY, ANY, 'Red', ANY] = 1, 
                    BLUE_FCT[ANY, ANY, 'Blue', ANY] = 1,
                    GREEN_FCT[ANY, ANY, 'Green', ANY] = 1,
                    PINK_FCT[ANY, ANY, 'Pink', ANY] = 1,
                    OTHER_FCT[ANY, ANY, ANY, ANY] = Case When CV(FCT) Not In('Red', 'Blue', 'Green', 'Pink', 'NY') Then 1 Else 0 End,
                    NA_FCT[ANY, ANY, 'NA', ANY] = 1,
                    --
                    RED_FCA[ANY, ANY, ANY, 'Red'] = 1, 
                    BLUE_FCA[ANY, ANY, ANY, 'Blue'] = 1,
                    GREEN_FCA[ANY, ANY, ANY, 'Green'] = 1,
                    PINK_FCA[ANY, ANY, ANY, 'Pink'] = 1,
                    OTHER_FCA[ANY, ANY, ANY, ANY] = Case When CV(FCA) Not In('Red', 'Blue', 'Green', 'Pink', 'NY') Then 1 Else 0 End,
                    NA_FCA[ANY, ANY, ANY, 'NA'] = 1
                  )
      )

...现在使用 UNION 获取所有国家/地区的总行以及每个国家/地区的行

--    M a i n    S Q L :
  Select     '00-All Countries' as COUNTRY, 
             Sum(RED_FCB) as RED_FCB, Sum(BLUE_FCB) as BLUE_FCB, Sum(GREEN_FCB) as GREEN_FCB, 
             Sum(PINK_FCB) as PINK_FCB, Sum(OTHER_FCB) as OTHER_FCB, Sum(NA_FCB) as NA_FCB,
             --
             Sum(RED_FCT) as RED_FCT, Sum(BLUE_FCT) as BLUE_FCT, Sum(GREEN_FCB) as GREEN_FCT, 
             Sum(PINK_FCT) as PINK_FCT, Sum(OTHER_FCT) as OTHER_FCT, Sum(NA_FCB) as NA_FCT,
             --
             Sum(RED_FCA) as RED_FCA, Sum(BLUE_FCA) as BLUE_FCA, Sum(GREEN_FCA) as GREEN_FCA, 
             Sum(PINK_FCA) as PINK_FCA, Sum(OTHER_FCA) as OTHER_FCA, Sum(NA_FCA) as NA_FCA
  From       grid
UNION ALL
  Select     COUNTRY, 
             Sum(RED_FCB) as RED_FCB, Sum(BLUE_FCB) as BLUE_FCB, Sum(GREEN_FCB) as GREEN_FCB, 
             Sum(PINK_FCB) as PINK_FCB, Sum(OTHER_FCB) as OTHER_FCB, Sum(NA_FCB) as NA_FCB,
             --
             Sum(RED_FCT) as RED_FCT, Sum(BLUE_FCT) as BLUE_FCT, Sum(GREEN_FCB) as GREEN_FCT, 
             Sum(PINK_FCT) as PINK_FCT, Sum(OTHER_FCT) as OTHER_FCT, Sum(NA_FCB) as NA_FCT,
             --
             Sum(RED_FCA) as RED_FCA, Sum(BLUE_FCA) as BLUE_FCA, Sum(GREEN_FCA) as GREEN_FCA, 
             Sum(PINK_FCA) as PINK_FCA, Sum(OTHER_FCA) as OTHER_FCA, Sum(NA_FCA) as NA_FCA
  From       grid
  Group By   COUNTRY
  Order By   COUNTRY
/*      R e s u l t :
COUNTRY             RED_FCB   BLUE_FCB  GREEN_FCB   PINK_FCB  OTHER_FCB     NA_FCB    RED_FCT   BLUE_FCT  GREEN_FCT   PINK_FCT  OTHER_FCT     NA_FCT    RED_FCA   BLUE_FCA  GREEN_FCA   PINK_FCA  OTHER_FCA     NA_FCA
---------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
00-All Countries          5          3          0          1          7          4          2          5          0          0          5          4          0          1          1          5          9          6
01-France                 2          0          0          1          1          1          0          3          0          0          1          1          0          1          0          2          1          0
02-America                2          2          0          0          0          0          0          2          0          0          0          0          0          0          0          1          3          3
03-India                  1          1          0          0          2          1          0          0          0          0          3          1          0          0          1          0          3          2
04-Wales                  0          0          0          0          4          2          2          0          0          0          1          2          0          0          0          2          2          1    */
© www.soinside.com 2019 - 2024. All rights reserved.