Question

I currently have a temporary table like so

DBName API50 CounterValue
NULL    NULL   1
test1   34.5   NULL
NULL    NULL   2
test1   38.5   NULL

I want a script which will make my temporary table as below

DBName API50 CounterValue
test1   34.5   1
test1   38.5   2
Was it helpful?

Solution

If your table has a primary key, and you always want to associate the CounterValue field with the next field in the table, you can do a self-join:

SELECT t1.DBName, t1.API50, t2.CounterValue
FROM MyTable t1 INNER JOIN MyTable t2 ON t1.PrimaryKey -1 = t2.PrimaryKey
WHERE t1.DBName IS NOT NULL

OTHER TIPS

I like AHiggins answer as it solves the problem in SQL as it is probably expected. But what initially stuck me when I read your question is: Why solve it in SQL at all? It seems like the data is not originally created inside the database but somehow imported. If I am correct - or if exporting the table, editing and re-importing - is an option then you could solve this with a regular expression. All notable text editors can do this for you (I tested this with NotePad++).

If the table is in a text file using a tabulator \t as delimiter you could simply replace all

NULL\tNULL\t(.*)\n(.*)\t(.*)\tNULL

with \2\t\3\t\1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top