我如何获得最大日期值

时间:2019-03-22 12:31:33

标签: sql

这是我的桌子

Here's my table

学校(schcd)列在不同日期具有不同的值。如何获取上次更新的值?

 select distinct distcd,blkcd,schcd,is_correct_info,entry_date 
 from adhoc_teacher_attendance_prv 
 having max(entry_date) order by schcd

3 个答案:

答案 0 :(得分:0)

您可以尝试使用where和子查询,如果只希望is_correct_info = 1的行添加适当的过滤子句

select distinct 
    distcd,
    blkcd,
    schcd,
    is_correct_info,
    entry_date 
from 
    adhoc_teacher_attendance_prv 
where 
    entry_date = (select max(entry_date) 
                  from  adhoc_teacher_attendance_prv) 
    and is_correct_info = 1
order by 
    schcd

或子查询上的内部联接

select distinct 
    distcd,
    blkcd,
    schcd,
    is_correct_info,
    entry_date 
from 
    adhoc_teacher_attendance_prv 
inner join 
    (select max(entry_date) max_date
     from adhoc_teacher_attendance_prv) t on t.max_date = adhoc_teacher_attendance_prv.entry_date 
                                          and is_correct_info = 1
order by 
    schcd

答案 1 :(得分:0)

您需要group by前三列并获取最后日期:

select 
  distcd,
  blkcd,
  schcd,
  1 is_correct_info,
  max(entry_date) entry_date 
from adhoc_teacher_attendance_prv 
where is_correct_info = 1
group by
  distcd,
  blkcd,
  schcd

答案 2 :(得分:0)

您只需要按entry_date降序排序

Sub RangesToNewMasterWorksheet()

    ' List of Source Row Range Addresses
    Const cRowRanges As String = "I25:K25, I50:K50, I95:K95"
    Const cTarget As String = "Result"  ' Target Worksheet Name
    Const cHead1 As String = "ID"       ' 1st Column Header
    Const cHead2 As String = "Name"     ' 2nd Column Header
    Const cHead As Long = 2             ' Number of First Header Columns
    Const cRange As String = "Rng"      ' Range (Area) String
    Const cColumn As String = "C"       ' Column String
    Const cFirstCell As String = "A1"   ' Target First Cell Range Address

    Dim wb As Workbook    ' Source/Target Workbook
    Dim ws As Worksheet   ' Current Source/Target Worksheet
    Dim rng As Range      ' Current Source/Target Range
    Dim vntT As Variant   ' Target Array
    Dim vntA As Variant   ' Areas Array
    Dim vntR As Variant   ' Range Array
    Dim NoA As Long       ' Number of Areas
    Dim NocA As Long      ' Number of Area Columns (in Target Array)

    Dim i As Long   ' Area Counter
    Dim j As Long   ' Area Column Counter
    Dim k As Long   ' Target Array Row Counter
    Dim m As Long   ' Target Array Column Counter

    ' Speed Up.
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With

    ' Create a reference to ThisWorkbook i.e. the workbook containing this code.
    Set wb = ThisWorkbook

    ' Task: Delete a possibly existing instance of Target Worksheet.

    Application.DisplayAlerts = False
        On Error Resume Next
            wb.Worksheets(cTarget).Delete
        On Error GoTo 0
    Application.DisplayAlerts = True

    ' Handle unexpected error.
    On Error GoTo UnExpected

    ' Task: Calculate size of Target Array.

    ' Create a reference to the 1st worksheet. (Note: Not sheet.)
    For Each ws In wb.Worksheets
        Exit For
    Next
    ' Create a reference to the Source Row Range (in 1st worksheet.
    Set rng = ws.Range(cRowRanges)
    With rng
        NoA = .Areas.Count
        ReDim vntA(1 To NoA)
        ' Calculate Number of Area Columns (NocA).
        For i = 1 To NoA
            With .Areas(i)
                ' Write number of columns of current Area (i) to Areas Array.
                vntA(i) = .Columns.Count
                NocA = NocA + vntA(i)
            End With
        Next
    End With

    ' Resize Target Array.
    '   Rows:     Number of worksheets + 1 for headers.
    '   Columns:  Number of First Header Columns + Number of Area Columns.
    ReDim vntT(1 To wb.Worksheets.Count + 1, 1 To cHead + NocA)

    ' Task: Write 'Head' (headers) to Target Array.

    vntT(1, 1) = cHead1
    vntT(1, 2) = cHead2
    k = cHead
    For i = 1 To NoA
        For j = 1 To vntA(i)
            k = k + 1
            vntT(1, k) = cRange & i & cColumn & j
        Next
    Next

    ' Task Write 'Body' (all except headers) to Target Array.

    k = 1
    For Each ws In wb.Worksheets
        k = k + 1
        vntT(k, 1) = k - 1
        vntT(k, 2) = ws.Name
        Set rng = ws.Range(cRowRanges)
        m = cHead
        For i = 1 To NoA
            vntR = rng.Areas(i)
            For j = 1 To vntA(i)
                m = m + 1
                vntT(k, m) = vntR(1, j)
            Next
        Next
    Next

    ' Task: Copy Target Array to Target Worksheet.

    ' Add new worksheet to first tab (1).
    Set ws = wb.Sheets.Add(Before:=wb.Sheets(1))
    ws.Name = cTarget
    ' Calculate Target Range i.e. resize First Cell Range by size of
    ' Target Array.
    Set rng = ws.Range(cFirstCell).Resize(UBound(vntT), UBound(vntT, 2))
    rng = vntT

    ' Task: Apply Formatting.

    ' Apply formatting to Target Range.
    With rng
        .Columns.AutoFit
        ' Apply formatting to Head (first row).
        With .Resize(1)
            .Interior.ColorIndex = 49
            With .Font
                .ColorIndex = 2
                .Bold = True
            End With
            .BorderAround xlContinuous, xlThin
            .Borders(xlInsideVertical).LineStyle = xlContinuous
        End With
        ' Apply formatting to Body (all except the first row).
        With .Resize(rng.Rows.Count - 1).Offset(1)
            .Interior.ColorIndex = xlColorIndexNone
            With .Font
                .ColorIndex = xlColorIndexAutomatic
                .Bold = False
            End With
            .Borders(xlEdgeLeft).LineStyle = xlContinuous
            .Borders(xlEdgeRight).LineStyle = xlContinuous
            .Borders(xlInsideVertical).LineStyle = xlContinuous
        End With
    End With

    MsgBox "The program finished successfully.", vbInformation, "Success"

ProcedureExit:

    ' Speed Down.
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With

Exit Sub

UnExpected:
    MsgBox "An unexpected error occurred. Error '" & Err.Number & "': " _
            & Err.Description, vbCritical, "Error"
    GoTo ProcedureExit

End Sub

在方法表达式语法中,我将使用.FirstOrDefault(),应该选择top(1)才能仅获取第一个

过滤以用于schcd

select distinct distcd,blkcd,schcd,is_correct_info,entry_date 
 from adhoc_teacher_attendance_prv 
order by entry_date desc
相关问题