Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 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