Skip to content
Advertisement

How to match and fill based on first record in SQL?

Currently have this table here:

i have two columns a URL which constantly changes and a url code – I want to take the first URL record and “fill” a new column where the URL_code matches – see outcome below

I would like to take the very first URL in the URL column for each url code, so i want to produce a table such as this:

I have tried joining, case and when statements including LIKE in SQL but has not worked.

The url’s are different – I just want to take the first one

Any ideas?

Advertisement

Answer

For this to work, you need a column that can be used to order the records. I assume that such a column exists and is called id. In MySQL 8.0, you can use window function first_value():

In earlier versions, you could use an inline query:

Demo on DB Fiddle:

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