2016-12-22 10 views
0

アクセスデータベースには、マシンパークのlogDataが含まれています。管理目的のために、私はExcelを使用して生成されたいくつかの図が必要です。データは、VBAクエリを使用してAccessから取得されます。ここまでは順調ですね。 dateStampに加えて、週番号も格納されます(Access/ExcelにはISO週番号に関する問題があるため)。ただし、2016年1月のデータを抽出すると、1〜4週だけでなく、2016年の53週も同様に含まれます。ORDER BY on timeStamp Acces VBA/Excel

"Your query does not include the specified expression Format$(logData.dateStamp,'yyyy/mm') as part of an aggregate function."

以下のSQLクエリでは:私は53で、1、2、3、4 この最後のステップは、エラーで失敗し、それをソートしたいなAVGなど

TRANSFORM sum((logData.hoursDay+logData.hoursNight)*60) 
SELECT reasons.reason FROM reasons 
INNER JOIN (logData INNER JOIN testRigs ON logData.machine = machines.ID) ON reasons.ID = logData.reason 
WHERE Format$(logData.dateStamp,'mm') = 1 
AND machines.type = "A" 
GROUP BY reasons.reason 
ORDER BY Format$(logData.dateStamp,'yyyy/mm') DESC 
PIVOT logData.week; 

集計関数( )、COUNT()はSELECTステートメント内にあることができますが、この列は必要ありません。

どのようにヒント週を正しくソートするためのヒント?

+0

および週。以下の関数を参照してください。 – Gustav

答えて

0

あなたは週番号を保存する必要はありませんが、特定の日付の場合、正しいソートを得るために年と週の両方を見つける必要があります。

この機能があることを行います。あなたは_group By_セクションにその式を持つことができない場合、あなたはそれと_order By_セクションと_Pivot_ない週ではなくyear-を削除する必要があります

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