2016-11-07 12 views
0

SQL言語を使用するMS ACCESS 2016のクエリで助けが必要です。 私は20人の雇用者を持つテーブルを持っています。テーブルには名前、姓、性別、生年月日、雇用日と支払いが含まれています。 私は特定の年齢を計算する必要があります。つまり、私は雇用者の25歳の人を知りたいのですが、SQL言語でコマンドを書く方法を知っていません。誰かが助けてくれれば非常に感謝します。 。MS ACCESSのSQL言語2016

+2

あなたが既に達成したことを教えてください! – duDE

+0

サンプルテーブルのデータと期待される結果を追加します。 (フォーマットされたテキストと同じように) – jarlh

+0

質問を書いたければ、始める場所は言語を学ぶことです。チュートリアル、ビデオ、書籍、ドキュメンテーション、サンプル、クラス、オンラインチュートリアルは、人々がSQLやその他のコンピュータ言語を学ぶのに役立ちます。 –

答えて

0

単純な方法でSQLで100%正しい年齢を直接計算することはできません。

したがって、このようなUDF(ユーザ定義関数)を使用:

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 

コピーおよび空のモジュールにコードを貼り付けます。もちろん、

Select *, AgeSimple([DateOfBirth]) As Age 
From YourTable 
Where AgeSimple([DateOfBirth]) >= 25 

あなたのものと、テーブル名やフィールド名を置き換える:

は、次にクエリを実行します。