Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL to select certain fields then set a column 'price' depending is there is a match, or a difference 'price' if null

Posted on 2014-07-27
3
Medium Priority
?
204 Views
Last Modified: 2014-07-27
Hi

I need to be able to build a list of address references (UPRN) and cross match with another table looking up the same UPRN .

If there IS a match on the 2nd table then I need to set a price column to be 10.00 otherwise if there is no match (null) I want to set a price of 20.00

This code lists all the UPRN data from t_blpu and then if there is a UPRN match in t_refuse_garden_2014 also lists that in the column next to it.

Really all I want is every c_uprn from t_blpu then a set of prices next to each one depending if there is a match or not.

For info the c_uprn in t_blpu is prefixed '0000' hence the addition.

SELECT     t_blpu.c_uprn, t_refuse_garden_2014.green_uprn
FROM         t_blpu LEFT OUTER JOIN
                      t_refuse_garden_2014 ON t_blpu.c_uprn = '0000' + t_refuse_garden_2014.green_uprn
ORDER BY t_refuse_garden_2014.green_uprn DESC 

Open in new window

0
Comment
Question by:Neil Thompson
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 40222590
Something like this perhaps:
SELECT  b.c_uprn,
        g.green_uprn
	CASE 
		WHEN g.green_uprn IS NOT NULL THEN 10.00
		ELSE 20.00
	END Price
FROM    t_blpu b
        LEFT OUTER JOIN t_refuse_garden_2014 g ON b.c_uprn = '0000' + g.green_uprn
ORDER BY 
	g.green_uprn DESC 

Open in new window

0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 40222606
Thanks Anthony, its giving me this error when I paste it and try to run it. Apologies my SQL is ok at getting things with a select and join but nothing else :(

Error in SELECT clause: expression near 'CASE'.
Unable to parse query text.
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 40222614
Found it, just needed a , before case, thank you so much, saved me days of searching trying to do this :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question