Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Analysis Server and Reporting Services (2005)
 Removing Null Values

Author  Topic 

nokiauk
Starting Member

9 Posts

Posted - 2012-04-12 : 05:10:55
I have written a SQL query which will be used a a temp table as part of another larger query, I wish to however remove all the NULL values first if possible. The query is currently:

SELECT

Loc.PlaceRef,
CASE
WHEN LocAtt.AttributeCode = 'PAPT'
THEN LocAtt.SubAttributeCode
END AS [Apt Size],

CASE
WHEN LocAtt.AttributeCode = 'SLFC'
THEN LocAtt.SubAttributeCode
END AS [Contained Status]

FROM

dbo.vwLocationCurrent AS Loc
JOIN
dbo.vwLocationAttributesCurrent AS LocAtt ON Loc.PlaceRef = LocAtt.PlaceRef

WHERE

Loc.PlaceRef = '004NOR003'
AND
(LocAtt.AttributeCode = 'PAPT' OR
LocAtt.AttributeCode = 'SLFC')

The results are:

PlaceRef Apt Size Contained Status
004NOR003 2P2 NULL
004NOR003 NULL SC

I think this is because there is many attributes against each reference but I only want to show the values I have requested so it looks like:

PlaceRef Apt Size Contained Status
004NOR003 2P2 SC

Is this possible?

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-12 : 07:16:52
What should the output be like if the result of your current query was this, or some other similar combination?

004NOR003 2P2 NULL
004NOR003 XYZ SC
If you have only one valid value for Apt Size and Contained Status for any given PlaceRef, you can use the Max function like this:
SELECT
Loc.PlaceRef,
MAX(CASE
WHEN LocAtt.AttributeCode = 'PAPT' THEN LocAtt.SubAttributeCode
END) AS [Apt Size],
MAX(CASE
WHEN LocAtt.AttributeCode = 'SLFC' THEN LocAtt.SubAttributeCode
END) AS [Contained Status]
FROM
dbo.vwLocationCurrent AS Loc
JOIN dbo.vwLocationAttributesCurrent AS LocAtt
ON Loc.PlaceRef = LocAtt.PlaceRef
WHERE
Loc.PlaceRef = '004NOR003'
AND (
LocAtt.AttributeCode = 'PAPT'
OR LocAtt.AttributeCode = 'SLFC'
)
GROUP BY
Loc.PlaceRef
Go to Top of Page

nokiauk
Starting Member

9 Posts

Posted - 2012-04-12 : 08:07:23
For each PlaceRef there would be an Apt Size and a Contained Status, the nulls appear as there is no Contanied Status value for Apt Size and vice versa.

The values for Apt Size and Contained Status vary depending on the PlaceRef.

In this case I would like the output to be simply on 1 row:

PlaceRef Apt Size Contained Status
004NOR003 2P2 SC


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-12 : 19:38:16
quote:
Originally posted by nokiauk

For each PlaceRef there would be an Apt Size and a Contained Status, the nulls appear as there is no Contanied Status value for Apt Size and vice versa.

The values for Apt Size and Contained Status vary depending on the PlaceRef.

In this case I would like the output to be simply on 1 row:

PlaceRef Apt Size Contained Status
004NOR003 2P2 SC




Doesn't the query I posted earlier do that for you?
Go to Top of Page

nokiauk
Starting Member

9 Posts

Posted - 2012-04-13 : 02:46:02
Yes it does, having now had a chance to try it out.

Thanks for your assistance.

Go to Top of Page
   

- Advertisement -