Skip to content
Advertisement

T-SQL – Get customized row when it exists

We have a table where we keep rows with vanilla settings for products, and customized rows per product per customer as they see fit to override. When a customer does not override, the vanilla rows with vanilla settings are selected but when customized rows exists for products and customers, the customized rows must be returned and the vanilla rows thrown out.

I have the sample code and my solution using ROW_NUMBER() to guide the row selection. This ROW_NUMBER seems to make our query run longer so I am asking if anyone see better options to achieve the same results.

Thank you!

Advertisement

Answer

Try using a correlated subquery and union all:

The first gets the clinicid when there is a non-NULL value. The second gets the rest.

Now, very importantly, you want an index on #TAttrib(ItemID, ClinicID) on the able for these queries:

This would also benefit your query.

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