Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 31st, 2009, 07:38 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default Removing NULL Value From Query Results

Hello All,

I wrote this query but the results aren't what I expected. I have a bunch of NULL values displayed when there are no NULL values in the table.

Here is my query:
Code:
SELECT
(case when l.name = 'Sodium' then l.name end) as na_name, 
(case when l.name = 'Sodium' then l.value end) as na_value,
(case when l.name = 'Sodium' then l.valuedate end) as na_date,
(case when l.name = 'Postassium' then l.name end) as k_name, 
(case when l.name = 'Potassium' then l.value end) as k_value,
(case when l.name = 'Potassium' then l.valuedate end) as k_date,
(case when l.name = 'Chloride' then l.name end) as cl_name, 
(case when l.name = 'Chloride' then l.value end) as cl_value,
(case when l.name = 'Chloride' then l.valuedate end) as cl_date

FROM lab_table l
WHERE l.name in ('Sodium', 'Potassium', 'Chloride')
and l.valuedate between '20090101' and '20090531'
What displays is:
Quote:
na_name na_value na_valuedate k_name k_value k_valuedate cl_name cl_value cl_valuedate
------------------------------------------------------------------------------------------------------------------------
NULL NULL NULL NULL NULL NULL NULL NULL NULL
Sodium 140 20090501 11:00 NULL NULL NULL NULL NULL NULL
NULL NULL NULL Potassium 3.7 20090501 11:00 NULL NULL NULL
NULL NULL NULL NULL NULL NULL Chloride 105 20090501 11:00
I am sorry about the formatting of this. I also just posted one set of three names and values. The three tests, Sodium, Potassium and Chloride, are run at the same time so the ValueDate will always be the same for the group of three.

I would like the name, value and valuedate for all three items to display on one line without all of the null values.

Code:
Sodium  140  20090501 11:00 AM  Potassium  3.7  20090501 11:00 AM  Chloride  105  20090501 11:00 AM
Sodium  141  20090502 11:00 AM  Potassium  4.0  20090502 11:00 AM  Chloride  100  20090502 11:00 AM
Sodium  137  20090503 11:00 AM  Potassium  3.5  20090503 11:00 AM  Chloride  101  20090503 11:00 AM
How can I do this?

Thanks.

Code:

USE TempDB
GO

CREATE TABLE dbo.labtable
(      Name          VARCHAR(50),
       Value         VARCHAR(50),
        Valuedate     DateTime)
INSERT INTO labtable
(Name,Value,Valuedate)
SELECT 'Sodium','140', '2009-05-01' UNION ALL
SELECT 'Potassium','3.7', '2009-05-01' UNION ALL
SELECT 'Chloride','105', '2009-05-01' UNION ALL
SELECT 'Sodium','141', '2009-05-02' UNION ALL
SELECT 'Potassium','4.0', '2009-05-02' UNION ALL
SELECT 'Chloride','100', '2009-05-02' UNION ALL
SELECT 'Sodium','137', '2009-05-03' UNION ALL
SELECT 'Potassium','3.5', '2009-05-03' UNION ALL
SELECT 'Chloride','101', '2009-05-03'
 
Old May 31st, 2009, 07:59 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

You need something to group by or each row will be treated individually, which causes the nulls...

SELECT
CONVERT(CHAR(10),l.ValueDate,120)AS Sample_Date,
MAX(casewhen l.name ='Sodium'then l.name end)as na_name,
MAX(casewhen l.name ='Sodium'then l.value end)as na_value,
MAX(casewhen l.name ='Potassium'then l.name end)as k_name,
MAX(casewhen l.name ='Potassium'then l.value end)as k_value,
MAX(casewhen l.name ='Chloride'then l.name end)as cl_name,
MAX(casewhen l.name ='Chloride'then l.value end)as cl_value
FROM labtable l
WHERE l.name in('Sodium','Potassium','Chloride')
and
l.valuedate between'20090101'and'20090531'
GROUPBY ValueDate

My question would be, why isn't the following good enough so that you don't have columns of repeating data, null or not?

SELECT
CONVERT(CHAR(10),l.ValueDate,120)AS Sample_Date,
MAX(casewhen l.name ='Sodium'then l.value end)as [Sodium(NA)],
MAX(casewhen l.name ='Potassium'then l.value end)as [Potassium(K)],
MAX(casewhen l.name ='Chloride'then l.value end)as [Clorine(CL)]
FROM labtable l
WHERE l.name in('Sodium','Potassium','Chloride')
and
l.valuedate between'20090101'and'20090531'
GROUPBY ValueDate

__________________
--Jeff Moden
The Following User Says Thank You to Jeff Moden For This Useful Post:
eusanpe (May 31st, 2009)
 
Old May 31st, 2009, 08:36 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Honesty..I like your way better. It is much cleaner

Thank you.
 
Old June 1st, 2009, 01:18 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Heh... cool. Just don't mispell Chlorine like I did.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
results shown by query Vince_421 Access 1 February 4th, 2007 12:45 PM
removing Null values from linkcriteria aciras Beginning VB 6 3 August 29th, 2006 01:19 PM
different results were given for the same query madhusrp SQL Server 2000 2 May 9th, 2006 01:54 AM
Crosstab query with no results edubbelaar Access 0 November 22nd, 2004 06:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.