SQL:生成 'computation row'

标签 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) 执行此操作,而且数量相当多。

我确信有一种更优雅的方法可以做到这一点。

附注:

顺便说一句,我的代码序列如下所示:

// 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

最佳答案

我已将带有 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

关于SQL:生成 'computation row',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6059352/

相关文章:

sql - 如何在 SQL 的时区中使用 'America/New_York '

mysql - 使用纯文本的 SQL 更新 MD5 密码

sql - 是否已弃用SQL Server时间戳,以及在替换中使用什么?

sql - 当只有某些行是有效的正则表达式时,Postgres : select using column as regex,

SQL order by on UNION

sql - 如何在 Android 中使用列表适配器显示矩阵光标?

MySQL 让它更快,将成员计数添加到家庭表中

mysql - 如果 且有计数,则选择总和

mysql - 如何显示所有用户定义的变量(MySQL)

sql - MS Access Year(Date() 函数