SQL:制作'计算行'

时间:2011-05-19 13:23:32

标签: sql

我有一张看起来像这样的表

TYPE | A | B | C | ... | Z
one  | 4 | 4 | 4 | ... | 4
two  | 3 | 2 | 2 | ... | 1

我想插入一个带计算的行(第一行减去第二行):

TYPE | A | B | C | ... | Z
one  | 4 | 4 | 4 | ... | 4
two  | 3 | 2 | 2 | ... | 1
delta| 1 | 2 | 2 | ... | 3

我在想一个看起来像

的SQL命令
(select A from table where type=one) - (select A from table where type=two)

下方是,它太长了,我也必须为所有列(A-Z)这样做,这是非常多的。

我确信有更优雅的方式来做这件事。

PS:

我的代码序列看起来像这样:btw:

// I'm inserting the data from a RawTable to a TempTable

INSERT one
INSERT two
INSERT delta
INSERT three
INSERT four
INSERT delta
...
INSERT onehundredone
INSERT onehundredtwo
INSERT delta

4 个答案:

答案 0 :(得分:3)

我已将identity的ID列添加到临时表中。您可以使用它来确定应该对哪些行进行分组。

create table YourTable
(
  ID int identity primary key,
  [TYPE] varchar(20),
  A int,
  B int,
  C int
)

insert into YourTable ([TYPE], A, B, C)
select 'one',   4, 4, 4 union all
select 'two',   3, 2, 2 union all
select 'three', 7, 4, 4 union all
select 'four',  3, 2, 2 union all
select 'five',  8, 4, 4 union all
select 'six',   3, 2, 2


select T.[TYPE], T.A, T.B, T.C
from
  (
    select
      T.ID,
      T.[TYPE],
      T.A,
      T.B,
      T.C
    from YourTable as T  
    union all  
    select
      T2.ID,
      'delta' as [TYPE],
      T1.A-T2.A as A,
      T1.B-T2.B as B,
      T1.C-T2.C as C
    from YourTable as T1
      inner join YourTable as T2
        on T1.ID = T2.ID-1 and
           T2.ID % 2 = 0
  ) as T
order by T.ID, case T.[TYPE] when 'delta' then 1 else 0 end

结果:

TYPE                 A           B           C
-------------------- ----------- ----------- -----------
one                  4           4           4
two                  3           2           2
delta                1           2           2
three                7           4           4
four                 3           2           2
delta                4           2           2
five                 8           4           4
six                  3           2           2
delta                5           2           2

从组中的第一行对列C进行排序:

select T.[TYPE], T.A, T.B, T.C
from
  (
    select
      T1.ID,
      T1.[TYPE],
      case T1.ID % 2 when 1 then T1.C else T2.C end as Sortorder,
      T1.A,
      T1.B,
      T1.C
    from YourTable as T1
      left outer join YourTable as T2
        on T1.ID = T2.ID+1  
    union all  
    select
      T2.ID,
      'delta' as [TYPE],
      T1.C as Sortorder,
      T1.A-T2.A as A,
      T1.B-T2.B as B,
      T1.C-T2.C as C
    from YourTable as T1
      inner join YourTable as T2
        on T1.ID = T2.ID-1 and
           T2.ID % 2 = 0
  ) as T
order by T.Sortorder, T.ID, case T.[TYPE] when 'delta' then 1 else 0 end

答案 1 :(得分:1)

我不知道有什么方法可以“轻松”完成这项工作(即无需指定每一栏),我无法想出任何方法轻松完成,所以我会记录为说它无法完成。容易。

非简单的方法是构建动态代码 - 循环数据库元数据,构建包含语句的字符串,逐列执行所需的例程,然后执行该字符串。你真的想尽可能避免这种情况。

一个快捷方式,如果您只需要构建一个执行此操作的过程或函数(即构建一次运行多次),您可以将列列表复制到电子表格(Excel)中,使用forumlas构建高度重复的语句引用列名,然后将结果复制回来。 (这比解释要简单得多。)

答案 2 :(得分:1)

我不知道你为什么要这样做,但我接近它的方式是:

insert into table
select 'delta', 
t1.a - t2.a, 
t1.b - t2.b
.....
from table t1, 
table t2
where t1.type = 'one'
and t2.type = 'two'

您必须在插入“one”和“two”后立即运行此查询,然后在插入“three”和“four”后重新运行它。令人讨厌的令人讨厌的讨厌。

如果您可以以某种方式重新命名列,或创建数字列,则可以在单个查询中运行它。

答案 3 :(得分:0)

如果将1替换为1,将2替换为2,依此类推,那么也许这个sql可以工作:

INSERT INTO PodMays
SELECT
  "Delta", A.A-B.A, A.B-B.B, A.C-B.C, A.D-B.D, A.E-B.E
FROM
  (
    SELECT TOP 1
      * 
    FROM
      (SELECT TOP 2 * FROM PodMays WHERE Type <> "Delta" ORDER BY Type DESC)
    ORDER BY
      Type ASC
  ) AS A,
  (
    SELECT TOP 1 
      * 
    FROM 
      (SELECT TOP 2 * FROM PodMays WHERE Type <> "Delta" ORDER BY Type DESC)
    ORDER BY 
      Type DESC
  ) AS B