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
202 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 500 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

623 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