Skip to content
Advertisement

Create a new column which shows the last enquiry date based on ID using SQL

I have the EnquiryDate and the ID column. I am trying to create the ‘previous_EnquiryDate column.

Previous enquiryDate is based on ID. For each ID, when is the most recent enquiry that came in before the current enquiry.

Example in row 1, this is the first enquiry by this ID, so previous enquiryDate is the same as the EnquiryDate. In row 9, this ID has enquired before on 2/11/2019 so this is the previous enquiryDate.

I just started using SQL recently and have no clue how this can be done or if this can even be done on SQL.

Advertisement

Answer

You don’t say which database you are using so I’ll assume yours supports windows functions; most of them do nowadays.

The following query shows the date you want:

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