【问题标题】:SQL to build a quarterly table from 3 monthly records without groupingSQL从3个月的记录构建一个季度表而不分组
【发布时间】:2015-01-07 01:05:13
【问题描述】:

我需要在 MS Access2010 中构建一个季度表,其中包含 3 个重复的每月列,例如 Month1_temp、Month2_temp、Month3_temp。我的源数据是一张每月有一条记录的表。因此,我必须阅读 3 个月的记录才能建立一个季度记录。 当我读取每月源记录时,“月份”值(1、2 或 3)将确定将在目标记录中填充的临时列。 目标表中的主键是串联的“Location + Year + Qtr”

示例:
我的每月明细源数据有这样的字段; 首要的关键 地点 年 Qtr(值 1、2、3 或 4) 月份(值 1、2 或 3) 温度

目标表是: 首要的关键 地点 年 季度 Month1_Temp Month2_Temp Month3_Temp

任何帮助将不胜感激 - 我不知道从哪里开始。

【问题讨论】:

    标签: sql insert ms-access-2010


    【解决方案1】:

    您可能需要调整它以与 MS Access 一起使用,但这应该向您展示一种可能的想法:

    INSERT INTO Summarry (Location,Year,Quarter,Month1_Temp,Month2_Temp,Month3_Temp)
    SELECT a.Location,a.Year,a.Quarter,b.NumericValue,c.NumericValue,d.NumericValue
    FROM
    (
    SELECT DISTINCT Location,Year,Quarter
    FROM DetailSource
    ) a 
    
    INNER JOIN 
    
    (
    SELECT Location,Year,Quarter,NumericValue
    FROM DetailSource
    WHERE Month = 1
    ) b ON (a.Location = b.Location AND a.Year = b.Year AND a.Quarter = b.Quarter)
    
    INNER JOIN 
    
    (
    SELECT Location,Year,Quarter,NumericValue
    FROM DetailSource
    WHERE Month = 2
    ) c ON (a.Location = c.Location AND a.Year = c.Year AND a.Quarter = c.Quarter)
    
    INNER JOIN 
    
    (
    SELECT Location,Year,Quarter,NumericValue
    FROM DetailSource
    WHERE Month = 3
    ) d ON (a.Location = d.Location AND a.Year = d.Year AND a.Quarter = d.Quarter)
    
    WHERE a.Location = 'MyLocation' AND a.Year = 2015 AND a.Quarter = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2022-12-18
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多