Microsoft Access - 调用单独的表来定义查询中的日期范围

时间:2017-08-24 13:38:52

标签: sql ms-access

我是Access的新手,我正在努力学习。

我有一张表来跟踪当天使用的消耗品数量,一天可能有多个条目。例如,它看起来像

   Date    | Consumable x | Consumable y 
'8/24/2017'|     '4'      |     '3'
'8/24/2017'|     '6'      |     '1'

我有第二张桌子来定义我的周。例如,它看起来像

WeekID | WeekCounter | WeekStartDate | WeekEndDate 
 '1'   |  'Week 1'   |   '1/4/2017'  | '1/10/2017'

我想根据周表中定义的范围,创建一个可以汇总每周所有耗材的查询。最终产品应如下所示:

   Week  | Consumable x | Consumable y 
'Week 20'|    '24'      |    '44'
'Week 21'|    '30'      |    '41'

我尝试了许多不同的方法,但对我尝试的任何事情都没有信心。任何帮助将不胜感激!

谢谢

3 个答案:

答案 0 :(得分:1)

你可以这样做:

Select
    WeekCounter As Week,
    Sum([Consumable x]) As TotalX,
    Sum([Consumable y]) As TotalY
From
    TableConsume,
    TableWeek
Where
    TableConsume.Date Between WeekStartDate And WeekEndDate
Group By
    WeekCounter

答案 1 :(得分:0)

您需要使用子查询。这些方面的东西:

SELECT SUM([Consumable x]) AS SumX, SUM([Consumable Y]) AS SumY, Week FROM
(SELECT Table1.[Consumable x], Table1.[Consumable y], Table2.[Week] FROM
table1 inner join table2 on table1.mydate >= table2.WeekStartDate and table1.mydate <= table2.WeekEndDate)
GROUP BY Week

我注意到1月4日到1月10日你有第1周,所以我认为你实际上将WeekEndDate设为23:59:59(即1秒到午夜)。另一种方法是让你的周表与相隔7天的日期,然后将&lt; = table2.WeekEndDate更改为&lt; table2.WeekEndDate。

答案 2 :(得分:0)

您可以使用此方法。就像@Jonathan一样,我使用dDate而不是Date出于同样的原因(保留字)。

这只能在SQL而不是设计视图中查看,因为Access无法代表连接。

SELECT      WeekCounter
            , NZ(SUM(Consumable_X),0) AS C_X
            , NZ(SUM(Consumable_Y),0) AS C_Y
FROM        WeekDefinition LEFT JOIN Consumables ON WeekDefinition.WeekStartDate<=Consumables.dDate AND
                                                    WeekDefinition.WeekEndDate>=Consumables.dDate
GROUP BY    WeekCounter, WeekID
ORDER BY    WeekID  

我不得不质疑你在消耗品表背后的推理:
目前,您在不同领域拥有耗材X和耗材Y.多年来,当您添加Coonsumables A,B,C&amp; Z'每次您必须更新您的表和每个查询以计算新的耗材。

最好将表更改为包含三个字段:Date,Consumable_Type和Consumable_Amount(您可能需要添加另一个字段来创建主键)。
您的记录将包含以下值:
'8/24/17' | 'Consumable X' | '4''8/24/17' | 'Consumable Y' | '3'

您的查询将如下所示:

SELECT      WeekCounter
            , Consumable_Type
            , SUM(Consumable_Amount) AS Total
FROM        WeekDefinition LEFT JOIN Consumables ON WeekDefinition.WeekEndDate>=[Consumables].dDate AND 
                                                    WeekDefinition.WeekStartDate<=[Consumables].dDate
GROUP BY    WeekCounter, WeekID, Consumable_Type
ORDER BY    WeekID

交叉表查询将根据需要返回表:

TRANSFORM   Sum(Total) AS SumOfTotal
SELECT      WeekCounter
FROM        MyQuery
GROUP BY    WeekCounter
PIVOT       Consumable_Type  

我尝试将上述两个查询合并为一个,但无法完成:

TRANSFORM   SUM(Total) AS Total_Consumables
SELECT      WeekCounter, Consumable_Type, Total
FROM
            (
                SELECT      WeekCounter
                            , Consumable_Type
                            , SUM(Consumable_Amount) AS Total
                FROM        WeekDefinition LEFT JOIN Consumables ON WeekDefinition.WeekStartDate<=Consumables.dDate AND
                                                                    WeekDefinition.WeekEndDate>=Consumables.dDate
                GROUP BY    WeekCounter, WeekID, Consumable_Type
                ORDER BY    WeekID  
            ) AS Q1
GROUP BY    WeekCounter, Consumable_Type, Total
PIVOT       Consumable_Type