1

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

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
4

2 回答 2

3

运行 3 个单独的查询。将 SELECT 转换为 INSERT 取决于 RDBMS。对于 SQL Server,它只是INTO newTableName在子句之前添加一个FROM来创建一个新子句,或者INSERT INTO existingTableNameSELECT语句之前添加一个。

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

查询 1

SELECT COUNT(*) AS SUM1
FROM TX

结果

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

查询 2

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

结果

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

查询 3

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

结果

| INDEX1 | INDEX2 | MULT |
--------------------------
|      1 |      1 |  122 |
|      2 |      1 |  144 |
|      3 |      1 |  170 |
|      2 |      2 |  119 |
|      3 |      2 |  141 |
|      3 |      3 |  126 |
于 2012-11-16T02:17:02.800 回答
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 使用您的数据- 请注意,您的数据总和(第二个查询)与您问题中的总和不匹配。我相信这是一个错字。

请注意,我对第三个查询有点懒惰。我没有写出展开式,而是先将桌子展平,然后将其连接起来。

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

于 2012-11-16T02:15:24.330 回答