【问题标题】:month vs month query (data in the same table)月vs月查询(同表数据)
【发布时间】:2013-09-04 20:38:55
【问题描述】:

我有如下结构的访问表。

我想做一个查询,从本月输入的数据中减去上个月的 VOLUME(SOURCE 字段中数据的月份)。

对于上面的例子,结果应该是这样的:

SOURCE | VERSION | SALES MODEL | DESTINATION | PERIOD | VOLUME |
-------+---------+-------------+-------------+--------+--------|
201309 |   1     |   model 1   |     eu      | 201309 |    -1  |

在表格中,我有更多型号、更多月份和更多来源。我需要一直减去 source 和 source-1 ,并且数据应该与模型、目的地和时期相匹配。 因此,如果我有 3 个来源(而不是上面的两个),它应该返回 201308-201307 和 201309-201308 结果。 可以访问吗?

【问题讨论】:

    标签: sql ms-access join subquery


    【解决方案1】:

    您可以使用表别名将表连接到自身。一旦你知道了这一点,唯一棘手的事情就是弄清楚哪个月份的值紧随其后。

    Select
      t1.source,
      t1.version,
      t1.[sales model],
      t1.destination,
      t1.period,
      t1.volume - t2.volume as volume
    From
      table t1
        inner join
      table t2
        on
          t1.Source = IIf(t2.source Mod 100 = 12, t2.Source + 89, t2.Source + 1) And
          t1.version = t2.version and
          t1.[sales model] = t2.[sales model] and
          t1.destination = t2.destination and
          t1.period = t2.period
    

    编辑 - 修复了 12 月的下个月测试

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2022-01-13
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      相关资源
      最近更新 更多