Skip to content
Advertisement

How do I query SQL for the latest record based on latest Date and Time OR Date_Time columns?

Situation: SerialNumber has duplicate rows and I need to select the latest records and get what is the Status value (either it PASS or FAIL does not matter).

SerialNumber must be check based on the previous StationNumber. Example: I want to get the Status value of the latest record SerialNumber 197601234512345 in Station 2, so value StationNumber = @Station in the command line below is actually “Station 1”

Scenario: SerialNumber can either be pass or fail in Station 1, then SerialNumber exp:197601234512345 is brought forward to Station 2. In Station 2, need to check what latest Status of that SerialNumber exp:197601234512345 has in the previous StationNumber (Station 1).

Example table in the database db:

ID SerialNumber Date Time Date_Time Status StationNumber
213939 197601234512344 2021-04-28 11.53 AM 2021-04-28 11:53:00 PASS Station 1
213940 197601234512345 2021-04-28 11.54 AM 2021-04-28 11:54:00 FAIL Station 1
213941 197601234512345 2021-04-28 12.11 PM 2021-04-28 14:30:00 FAIL Station 2
213942 197601234512345 2021-04-28 12.11 PM 2021-04-28 14:31:00 FAIL Station 2
213943 197601234512344 2021-04-28 12.00 PM 2021-04-28 14:35:00 FAIL Station 3
213944 197601234512344 2021-04-28 12.05 PM 2021-04-28 14:46:00 FAIL Station 3
213945 197601234512344 2021-04-28 12.10 PM 2021-04-28 14:47:00 PASS Station 3

Note: All datatype for the columns are VARCHAR(45)

So far, the SQL commands that I have tried has NO Error, but the MySqlreader does NOT Execute the ExecuteReader() which hold the SQL command. How do I know it? By doing the test display label on Label13.Text in Update 1: C# code, At the front-end system it display till Label13.Text = “Check 3”; then direct gives me Label13.Text = “Bad Output”;

Below here are my five tried sql commands. But none of it works hurm =( :

Update 1: C# code on 2021-04-29

Hope anyone can help and spot the mistake or have a solution. Thank you.

Advertisement

Answer

Solution: my situation kinda relate to situation that happen in enter link description here

so, my new SQL command

I test to insert Serial Number 197601234512345 in Station 3 finally, it gets the latest row from the previous Station (Station 2)

ID SerialNumber Date Time Date_Time Status StationNumber
213942 197601234512345 2021-04-28 12.11 PM 2021-04-28 14:31:00 PASS Station 2

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement