Rumah > Soal Jawab > teks badan
P粉7446912052023-07-26 10:58:55
Anda perlu menggunakan subkueri untuk mencapai ini. Sebenarnya, anda perlu mendapatkan versi rekod maksimum setiap ID janji temu:
SELECT appointment_record_version_number, appointment_id, appointment_pallets AS close_pallets, appointment_units AS close_units FROM b.dh AS t1 WHERE t1.appointment_record_version_number = ( SELECT MAX(appointment_record_version_number) FROM b.dh WHERE b.dh.data = t1.data ) AND last_updated_datetime BETWEEN '2023-06-01' AND '2023-06-30' AND warehouse_id = 'xxx'
Anda juga boleh menggunakan pernyataan JOIN untuk memilih nilai maksimum, yang kadangkala lebih pantas:
SELECT t1.appointment_record_version_number, t1.appointment_id, t1.appointment_pallets AS close_pallets, t1.appointment_units AS close_units FROM b.dh AS t1 LEFT JOIN b.dh AS t2 ON ( t1.appointment_record_version_number = t2.appointment_record_version_number AND t1.appointment_id < t2.appointment_id ) WHERE t2.appointment_record_version_number IS NULL AND last_updated_datetime BETWEEN '2023-06-01' AND '2023-06-30' AND warehouse_id = 'xxx';
Bergantung pada kes penggunaan anda, terutamanya jika pangkalan data anda besar, anda boleh menggunakan subkueri atau indeks tambahan untuk mengoptimumkan permintaan lagi, tetapi ia sudah cukup pantas.