基于先前记录(SQL Server)记录的更改的SQL查询表

时间:2014-04-11 00:17:40

标签: sql sql-server

对于我迫在眉睫的问题道歉,但我可能有点过头了...... SQL不是我接受过培训的东西,而且它是一种随时随地学习的方式。我正在尝试查询SQL Server 2008中的现有单个表(在SQL Server的兼容模式下 2000 )。

表结构类似于以下内容:

Table1
+----------+----------+----------+---------+---------+---------+------------+---------+
|   Date   | RecordID | Revision | AttribA | AttribB | AttribC |  AttribD   | AttribE |
+----------+----------+----------+---------+---------+---------+------------+---------+
| 1/1/2014 |        1 |        0 | mammal  | horse   | slimy   | tasty      | Grade 4 |
| 1/1/2014 |        1 |        1 | mammal  | dog     | slimy   | tasty      | Grade 3 |
| 1/1/2014 |        1 |        2 | mammal  | dog     | soft    | tasty      | Grade 2 |
| 1/1/2014 |        1 |        3 | mammal  | dog     | soft    | lick       | Grade 2 |
| 1/1/2014 |        2 |        0 | puppet  | sock    | soft    | less tasty | Grade 9 |
| 1/1/2014 |        2 |        1 | puppet  | sock    | soft    | not tasty  | Grade 9 |
+----------+----------+----------+---------+---------+---------+------------+---------+

RecordIDRevisionNumPrimary Keys。作为审核历史记录的形式,RecordID中可能存在多个Table1修订版。

另外,让我们说AttributeA& AttributeB与A组非正式相关, 属性C& AttributeD与B组相关,AttributeE本身作为C组。

我的任务是编写一个查询,给定RecordID,将返回以下内容:

+----------+----------+----------+---------+---------+---------+
|   Date   | RecordID | Revision | Group A | Group B | Group C |
+----------+----------+----------+---------+---------+---------+
| 1/1/2014 |        1 |        0 |         |         |         |
| 1/1/2014 |        1 |        1 |    X    |         |     X   |
| 1/1/2014 |        1 |        2 |         |    X    |     X   |
| 1/1/2014 |        1 |        3 |         |    X    |         |
+----------+----------+----------+---------+---------+---------+

如果“Group”中的任何列已从上一个值更新,则查询返回单个记录的每个修订的条目,其中包含“X”。

到目前为止,我最好的尝试是设置:

DECLARE  @ID   INTEGER
DECLARE  @Revision  INTEGER
SET @ID = 1
SET @Revision = 3
...
  SET @GroupA = (SELECT COUNT(*) FROM
   (SELECT DISTINCT AttribA, AttribB
    FROM [dbo].[Table1]
    WHERE RecordID = @ID 
      AND RevisionID in (@Revision ,@Revision -1)) A)
... 
SELECT @ID
      ,[RevisionID]
      ,[Date]
      ,[GroupA] =
        CASE 
          WHEN @GroupA = 1 THEN ''
          ELSE 'X'
        END
...
  FROM [dbo].[Table1]
  WHERE RecordID = @ID
    AND RevisionID = @Revision

我已经删除了一些重复的代码来向您展示一般策略,但老实说我认为这是一个完全混乱,我怀疑我能够在不将其变成噩梦的情况下进行补救。任何人都可以告诉我他们如何更好地完成SQL Server 2000兼容性中描述的任务吗?我很感激这里的任何帮助。

2 个答案:

答案 0 :(得分:1)

尝试下面的sql:

<强>已更新 将内连接更改为左连接。 ;)

select a.RecordId,a.Revision
    , Case When a.AttribA +' '+a.AttribB <> ab.[Group A] then 'X' else '' End as 'Group A'
    , Case When a.AttribC +' '+a.AttribD <> ab.[Group B] then 'X' else '' End as 'Group B'
    , Case When a.AttribE <> ab.[Group C] then 'X' else '' End as 'Group C'
    , a.AttribA +' '+a.AttribB as 'Group A'
    , a.AttribC+ ' ' + a.AttribD as 'Group B'
    , a.AttribE as 'Group C'
    , ab.[Group A] as 'PrevRevision A'
    , ab.[Group B] as 'PrevRevision B'
    , ab.[Group C] as 'PrevRevision C'
from @tempData a
left join
(
    select b.RecordId,b.Revision
    , b.AttribA +' '+b.AttribB as 'Group A'
    , b.AttribC+ ' ' + b.AttribD as 'Group B'
    , b.AttribE as 'Group C'
    from @tempData b

) ab on a.RecordId=ab.RecordId and a.Revision = ab.Revision + 1

order by a.RecordId, a.Revision

结果数据:

RecordId    Revision    Group A Group B Group C Group A       Group B         Group C  PrevRevision A   PrevRevision B   PrevRevision C
----------- ----------- ------- ------- ------- ------------- --------------- -------- ---------------- ---------------- --------------- 
1           0                                   mammal horse  slimy tasty     Grade 4  NULL             NULL             NULL
1           1           X               X       mammal dog    slimy tasty     Grade 3  mammal horse     slimy tasty      Grade 4
1           2                   X       X       mammal dog    soft tasty      Grade 2  mammal dog       slimy tasty      Grade 3
1           3                   X               mammal dog    soft lick       Grade 2  mammal dog       soft tasty       Grade 2
2           0                                   puppet sock   soft less tasty Grade 9  NULL             NULL             NULL
2           1                   X               puppet sock   soft not tasty  Grade 9  puppet sock      soft less tasty  Grade 9

答案 1 :(得分:0)

我不知道SQL Server 2000的局限性,但也许这种方法可行:

select 
  RecordID,
  Revision, 
  AttribA,
  AttribB,
  ( select 'X' 
    from t1 as B
    where B.RecordID = A.RecordID
    and   B.Revision = A.revision - 1
    and  (B.AttribA <> A.AttribA or B.AttribB <> A.AttribB)
  ) AS GroupA
from t1 as A
where RecordID = 1
;

这是一个表示它正在运行的小提琴:http://sqlfiddle.com/#!3/b4626e/6