SQL Server数据透视表与运行总计总数行

时间:2018-07-10 14:12:08

标签: sql-server pivot sql-server-2014 running-total

我有一张要显示的数据透视表。该表具有一个 URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream())); String ip = in.readLine(); try { SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); SSLServerSocket ss = (SSLServerSocket) sslFactory.createServerSocket(PORT); int idSession = 0; while (true) { SSLSocket socket = (SSLSocket)ss.accept(); if(socket.getInetAddress().getHostAddress().equals(ip)){ if (socket != null && !socket.isClosed()) { socket.close(); } } ((ServidorThread) new ServidorThread(socket, idSession)).start(); idSession++; } } catch (IOException ex) { ex.printStackTrace(System.out); Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex); } } category列。我想为每个sub-category添加一个总计行,并在运行计数中添加一个初始值,该初始值来自另一个表,并添加来自特定category的值。

这里有一个完整的工作示例来帮助说明。

设置

sub-category

我当前的CREATE TABLE dbo.rawdata ( MyMonth [NVARCHAR](7) NOT NULL , MyCategory [NVARCHAR](50) NOT NULL , MySubCategory [NVARCHAR](50) NOT NULL , MyCount [INT] NOT NULL ) ON [DEFAULT]; CREATE TABLE dbo.initial ( MyCategory [NVARCHAR](50) NOT NULL , MyStart [INT] NOT NULL ) ON [DEFAULT]; INSERT INTO dbo.rawdata (MyMonth, MyCategory, MySubCategory, MyCount) VALUES ('2018-12', 'three', 'bravo', 7), ('2018-10', 'three', 'echo', 10), ('2018-07', 'four', 'echo', 17), ('2018-12', 'five', 'bravo', 35), ('2018-03', 'three', 'delta', 11), ('2018-03', 'six', 'charlie', 1), ('2018-09', 'five', 'echo', 11), ('2018-12', 'one', 'charlie', 23), ('2018-02', 'five', 'charlie', 36), ('2018-02', 'three', 'delta', 46), ('2018-01', 'two', 'delta', 29), ('2018-02', 'four', 'charlie', 15), ('2018-11', 'one', 'charlie', 25), ('2018-10', 'two', 'bravo', 27), ('2018-05', 'four', 'bravo', 17), ('2018-12', 'five', 'echo', 12), ('2018-05', 'four', 'charlie', 21), ('2018-12', 'one', 'delta', 43), ('2018-12', 'three', 'bravo', 33), ('2018-07', 'two', 'alpha', 32), ('2018-11', 'five', 'delta', 44), ('2018-01', 'six', 'echo', 38), ('2018-08', 'one', 'charlie', 9), ('2018-06', 'three', 'echo', 15), ('2018-08', 'four', 'bravo', 44), ('2018-07', 'six', 'alpha', 50), ('2018-12', 'two', 'echo', 4), ('2018-04', 'six', 'bravo', 40), ('2018-03', 'six', 'delta', 33), ('2018-05', 'five', 'alpha', 11), ('2018-01', 'three', 'echo', 24), ('2018-09', 'five', 'charlie', 10), ('2018-09', 'four', 'delta', 36), ('2018-04', 'two', 'echo', 13), ('2018-02', 'one', 'alpha', 24), ('2018-07', 'one', 'bravo', 2), ('2018-06', 'five', 'echo', 33), ('2018-07', 'five', 'charlie', 46), ('2018-12', 'six', 'bravo', 28), ('2018-10', 'two', 'echo', 10), ('2018-01', 'four', 'delta', 1), ('2018-06', 'three', 'bravo', 25), ('2018-05', 'four', 'charlie', 27), ('2018-04', 'three', 'alpha', 48), ('2018-10', 'three', 'alpha', 8), ('2018-04', 'two', 'delta', 17), ('2018-07', 'five', 'charlie', 2), ('2018-03', 'five', 'alpha', 45), ('2018-08', 'two', 'charlie', 21), ('2018-11', 'three', 'bravo', 32), ('2018-07', 'one', 'echo', 34), ('2018-12', 'one', 'echo', 21), ('2018-08', 'two', 'delta', 8), ('2018-04', 'three', 'delta', 32), ('2018-11', 'five', 'alpha', 23), ('2018-03', 'two', 'echo', 16), ('2018-02', 'six', 'echo', 35), ('2018-11', 'three', 'alpha', 16), ('2018-08', 'four', 'alpha', 40), ('2018-03', 'one', 'echo', 39), ('2018-09', 'one', 'charlie', 22), ('2018-06', 'three', 'bravo', 38), ('2018-02', 'one', 'bravo', 18), ('2018-11', 'four', 'echo', 41), ('2018-12', 'three', 'alpha', 49), ('2018-02', 'six', 'delta', 24), ('2018-09', 'five', 'alpha', 41), ('2018-09', 'six', 'delta', 12), ('2018-04', 'three', 'delta', 15), ('2018-12', 'three', 'delta', 36), ('2018-05', 'five', 'delta', 26), ('2018-01', 'three', 'echo', 22), ('2018-03', 'four', 'delta', 26), ('2018-05', 'three', 'echo', 33), ('2018-07', 'three', 'bravo', 1), ('2018-10', 'four', 'echo', 37), ('2018-01', 'three', 'alpha', 32), ('2018-04', 'two', 'bravo', 2), ('2018-08', 'one', 'bravo', 41), ('2018-03', 'three', 'bravo', 40), ('2018-07', 'three', 'alpha', 38), ('2018-02', 'three', 'bravo', 2), ('2018-11', 'six', 'charlie', 17), ('2018-08', 'three', 'echo', 5), ('2018-02', 'six', 'bravo', 49), ('2018-02', 'one', 'alpha', 9), ('2018-07', 'five', 'charlie', 26), ('2018-05', 'five', 'echo', 7), ('2018-11', 'six', 'bravo', 31), ('2018-08', 'four', 'alpha', 19), ('2018-05', 'one', 'charlie', 30), ('2018-05', 'one', 'echo', 31), ('2018-01', 'four', 'bravo', 31), ('2018-06', 'four', 'alpha', 29), ('2018-10', 'one', 'alpha', 45), ('2018-04', 'two', 'charlie', 41), ('2018-08', 'one', 'delta', 24), ('2018-01', 'five', 'bravo', 27), ('2018-08', 'two', 'charlie', 43), ('2018-02', 'four', 'delta', 19); INSERT INTO dbo.initial (MyCategory, MyStart) VALUES ('five', 9), ('four', 4), ('one', 6), ('six', 6), ('three', 3), ('two', 9); SQL查询:

pivot

PIVOT输出:

SELECT
      MyCategory
    , MySubCategory
    , ISNULL("2018-01", 0) AS "2018-01"
    , ISNULL("2018-02", 0) AS "2018-02"
    , ISNULL("2018-03", 0) AS "2018-03"
    , ISNULL("2018-04", 0) AS "2018-04"
    , ISNULL("2018-05", 0) AS "2018-05"
    , ISNULL("2018-06", 0) AS "2018-06"
    , ISNULL("2018-07", 0) AS "2018-07"
    , ISNULL("2018-08", 0) AS "2018-08"
    , ISNULL("2018-09", 0) AS "2018-09"
    , ISNULL("2018-10", 0) AS "2018-10"
    , ISNULL("2018-11", 0) AS "2018-11"
    , ISNULL("2018-12", 0) AS "2018-12"
FROM
    (SELECT
         MyCategory
         , MySubCategory
         , MyMonth
         , MyCount
     FROM 
         dbo.rawdata) SourceTable
PIVOT
    (SUM(MyCount)
     FOR MyMonth IN ("2018-01", "2018-02", "2018-03", "2018-04", "2018-05", "2018-06", "2018-07", "2018-08", "2018-09", "2018-10", "2018-11", "2018-12")
    ) PivotTable
ORDER BY 
    MyCategory, MySubCategory;

所需的输出:

为每个 +------------+---------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ | MYCATEGORY | MYSUBCATEGORY | JAN-18 | FEB-18 | MAR-18 | APR-18 | MAY-18 | JUN-18 | JUL-18 | AUG-18 | SEP-18 | OCT-18 | NOV-18 | DEC-18 | +------------+---------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ | five | alpha | 0 | 0 | 45 | 0 | 11 | 0 | 0 | 0 | 41 | 0 | 23 | 0 | | five | bravo | 27 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 35 | | five | charlie | 0 | 36 | 0 | 0 | 0 | 0 | 74 | 0 | 10 | 0 | 0 | 0 | | five | delta | 0 | 0 | 0 | 0 | 26 | 0 | 0 | 0 | 0 | 0 | 44 | 0 | | five | echo | 0 | 0 | 0 | 0 | 7 | 33 | 0 | 0 | 11 | 0 | 0 | 12 | | four | alpha | 0 | 0 | 0 | 0 | 0 | 29 | 0 | 59 | 0 | 0 | 0 | 0 | | four | bravo | 31 | 0 | 0 | 0 | 17 | 0 | 0 | 44 | 0 | 0 | 0 | 0 | | four | charlie | 0 | 15 | 0 | 0 | 48 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | four | delta | 1 | 19 | 26 | 0 | 0 | 0 | 0 | 0 | 36 | 0 | 0 | 0 | | four | echo | 0 | 0 | 0 | 0 | 0 | 0 | 17 | 0 | 0 | 37 | 41 | 0 | | one | alpha | 0 | 33 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 45 | 0 | 0 | | one | bravo | 0 | 18 | 0 | 0 | 0 | 0 | 2 | 41 | 0 | 0 | 0 | 0 | | one | charlie | 0 | 0 | 0 | 0 | 30 | 0 | 0 | 9 | 22 | 0 | 25 | 23 | | one | delta | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 24 | 0 | 0 | 0 | 43 | | one | echo | 0 | 0 | 39 | 0 | 31 | 0 | 34 | 0 | 0 | 0 | 0 | 21 | | six | alpha | 0 | 0 | 0 | 0 | 0 | 0 | 50 | 0 | 0 | 0 | 0 | 0 | | six | bravo | 0 | 49 | 0 | 40 | 0 | 0 | 0 | 0 | 0 | 0 | 31 | 28 | | six | charlie | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 17 | 0 | | six | delta | 0 | 24 | 33 | 0 | 0 | 0 | 0 | 0 | 12 | 0 | 0 | 0 | | six | echo | 38 | 35 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | three | alpha | 32 | 0 | 0 | 48 | 0 | 0 | 38 | 0 | 0 | 8 | 16 | 49 | | three | bravo | 0 | 2 | 40 | 0 | 0 | 63 | 1 | 0 | 0 | 0 | 32 | 40 | | three | delta | 0 | 46 | 11 | 47 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 36 | | three | echo | 46 | 0 | 0 | 0 | 33 | 15 | 0 | 5 | 0 | 10 | 0 | 0 | | two | alpha | 0 | 0 | 0 | 0 | 0 | 0 | 32 | 0 | 0 | 0 | 0 | 0 | | two | bravo | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 27 | 0 | 0 | | two | charlie | 0 | 0 | 0 | 41 | 0 | 0 | 0 | 64 | 0 | 0 | 0 | 0 | | two | delta | 29 | 0 | 0 | 17 | 0 | 0 | 0 | 8 | 0 | 0 | 0 | 0 | | two | echo | 0 | 0 | 16 | 13 | 0 | 0 | 0 | 0 | 0 | 10 | 0 | 4 | +------------+---------------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 添加一个总计行。 第一个月月的值将从category表中的初始值加上 dbo.initial alpha的值。下个月的值将取上个月的值加上sub-category alpha。如果sub-category没有category alpha,则使用0。

所需的输出示例:

为了简洁起见,我只显示了几行和几个月,并且出于示例目的,我仅显示了数学。

+------------+---------------+---------+---------+---------+---------+
| MYCATEGORY | MYSUBCATEGORY | INITIAL | JAN-18  | FEB-18  | MAR-18  |
+------------+---------------+---------+---------+---------+---------+
| five       | alpha         |         | 0       | 0       | 45      |
| five       | bravo         | 27      | 0       | 0       |         |
| five       | ...snippit... | ....... | ....... | ....... | ....... |
| five       | total         | 9       | 9+0=9   | 9+0=9   | 9+45=54 |
| three      | alpha         |         | 32      | 0       | 0       |
| three      | delta         | 0       | 46      | 11      | 47      |
| five       | ...snippit... | ....... | ....... | ....... | ....... |
| three      | total         | 3       | 3+32=35 | 35+0=35 | 35+0=35 |
+------------+---------------+---------+---------+---------+---------+

1 个答案:

答案 0 :(得分:0)

您可以尝试一下。先写一个数据透视CTE,然后写UNION ALL CTE结果集和CTE totle结果集。

;WITH CTE AS(
  SELECT  
    MyCategory
    , MySubCategory
    , IIF(MyMonth ='2018-01', MyCount,0) AS 'col01'
    , IIF(MyMonth ='2018-02', MyCount,0) AS 'col02'
    , IIF(MyMonth ='2018-03', MyCount,0) AS 'col03'
    , IIF(MyMonth ='2018-04', MyCount,0) AS 'col04'
    , IIF(MyMonth ='2018-05', MyCount,0) AS 'col05'
    , IIF(MyMonth ='2018-06', MyCount,0) AS 'col06'
    , IIF(MyMonth ='2018-07', MyCount,0) AS 'col07'
    , IIF(MyMonth ='2018-08', MyCount,0) AS 'col08'
    , IIF(MyMonth ='2018-09', MyCount,0) AS 'col09'
    , IIF(MyMonth ='2018-10', MyCount,0) AS 'col10'
    , IIF(MyMonth ='2018-11', MyCount,0) AS 'col11'
    , IIF(MyMonth ='2018-12', MyCount,0) AS 'col12'
  FROM  dbo.rawdata
)
SELECT t.*,i.MyStart
FROM 
(
select 
     MyCategory
    ,MySubCategory
    ,col01  AS '2018-01'
    ,col02  AS '2018-02'
    ,col03  AS '2018-03'
    ,col04  AS '2018-04'
    ,col05  AS '2018-05'
    ,col06  AS '2018-06'
    ,col07  AS '2018-07'
    ,col08  AS '2018-08'
    ,col09  AS '2018-09'
    ,col10  AS '2018-10'
    ,col11  AS '2018-11'
    ,col12  AS '2018-12'
from CTE t
union all 
select MyCategory,
         'totle' as MySubCategory,
          SUM(col01),
          SUM(col01 + col02),
          SUM(col01 + col02+ col03),
          SUM(col01 + col02+ col03 +col04),
          SUM(col01 + col02+ col03 +col04 +col05),
          SUM(col01 + col02+ col03 +col04 +col05 + col06),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07 +col08),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07 +col08 +col09),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07 +col08 +col09 +col10),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07 +col08 +col09 +col10 +col11),
          SUM(col01 + col02+ col03 +col04 +col05 + col06 +col07 +col08 +col09 +col10 +col11+col12)
  from CTE
  GROUP BY MyCategory
) t inner join dbo.initial i on t.MyCategory = i.MyCategory
order by MyCategory,MySubCategory

结果

| MyCategory | MySubCategory | 2018-01 | 2018-02 | 2018-03 | 2018-04 | 2018-05 | 2018-06 | 2018-07 | 2018-08 | 2018-09 | 2018-10 | 2018-11 | 2018-12 | MyStart |
|------------|---------------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
|       five |         alpha |       0 |       0 |       0 |       0 |      11 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |         alpha |       0 |       0 |      45 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      23 |       0 |       9 |
|       five |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      41 |       0 |       0 |       0 |       9 |
|       five |         bravo |      27 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      35 |       9 |
|       five |       charlie |       0 |      36 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      10 |       0 |       0 |       0 |       9 |
|       five |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |      46 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       2 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |      26 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |         delta |       0 |       0 |       0 |       0 |      26 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      44 |       0 |       9 |
|       five |          echo |       0 |       0 |       0 |       0 |       7 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |          echo |       0 |       0 |       0 |       0 |       0 |      33 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|       five |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      12 |       9 |
|       five |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      11 |       0 |       0 |       0 |       9 |
|       five |         totle |      27 |      63 |     108 |     108 |     152 |     185 |     259 |     259 |     321 |     321 |     388 |     435 |       9 |
|       four |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      40 |       0 |       0 |       0 |       0 |       4 |
|       four |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      19 |       0 |       0 |       0 |       0 |       4 |
|       four |         alpha |       0 |       0 |       0 |       0 |       0 |      29 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         bravo |      31 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      44 |       0 |       0 |       0 |       0 |       4 |
|       four |         bravo |       0 |       0 |       0 |       0 |      17 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |       charlie |       0 |       0 |       0 |       0 |      21 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |       charlie |       0 |      15 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |       charlie |       0 |       0 |       0 |       0 |      27 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      36 |       0 |       0 |       0 |       4 |
|       four |         delta |       1 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         delta |       0 |       0 |      26 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         delta |       0 |      19 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      37 |       0 |       0 |       4 |
|       four |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      41 |       0 |       4 |
|       four |          echo |       0 |       0 |       0 |       0 |       0 |       0 |      17 |       0 |       0 |       0 |       0 |       0 |       4 |
|       four |         totle |      32 |      66 |      92 |      92 |     157 |     186 |     203 |     306 |     342 |     379 |     420 |     420 |       4 |
|        one |         alpha |       0 |      24 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      45 |       0 |       0 |       6 |
|        one |         alpha |       0 |       9 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       2 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |         bravo |       0 |      18 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      41 |       0 |       0 |       0 |       0 |       6 |
|        one |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |       0 |       0 |       0 |       0 |       6 |
|        one |       charlie |       0 |       0 |       0 |       0 |      30 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      22 |       0 |       0 |       0 |       6 |
|        one |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      23 |       6 |
|        one |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      25 |       0 |       6 |
|        one |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      43 |       6 |
|        one |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      24 |       0 |       0 |       0 |       0 |       6 |
|        one |          echo |       0 |       0 |       0 |       0 |      31 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |          echo |       0 |       0 |       0 |       0 |       0 |       0 |      34 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      21 |       6 |
|        one |          echo |       0 |       0 |      39 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        one |         totle |       0 |      51 |      90 |      90 |     151 |     151 |     187 |     261 |     283 |     328 |     353 |     440 |       6 |
|        six |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |      50 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         bravo |       0 |       0 |       0 |      40 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      28 |       6 |
|        six |         bravo |       0 |      49 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      31 |       0 |       6 |
|        six |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      17 |       0 |       6 |
|        six |       charlie |       0 |       0 |       1 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         delta |       0 |       0 |      33 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         delta |       0 |      24 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      12 |       0 |       0 |       0 |       6 |
|        six |          echo |       0 |      35 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |          echo |      38 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       6 |
|        six |         totle |      38 |     146 |     180 |     220 |     220 |     220 |     270 |     270 |     282 |     282 |     330 |     358 |       6 |
|      three |         alpha |       0 |       0 |       0 |      48 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       8 |       0 |       0 |       3 |
|      three |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      16 |       0 |       3 |
|      three |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      49 |       3 |
|      three |         alpha |      32 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |      38 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       2 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       0 |      40 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |      25 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       1 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |      38 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      33 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      32 |       0 |       3 |
|      three |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       7 |       3 |
|      three |         delta |       0 |       0 |       0 |      32 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         delta |       0 |       0 |      11 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         delta |       0 |      46 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         delta |       0 |       0 |       0 |      15 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      36 |       3 |
|      three |          echo |      22 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |          echo |       0 |       0 |       0 |       0 |      33 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |          echo |       0 |       0 |       0 |       0 |       0 |      15 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |          echo |      24 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       3 |
|      three |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       5 |       0 |       0 |       0 |       0 |       3 |
|      three |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      10 |       0 |       0 |       3 |
|      three |         totle |      78 |     126 |     177 |     272 |     305 |     383 |     422 |     427 |     427 |     445 |     493 |     618 |       3 |
|        two |         alpha |       0 |       0 |       0 |       0 |       0 |       0 |      32 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |         bravo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      27 |       0 |       0 |       9 |
|        two |         bravo |       0 |       0 |       0 |       2 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |       charlie |       0 |       0 |       0 |      41 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      43 |       0 |       0 |       0 |       0 |       9 |
|        two |       charlie |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      21 |       0 |       0 |       0 |       0 |       9 |
|        two |         delta |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       8 |       0 |       0 |       0 |       0 |       9 |
|        two |         delta |       0 |       0 |       0 |      17 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |         delta |      29 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |          echo |       0 |       0 |      16 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       4 |       9 |
|        two |          echo |       0 |       0 |       0 |      13 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       9 |
|        two |          echo |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |       0 |      10 |       0 |       0 |       9 |
|        two |         totle |      29 |      29 |      45 |     118 |     118 |     118 |     150 |     222 |     222 |     259 |     259 |     263 |       9 |

sqlfiddle