如何在单独的表中插入聚合SQL查询的结果

时间:2012-11-16 02:06:49

标签: sql aggregate-functions

我有一个带索引的表,我正在使用sum执行聚合SQL查询 你可以在sqlfiddle中看到我在做什么here

Create table TX (
  i int NOT NULL PRIMARY KEY,
  x1 DECIMAL(7,3), 
  x2 DECIMAL(7,3), 
  x3 DECIMAL(7,3)
);


INSERT INTO TX (i,x1,x2,x3) values
(1,5, 6,6) ;
INSERT INTO TX (i,x1,x2,x3) values
(2,6, 7, 5);
INSERT INTO TX (i,x1,x2,x3) values
(3,5, 6, 7) ;
INSERT INTO TX (i,x1,x2,x3) values
(4,6, 7, 4);

我的问题是如何在3个不同的表中插入该查询的结果?

SELECT SUM(1),
       SUM(x1),SUM(x2),SUM(x3),
       SUM(x1*x1),
       SUM(x2*x1),SUM(x2*x2),
       SUM(x3*x1),SUM(x3*x2),SUM(x3*x3)

FROM TX

所以

我怎样才能得到像

这样的东西
Sum(1)
-----
n

index  Sums
------------
1      4
2      22
3      26

index1  index2   Mult
----------------------
1            1   122
2            1   144
2            2   170
3            1   119
3            2   141
3            3   126

而不是

    SUM(1) SUM(X1) SUM(X2) SUM(X3) SUM(X1*X1) SUM(X2*X1) SUM(X2*X2) SUM(X3*X1) SUM(X3*X2)  SUM(X3*X3)
_____________________________________________________________________________________________________
        4      22       26     22        122        144       170      119          141          126

2 个答案:

答案 0 :(得分:3)

运行3个单独的查询。将SELECT转换为INSERT取决于RDBMS。对于SQL Server,它只是在INTO newTableName子句之前添加FROM来创建新的INSERT INTO existingTableName,或者在SELECT语句之前添加Create table TX ( i int NOT NULL PRIMARY KEY, x1 DECIMAL(7,3), x2 DECIMAL(7,3), x3 DECIMAL(7,3) ); INSERT INTO TX (i,x1,x2,x3) values (1,5, 6,6) ; INSERT INTO TX (i,x1,x2,x3) values (2,6, 7, 5); INSERT INTO TX (i,x1,x2,x3) values (3,5, 6, 7) ; INSERT INTO TX (i,x1,x2,x3) values (4,6, 7, 4);

SELECT COUNT(*) AS SUM1
FROM TX

查询1

| SUM1 |
--------
|    4 |

<强> Results

SELECT SUM(X1) index1, SUM(X2) sums
FROM TX

查询2

| INDEX1 | SUMS |
-----------------
|     22 |   26 |

<强> Results

SELECT x.index1,
       x.index2,
       case x.id
       when 1 then SUM(x1*x1)
       when 2 then SUM(x2*x1)
       when 3 then SUM(x2*x2)
       when 4 then SUM(x3*x1)
       when 5 then SUM(x3*x2)
       when 6 then SUM(x3*x3)
       end Mult
FROM TX
CROSS JOIN
     (select 1 id, 1 index1, 1 index2 union all
      select 2 id, 2 index1, 1 index2 union all
      select 3 id, 3 index1, 1 index2 union all
      select 4 id, 2 index1, 2 index2 union all
      select 5 id, 3 index1, 2 index2 union all
      select 6 id, 3 index1, 3 index2) x
GROUP BY x.id, x.index1, x.index2
ORDER BY x.id

查询3

| INDEX1 | INDEX2 | MULT |
--------------------------
|      1 |      1 |  122 |
|      2 |      1 |  144 |
|      3 |      1 |  170 |
|      2 |      2 |  119 |
|      3 |      2 |  141 |
|      3 |      3 |  126 |

<强> Results

{{1}}

答案 1 :(得分:2)

SELECT SUM(1)
FROM TX;

SELECT 1, SUM(x1)
FROM TX
UNION ALL 
SELECT 2, SUM(x2)
FROM TX
UNION ALL 
SELECT 3, SUM(x3)
FROM TX;

SELECT a.x i1, b.x i2, SUM(a.s * b.s)
FROM
(
    SELECT i, 1 x, x1 s
    FROM TX
    UNION ALL 
    SELECT i, 2 x, x2 s
    FROM TX
    UNION ALL 
    SELECT i, 3 x, x3 s
    FROM TX
) a
INNER JOIN
(
    SELECT i, 1 x, x1 s
    FROM TX
    UNION ALL 
    SELECT i, 2 x, x2 s
    FROM TX
    UNION ALL 
    SELECT i, 3 x, x3 s
    FROM TX
) b ON a.i = b.i AND a.x >= b.x
GROUP BY a.x, b.x;

SQL Fiddle using your data - 请注意,您的数据总和(第二个查询)与您问题中的数据不匹配。我相信这是一个错字。

注意我对第三个查询有点懒惰。我没有写出扩展,而是首先将桌子弄平,并将其自身加入。

另请注意,在第一个查询中,SUM(1)可以替换为COUNT(*)