【问题标题】:Compare entries logged monthly比较每月记录的条目
【发布时间】:2017-03-13 18:36:05
【问题描述】:

我有一个记录每月条目的 SQL 表。

条目是 .csv 文本文件的内容。

内容具有重复的多个字段,因此主键是一个复合键,包括名称 (varchar)、说明 (varchar) 和 ReportRan (日期时间)。 记录文件后,它看起来像:

 Name | Description | ReportRan
comp1 | some data   | 2017-01-01
comp1 | more data   | 2017-01-01
comp1 | even more   | 2017-01-01
comp1 | s0me data   | 2017-02-01
comp1 | more data   | 2017-02-01
comp1 | new data    | 2017-02-01

我需要获取 1 月份而不是 2 月份的行。 (第 3 行)

2 月份的行不在 1 月份。 (第6行)

并且在月份之间具有字段更改的行。 (第4行)

【问题讨论】:

  • PK = 名称 + 描述 + 日期?
  • 如何区分“1 月份而不是 2 月份的行”和“月份之间字段发生变化的行”?如果值不同,则根据您对主键的定义,行不同。
  • @McNets 是的,主键是复合键。还有更多字段,但它们不相关并且经常重复。
  • @bbrumm 第一个我想知道报告是否在一月份有条目,但在二月份的报告中没有条目。至于字段更改,您是对的。如果我只记录任何不同的行,那么在接下来的几个月中就足够了。

标签: sql compare sql-server-2014


【解决方案1】:

您可以使用 NOT EXISTS:

Month=1 的行在 Month=2 上不存在

select t1.Name, t1.Description, t1.ReportRan
from   your_table t1
where  month(t1.ReportRan) = 1
and    not exists (select 1
                  from your_table t2
                  where t1.Name = t2.Name
                  and   t1.Description = t2.Description
                  and   month(t2.ReportRan) = 2);

或 Month=1 上不存在的 Month=2 行

select t1.Name, t1.Description, t1.ReportRan
from   your_table t1
where  month(t1.ReportRan) = 2
and    not exists (select 1
                  from your_table t2
                  where t1.Name = t2.Name
                  and   t1.Description = t2.Description
                  and   month(t2.ReportRan) = 1);

到目前为止,为了获取所有更改的行,我使用了 UNION 和以前的查询。

select t1.Name, t1.Description, t1.ReportRan
from   @tbl t1
where  month(t1.ReportRan) = 1
and    not exists (select 1
                  from @tbl t2
                  where t1.Name = t2.Name
                  and   t1.Description = t2.Description
                  and   month(t2.ReportRan) = 2)
UNION
select t1.Name, t1.Description, t1.ReportRan
from   @tbl t1
where  month(t1.ReportRan) = 2
and    not exists (select 1
                  from @tbl t2
                  where t1.Name = t2.Name
                  and   t1.Description = t2.Description
                  and   month(t2.ReportRan) = 1);


|Name |Description|ReportRan          |
|:----|:----------|:------------------|
|comp1|some data  |01/01/2017 00:00:00|
|comp1|even more  |01/01/2017 00:00:00|

|Name |Description|ReportRan          |
|:----|:----------|:------------------|
|comp1|s0me data  |01/02/2017 00:00:00|
|comp1|new data   |01/02/2017 00:00:00|

|Name |Description|ReportRan          |
|:----|:----------|:------------------|
|comp1|even more  |01/01/2017 00:00:00|
|comp1|new data   |01/02/2017 00:00:00|
|comp1|s0me data  |01/02/2017 00:00:00|
|comp1|some data  |01/01/2017 00:00:00|

dbfiddle here

【讨论】:

  • 第二个查询已关闭。在查看文本文件后,我看到了查询捕获的那个,但它仍然错过了 2 月报告中的另一个新条目。子查询是否需要为 reportran 指定 t1 或 t2?
  • ReportRan 是日期还是日期时间字段?
  • 第一个查询根本不适合我。我找到了匹配的行(显然具有不同的日期字段)并删除了二月条目。运行查询 1 并没有返回任何内容。对于第三个查询,描述仅作为示例有所不同。如果名称或任何其他字段不同,我需要记录它。但我认为@bbrumm 让我大开眼界,了解记录整行是可以的,因为它会在新月的报告中属于新行。
  • 只是一个日期字段。
  • 我认为工会更好dbfiddle.uk/…,但我正在尝试寻找一个简单的解决方案
猜你喜欢
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多