SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Summing 'created' fields in a SELECT statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

swims01
Yak Posting Veteran

59 Posts

Posted - 03/09/2012 :  15:54:24  Show Profile  Reply with Quote
Hey all.

I have a form that my company uses to get info on new clients.

We are paying staff for a certain amount of that form being completed. So, if 4 out of the 5 fields are filled out then they get paid on it. (Except there's much more than 5 fields)

I'm trying to figure out how to determine that 4 out of 5 fields have data in them. Any ideas how to do this?

My thought is that I do a CASE on each field and then assign a '1' if there is data found. I could then add the total together to get the final result showing the total fields filled out.


SELECT
CASE WHEN field1 IS NOT NULL THEN '1' ELSE '0' END AS field1,
CASE WHEN field2 IS NOT NULL THEN '1' ELSE '0' END AS field2,
CASE WHEN field3 IS NOT NULL THEN '1' ELSE '0' END AS field3,
CASE WHEN field4 IS NOT NULL THEN '1' ELSE '0' END AS field4,
CASE WHEN field5 IS NOT NULL THEN '1' ELSE '0' END AS field5,
sum( field1 + field2 + field3 + field4 + field5 ) as Score
FROM myTable


(I was thinking of dumping those results into a temp table and then I could 'select results where Score >= 4)

Skorch
Constraint Violating Yak Guru

USA
295 Posts

Posted - 03/09/2012 :  18:28:31  Show Profile  Reply with Quote
Would something like this work?

DECLARE @myTable TABLE (field1 int, field2 int, field3 int, field4 int, field5 int)
INSERT @myTable (field1, field2, field3, field4, field5)
SELECT 1, 1, 1, 1, 1 UNION
SELECT NULL, NULL, NULL, NULL, NULL UNION
SELECT 2, NULL, 2, 2, 2 UNION
SELECT 3, NULL, NULL, 3, 3 UNION
SELECT 4, 4, 4, NULL, 4 UNION
SELECT 0, NULL, NULL, 0, NULL UNION
SELECT 3, 3, 3, 3, NULL

SELECT field1+field2+field3+field4+field5 as Score
FROM (SELECT
CASE WHEN field1 IS NOT NULL THEN 1 ELSE 0 END AS field1,
CASE WHEN field2 IS NOT NULL THEN 1 ELSE 0 END AS field2,
CASE WHEN field3 IS NOT NULL THEN 1 ELSE 0 END AS field3,
CASE WHEN field4 IS NOT NULL THEN 1 ELSE 0 END AS field4,
CASE WHEN field5 IS NOT NULL THEN 1 ELSE 0 END AS field5
FROM @myTable) x


Some days you're the dog, and some days you're the fire hydrant.

Edited by - Skorch on 03/09/2012 18:29:14
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1429 Posts

Posted - 03/09/2012 :  18:34:24  Show Profile  Reply with Quote
SELECT *
FROM myTable
where CASE WHEN field1 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field2 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field3 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field4 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field5 IS NOT NULL THEN 1 ELSE 0 END >= 4


=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 03/11/2012 :  11:47:32  Show Profile  Reply with Quote
quote:
Originally posted by Bustaz Kool

SELECT *
FROM myTable
where CASE WHEN field1 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field2 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field3 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field4 IS NOT NULL THEN 1 ELSE 0 END +
	CASE WHEN field5 IS NOT NULL THEN 1 ELSE 0 END >= 4


=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno



Love it! Thanks all!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000