我正在尝试从 Excel 表中按 ID 选择 MAX(QTYSOLDTHISDAY)。但下面的代码不断选择许多具有 MAX 值的记录。我只需要选择一条记录。我试过按ID分组,它没有用。这是我的代码。谢谢
Dept(0) = "01"
Dept(1) = "02"
Dept(2) = "03"
Dept(3) = "04"
Dept(4) = "05"
Dim DeptNum As String
Using cn As New OleDbConnection With {.ConnectionString = String.Format(ConnectionNoHeader6, FileName6)}
Dim dt7 As New DataTable
cn.Open()
For Each DeptNum In Dept
Dim cmd1 As OleDbCommand = New OleDbCommand(
<Text>
SELECT
ID,
ARTNAME,
NETAMOUNTTHISDAY,
QTYSOLDTHISDAY,
AVAILABLESTOCK
FROM [<%= SheetName6 %>$]
WHERE ID = <%= DeptNum %> and QTYSOLDTHISDAY=(select max(QTYSOLDTHISDAY) from [<%= SheetName6 %>$] where ID = <%= DeptNum %> group by ID)
</Text>.Value,
cn
)
dt7.Load(cmd1.ExecuteReader)
For Each row As DataRow In dt7.Rows
Next
DataGridView2.DataSource = dt7
Next DeptNum
End Using