473,414 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Returning # of columns with non-null value

Using SQL2000. I want to return the # of columns with non-null
values. Here's my query so far:

select
case when Dx1 is not null then 0 else 1 end +
case when Dx2 is not null then 0 else 1 end +
case when Dx3 is not null then 0 else 1 end +
case when Dx4 is not null then 0 else 1 end as DxCount
from tblClerkshipDataClean
where PalmName = @PalmName

There are 7 rows for the particular PalmName I'm using. The query
returns a result of 7. However, there are 25 values in Dx1 thru Dx4
so the query should be returning 25.

What am I doing wrong here? Thanks in advance.
Jul 20 '05 #1
4 2005
On 20 Jul 2004 07:59:29 -0700, Ellen Manning wrote:
Using SQL2000. I want to return the # of columns with non-null
values. Here's my query so far:

select
case when Dx1 is not null then 0 else 1 end +
case when Dx2 is not null then 0 else 1 end +
case when Dx3 is not null then 0 else 1 end +
case when Dx4 is not null then 0 else 1 end as DxCount
from tblClerkshipDataClean
where PalmName = @PalmName

There are 7 rows for the particular PalmName I'm using. The query
returns a result of 7. However, there are 25 values in Dx1 thru Dx4
so the query should be returning 25.

What am I doing wrong here? Thanks in advance.


First, some DDL to verify that I understand your setup:

create table tblClerkShipDataClean
( PalmName char(3) not null,
rowID int not null,
dx1 int null,
dx2 int null,
dx3 int null,
dx4 int null,
constraint pkey00 PRIMARY KEY (PalmName,rowID)
)

go
insert into tblClerkShipDataClean (PalmName,rowID,dx3,dx4) Values
('ABC',1,1,2)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx4) Values
('ABC',2,3,4,5)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx3,dx4) Values
('ABC',3,6,7,8,9)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx3,dx4) Values
('ABC',4,10,11,12,13)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx3,dx4) Values
('ABC',5,14,15,16,17)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx3,dx4) Values
('ABC',6,18,19,20,21)
insert into tblClerkShipDataClean (PalmName,rowID,dx1,dx2,dx3,dx4) Values
('ABC',7,22,23,24,25)
go

And now, to do it:

declare @PalmName char(3)
select @PalmName='ABC'

SELECT count(FLD) from (
SELECT Dx1 as FLD from tblClerkshipDataClean where PalmName = @PalmName
union all
SELECT Dx2 as FLD from tblClerkshipDataClean where PalmName = @PalmName
union all
SELECT Dx3 as FLD from tblClerkshipDataClean where PalmName = @PalmName
union all
SELECT Dx4 as FLD from tblClerkshipDataClean where PalmName = @PalmName
) T3
Jul 20 '05 #2
Ellen Manning (ma**********@hotmail.com) writes:
Using SQL2000. I want to return the # of columns with non-null
values. Here's my query so far:

select
case when Dx1 is not null then 0 else 1 end +
case when Dx2 is not null then 0 else 1 end +
case when Dx3 is not null then 0 else 1 end +
case when Dx4 is not null then 0 else 1 end as DxCount
from tblClerkshipDataClean
where PalmName = @PalmName

There are 7 rows for the particular PalmName I'm using. The query
returns a result of 7. However, there are 25 values in Dx1 thru Dx4
so the query should be returning 25.


Haven't you swapped places of 0 and 1? And shouldn't you have a SUM?
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Yes, I noticed after I'd posted that I had my ones and zeros
backwards. Then after testing, discovered that I needed the sum.

Thanks for your replies.


Erland Sommarskog <es****@sommarskog.se> wrote in message news:<Xn*********************@127.0.0.1>...
Ellen Manning (ma**********@hotmail.com) writes:
Using SQL2000. I want to return the # of columns with non-null
values. Here's my query so far:

select
case when Dx1 is not null then 0 else 1 end +
case when Dx2 is not null then 0 else 1 end +
case when Dx3 is not null then 0 else 1 end +
case when Dx4 is not null then 0 else 1 end as DxCount
from tblClerkshipDataClean
where PalmName = @PalmName

There are 7 rows for the particular PalmName I'm using. The query
returns a result of 7. However, there are 25 values in Dx1 thru Dx4
so the query should be returning 25.


Haven't you swapped places of 0 and 1? And shouldn't you have a SUM?

Jul 20 '05 #4
Isn't this what you want:

SELECT COUNT(dx1)+COUNT(dx2)+COUNT(dx3)+COUNT(dx4)
FROM tblClerkshipDataClean
WHERE palmname = @palmname

--
David Portas
SQL Server MVP
--
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
5
by: Stanley Sinclair | last post by:
I have a need to return multiple result sets from a stored procedure. Want that SP to call others to get the data. Win2003, db2 8.1.5. Can't figure out how to handle open cursors, and return...
5
by: Gary Blakely | last post by:
I'm giving this post another try - it can't be too difficult for everyone.... In the program below, the web page has dataGrid1. the only thing that has been done to it at design time is to...
4
by: Bill Moran | last post by:
I'm having a little trouble understanding how to do something. I assume I'm just missing it in the documentation, so a pointer to relevent docs would be as welcome as a direct answer. I have a...
7
by: sameer | last post by:
Hi all, Application environment : VB.Net desktop application,.NET 1.1 Framework, VS 2003. communicates between the database and the application is done over webservices using ADO.NEt Datasets....
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
6
by: EvilOldGit | last post by:
const Thing &operator++(int) { Thing temp = *this; operator++(); return temp; } Is this code robust ? I get a compiler warning about returning a reference to a a local, which I guess is...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.