【问题标题】:SQL GROUP BY total price of every week of the yearSQL GROUP BY 一年中每周的总价
【发布时间】:2015-08-26 11:51:43
【问题描述】:

好的,所以我在这里做一些访问,但我无法弄清楚,已经通过搜索尝试了很多,但没有真正接近。我希望你们中的一些人知道该怎么做。

我的数据库中有一些表(只是一个简单的学校表)

产品表

Type    Price   Color  Height Model
--------------------------------------------
KL47    69      B      150    Dames Normal Model
VRS02   73      W      170    Heren Smal Model`

副本表 - 产品

Copienr Type
------------------------
K664    KL47
T553    KL47
V3553   VRS02
V7744   VRS02
V9941   VRS02

客户表

Customrnr   Companyname  Adress
-----------------------------------------
1           Jiver        Posthoorn 27
2           Veco         Weebosserweg 21
3           V&D          Omabakfietsjes 74

最后还有一张查看女巫客户租用女巫产品的表格

Rentnr  Customernr  Copienr Date-from   Date-Till
-------------------------------------------------------
5       3           K664    11/5/2014   11/29/2014
6       3           T553    10/7/2014   12/13/2014
7       3           V3553   11/18/2014  12/19/2014
8       3           V3553   4/8/2015    8/12/2015
9       3           V7744   4/8/2015    7/8/2015
10      3           T553    5/4/2015    6/8/2015

现在我想要一张包含 V&D 公司今年迄今为止租用的所有产品的表格。 按每周总价分组。

到目前为止我有这个:

SELECT t.typen, c.copienr, date-from, date-till, companyname, price
FROM typen t, copies c, rented-vieuw r, customers c
WHERE bedrijfsnaam = 'V&D'
AND c.customernr = r.customernr
AND r.copienr = c.copienr
AND c.type = t.type
AND date-from >= #1/1/2015#;

此外,为了按饮食周的总价格分组,我尝试了很多。认为这甚至没有任何意义,但我无法弄清楚。 我希望你能帮助我。谢谢!

编辑:产品表中的价格是租用产品的成本/弱***

【问题讨论】:

  • 我删除了mysql标签并将“access”更改为“ms-access”。
  • 啊,谢谢!抱歉,IT 领域的新人(2 月开始上学)

标签: sql ms-access


【解决方案1】:

所以这是我第一次尝试在线回答,

我对您的问题的理解是,您正在尝试仅针对今年的单个公司的产品创建查询。并获得该产品的总价。

我假设您的价格是每周价格。我添加了两列,让您了解我做了什么,总天数和总周数。此外,我根据您提供的拼写和描述将您的表命名为 Products、Copiers - Products、Custumers 和 Renting

SELECT Renting.Rentnr, DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till]) AS NumberDays, DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till])/7 AS NumberWeeks, Custumers.Companyname, Products.Type, Products.Price, Renting.[Date-from], Renting.[Date-Till], Format([Products]![Price]*DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till])/7,"Currency") AS Total
FROM Custumers INNER JOIN ((Products INNER JOIN [Copiers - Products] ON Products.Type = [Copiers - Products].Type) INNER JOIN Renting ON [Copiers - Products].Copienr = Renting.Copienr) ON Custumers.Customrnr = Renting.Customernr
WHERE (((Year([Renting]![Date-from]))=[Which year?]))
GROUP BY Renting.Rentnr, DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till]), DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till])/7, Custumers.Companyname, Products.Type, Products.Price, Renting.[Date-from], Renting.[Date-Till], Format([Products]![Price]*DateDiff("d",[Renting]![Date-from],[Renting]![Date-Till])/7,"Currency");

WHERE 语句用于限制您选择的年份,您可以将[Which year?] 更改为Now() 以根据计算机系统时间/日期自动选择当前年份。

WHERE 语句中添加AND ((Custumers.Companyname)=[Which Company?])) 还允许根据公司名称选择不同的公司

诸如年份和公司之类的硬编码值意味着您的查询只有一个选项,并且您需要针对每一年和公司进行新的查询。

我希望我已经达到目标或接近目标。

编辑:

我意识到我没有正确解释这一切: DateDiff(... 用于获取两个日期之间的差异,“d”表示按天我也可以使用“ww”来询问日期之间的周数,接下来的两个字段是您的日期字段。

所有连接都是自动进行的,因为我在关系窗口中链接了所有表。而且这个查询是在设计视图中设计的,没有使用 SQL 编辑器。

【讨论】:

  • 嗯,我不确定这是否可行,我正在尝试将其添加到我的查询中。也许我没有以正确的方式解释它,我的借口!但我想要一份过去一年(2015 年 1 月 1 日至今)租用的 al de products 公司“V&D”的列表。然后eatch的总价格疲软了v&d租了一些产品。表中给出的价格女巫是出租的价格/弱。
【解决方案2】:

您将需要从第一个日期到最后一个日期的日期周列表。此外,您将需要 ISO 年份,因为这可能与日历年不同。

首先创建一个表Number,其中包含一个字段:Number。将此作为主索引。

用 0 到 9 的 10 条记录填写。

然后创建一个查询DateFirstLast,它将第一个和最后一个日期作为参数:

PARAMETERS 
    DateFirst DateTime, 
    DateLast DateTime;
SELECT 
    DateAdd("d",[Hundred]![Number]*100+[Ten]![Number]*10+[One].[Number],[DateFirst]) AS [Date]
FROM 
    [Number] AS One, 
    [Number] AS Ten, 
    [Number] AS Hundred
WHERE 
    DateAdd("d",[Hundred]![Number]*100+[Ten]![Number]*10+[One].[Number],[DateFirst])<=[DateLast];

现在,在新查询中使用它作为源来创建 ISO 年-周数:

SELECT 
    DateFirstLast.Date, 
    ISO_WeekYearNumber([Date]) AS YearWeek
FROM 
    DateFirstLast;

这将列出日期和日期的年-周数。

例如,对于 2012 年,它将是从 2011W522013W01

计算周数的函数:

Public Function ISO_WeekYearNumber( _
  ByVal datDate As Date, _
  Optional ByRef intYear As Integer, _
  Optional ByRef bytWeek As Byte) _
  As String

' Calculates and returns year and week number for date datDate according to the ISO 8601:1988 standard.
' Optionally returns numeric year and week.
' 1998-2007, Gustav Brock, Cactus Data ApS, CPH.
' May be freely used and distributed.

  Const cbytFirstWeekOfAnyYear  As Byte = 1
  Const cbytLastWeekOfLeapYear  As Byte = 53
  Const cbytMonthJanuary        As Byte = 1
  Const cbytMonthDecember       As Byte = 12
  Const cstrSeparatorYearWeek   As String = "W"

  Dim bytMonth                  As Byte
  Dim bytISOThursday            As Byte
  Dim datLastDayOfYear          As Date

  intYear = Year(datDate)
  bytMonth = Month(datDate)
  bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)

  If bytWeek = cbytLastWeekOfLeapYear Then
    bytISOThursday = Weekday(vbThursday, vbMonday)
    datLastDayOfYear = DateSerial(intYear, cbytMonthDecember, 31)
    If Weekday(datLastDayOfYear, vbMonday) >= bytISOThursday Then
      ' OK, week count of 53 is caused by leap year.
    Else
      ' Correct for Access97/2000+ bug.
      bytWeek = cbytFirstWeekOfAnyYear
    End If
  End If

  ' Adjust year where week number belongs to next or previous year.
  If bytMonth = cbytMonthJanuary Then
    If bytWeek >= cbytLastWeekOfLeapYear - 1 Then
      ' This is an early date of January belonging to the last week of the previous year.
      intYear = intYear - 1
    End If
  ElseIf bytMonth = cbytMonthDecember Then
    If bytWeek = cbytFirstWeekOfAnyYear Then
      ' This is a late date of December belonging to the first week of the next year.
      intYear = intYear + 1
    End If
  End If

  ISO_WeekYearNumber = CStr(intYear) & cstrSeparatorYearWeek & Format(bytWeek, "00")

End Function

您可以加入销售数据并应用您需要的分组的日期和周列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2022-01-05
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多