SQL:具有最大日期

时间:2017-01-17 17:47:46

标签: mysql sql group-by

我有两张桌子:

Table 1:
worker_number, first_name , last_name , clinic , doctor_type

1   BB BB  Z2 ENT
10  CC CC  Z3 ENT
4   DD DD Z4    Orthopedist
5   EE EE Z5Surgeon
8   AA AA Z1 ENT


Table 2:
worker_number, first_name , last_name , clinic , doctor_type, dt_date, patient_id

'1', 'BB', 'BB', 'Z2', 'ENT', '2017-02-01 10:00:00', '1'
'1', 'BB', 'BB', 'Z2', 'ENT', '2017-02-27 15:20:00', '1'
'8', 'AA', 'AA', 'Z1', 'ENT', '2017-02-28 08:40:00', '1'

基本上我想从表1 中获取doctor_type = "A"所排序的所有行表2 dt_date具有相同{{1} }和worker_number

我尝试做类似的事情:

doctor_type

但是我得到了以下输出:

SELECT `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
order by `health`.`table 2`.dt_date desc

除了BB显示两次外,它的效果非常好,我怎样才能分组到最大日期?

出人意料的结果:

8   AA  AA  Z1
1   BB  BB  Z2
10  CC  CC  Z3
1   BB  BB  Z2
3   DD  DD  Z4
5   EE  EE  Z5

4 个答案:

答案 0 :(得分:1)

这会为您的输出添加一个额外的列。如果您不想这样做,可以使用另一个选择查询包装此查询,以仅选择所需的字段。

SELECT `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic, max(`table 2`.dt_date) max_dt_date
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
       ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
group by `table 1`.worker_number, `table 1`.first_name, `table 1`.last_name, `table 1`.clinic
order by max_dt_date desc

答案 1 :(得分:0)

您可以worker_number聚合MAX并根据MIN(或dt_date根据您的需要排序SELECT t1.worker_number, t1.first_name, t1.last_name, t1.clinic FROM `health`.`table 1` t1 LEFT JOIN `health`.`table 2` t2 ON t1.worker_number = t2.worker_number GROUP BY t1.worker_number ORDER BY MAX(t2.dt_date) DESC

试试这个:

Private Sub CommandButton1_Click()
  Dim fd As Office.FileDialog
  Dim wb As Workbook
  Dim ms As Workbook
  Dim Path As String
  Dim i As Integer
  Dim j As Integer

Set ms = ThisWorkbook


Path = "D:\SYSTEM DATA\\EVT.xlsx"

Set wb = Workbooks.Open(Path)

wb.Activate
For i = 2 To 12 Step 1
If wb.Sheets(1).Cells(1, i).Value = "EVT006" Then
j = i
Exit For
End If
Next i

wb.Sheets("Sheet1").Range(wb.Sheets("Sheet1").Cells(3, j), wb.Sheets("Sheet1").Cells(10, j)).Select 'the error line
Selection.Copy


ms.Activate
With ms
Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Transpose:=True
Application.CutCopyMode = False
End With

wb.Close True
End Sub

答案 2 :(得分:0)

如果你没有使用聚合函数,你需要group by子句但是你应该只使用DISTINCT

SELECT DISTINCT 
      `table 1`.worker_number
    , `table 1`.first_name
    , `table 1`.last_name
    , `table 1`.clinic
FROM `health`.`table 1`
LEFT JOIN `health`.`table 2`
ON `health`.`table 1`.worker_number = `health`.`table 2`.worker_number
order by `health`.`table 2`.dt_date desc

答案 3 :(得分:-1)

Select worker_number, first_name , last_name , clinic , doctor_type
from table1 t1
where doctor_type = "A"
Order By (Select dt_date from table2
          where worker_number = t1.worker_number 
             and doctor_type = t1.doctor_type)