4

I am storing logos in a varbinary(max) field. I'm looking for an SQL statement that says,

select logo where customerid=5

but if the logo for a particular customerid is NULL, then,

select logo where customerid=18

which is the default logo. I'd like to do this in a single query if possible. Can this be done? Thanks.

3
  • 1
    Which database type? that will change the answer somewhat Commented Jan 31, 2013 at 21:56
  • It's MSSQL, not sure why my question was altered from "if it's null" to "if that customerid doesn't exist", the customerid does exist but they might not have added a logo, leaving the field null Commented Jan 31, 2013 at 22:06
  • Sorry about the bad edit, I misunderstood the question. Commented Jan 31, 2013 at 22:09

2 Answers 2

4
SELECT COALESCE(b.logo, dflt.logo) AS logo 
FROM mytable dflt
LEFT OUTER JOIN mytable b ON b.customerid=5
WHERE dflt.customerid=18
Sign up to request clarification or add additional context in comments.

5 Comments

I think both of our answers work, but I'd be curious to see which of our answers performs better... on the one hand, I'm skeptical of the join. On the other hand, I like getting to use coalesce and I'm not sure my IN() + ORDER BY CASE would be any better, especially as those don't work well with indexes but your join conditions do.
Also, yours is more portable, you're not having to convert between TOP 1, LIMIT 1, etc.
Yeah I don't have MS SQL Server handy so I can't test which performs better. If customerid is the primary key (or clustered key) then I assume it'll match 1 or 2 rows very efficiently, so either solution will be quick.
This works, what I don't get is how you join tables that don't exist. What makes b and dflt copies of mytable?
The b and dflt are sometimes called table aliases. These are not copies of the table -- think of them more like a "label" for a subset of rows in mytable. By joining these rows, we create a new virtual row which is one from each alias arranged side-by-side. This allows an expression in the SELECT-list to reference one row from each alias.
2
SELECT TOP 1 logo
FROM [Table]
WHERE logo is not null and customerid IN (@CustomerID, 18)
ORDER BY CASE WHEN customerid= 18 THEN 1 ELSE 0 END

Note how I handled the order. I suspect you may have some customers with an ID of <18 and some customers with an id >18, and so you need to be careful here.

3 Comments

This works! Thank you! I just chose this one because I still don't understand joins very well, but thanks to all.
Programming in SQL without understanding joins is like programming in Java or C# or other language without understanding while loops.
I know, I've been staring at your solution for a half hour trying to figure it out, it works too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.