I have a table that looks like this:
ID AMID DESC value
-----------------------------------------------------------------
100 type A AMID type A 10
101 type B AMID type B 18
102 type C AMID type C 34
101 null null 4
102 null null 19
103 type D AMID type D 6
103 type E null 7
The table contains around 6 million rows.
Now I want to have the result like this
ID AMID DESC value
-------------------------------------------------------------
100 type A AMID type A 10
101 type B AMID type B 18
102 type C AMID type C 34
101 type B AMID type B 4
102 type C AMID type C 19
103 type D AMID type D 6
103 type E null 7
It has show the same values when the AMID for ID in both rows are equal are one of it is null, if the AMID is different then leave it like that..
Thanks in advance for the help..
Cheers,
Harish
First, let’s set up some proper DDL and sample data:
Instead of using TOP without ORDER BY, which I don’t even know what it means, you can pick exactly one row to use for populating the NULL rows using partition + order. In your limited sample data you only have a max of two rows for any ID, but in case you can have more, this is important (otherwise, like TOP without ORDER BY, you’re going to pull some arbitrary row, making your update less than predictable). This orders by the value in the
AMIDcolumn, but you can change this to pick the top row perIDusing any criteria in the table.Results:
Don’t forget to clean up:
As an aside,
DESCis a horrible name for a column, since it’s a T-SQL keyword, and therefore always has to be escaped with[double quotes].