2016-12-28 14 views
0

Access 2007で2つのクエリを使用して、5つ以上の子が1つのセットに含まれています。私は1つのクエリを作成するためにSQLでこれらを組み合わせたいと思います。誰かがこの例の構文を示すだろうか?または、同じ結果を得るための簡単な方法がありますか?Access SQLでサブクエリを組み合わせる

qryChildrenOverFiveと呼ばれる最初のクエリは、

SELECT 
tblEventParticipants.EventParticipantsID, 
DateDiff("yyyy", [tblChild.DoB],Date()) AS Age, 
tblChild.DoB 
FROM 
    tblEventParticipants 
    INNER JOIN (tblChild INNER JOIN tblChildParticipant 
    ON tblChild.ChildID = tblChildParticipant.ChildFK) 
    ON tblEventParticipants.EventParticipantsID = 
    tblChildParticipant.EventParticipantFK 
WHERE (((tblEventParticipants.EventParticipantsID)=[CurrentID]) 
    AND ((DateDiff("yyyy",[tblChild.DOB],Date()))>5)); 

サブクエリとして上記を使用する、他では、サブ問題として

SELECT qryChildrenOverFive.EventParticipantsID, 
    Count(qryChildrenOverFive.Age) AS NumOverFives 
FROM qryChildrenOverFive 
GROUP BY qryChildrenOverFive.EventParticipantsID; 

で、なぜSQLへのアクセスはありませんWHERE句の最初のクエリで別名 'Age'を使用しますか?

+0

別名「年齢」が使用されていない年齢を計算するには、このような機能を必要としています。すべてのクエリについて、SQLは、最初にFROM、WHERE、そしてSELECTセクションのみを分析します。詳細はこちら:[link] http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-処理段階 - 命令実行順序/ [/リンク] – KevinP

+0

知っておいてください。ありがとう。 –

答えて

0
DateDiff("yyyy", [tblChild.DoB],Date()) AS Age 

は、暦年の差のみを返します。その後、

Public Function AgeSimple(_ 
    ByVal datDateOfBirth As Date) _ 
    As Integer 

' Returns the difference in full years from datDateOfBirth to current date. 
' 
' Calculates correctly for: 
' leap years 
' dates of 29. February 
' date/time values with embedded time values 
' 
' DateAdd() is used for check for month end of February as it correctly 
' returns Feb. 28. when adding a count of years to dates of Feb. 29. 
' when the resulting year is a common year. 
' After an idea of Markus G. Fischer. 
' 
' 2007-06-26. Cactus Data ApS, CPH. 

    Dim datToday As Date 
    Dim intAge As Integer 
    Dim intYears As Integer 

    datToday = Date 
    ' Find difference in calendar years. 
    intYears = DateDiff("yyyy", datDateOfBirth, datToday) 
    If intYears > 0 Then 
    ' Decrease by 1 if current date is earlier than birthday of current year 
    ' using DateDiff to ignore a time portion of datDateOfBirth. 
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0) 
    End If 

    AgeSimple = intAge 

End Function 

と::クエリのための論理的な実行順序があるので

AgeSimple([tblChild.DoB]) AS Age 
+0

また学ぶ価値がある!ありがとう。 –