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 18th, 2009, 04:07 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default Calculation In Select Statement

Hello All,

I am looking for a way to perform a calculation on the results of weight and height. I need see if there is a result for the height and weight for a person on the same date since they are entered at the same time. If so, then I need to perform the calculation (weight/(height*height) x 703).

Any help would be appreciated.

Thank you.

Code:

USE TempDB
GO

CREATE TABLE dbo.HeightWeight
(      Name          VARCHAR(50),
        Date          DateTime,
        Description   VARCHAR(50),
        Result        VARCHAR(20)
)
INSERT INTO HeightWeight
(Name,Date,Description,Result)
SELECT 'Test1','2009-05-01','Height','60' UNION ALL
SELECT 'Test1','2009-05-01','Weight','175' UNION ALL
SELECT 'Test1','2009-05-02','Height','60' UNION ALL
SELECT 'Test1','2009-05-02','Weight','176' UNION ALL
SELECT 'Test2','2009-05-03','Height','65' UNION ALL
SELECT 'Test2','2009-05-03','Weight','190' UNION ALL
SELECT 'Test3','2009-05-04','Height','68' UNION ALL
SELECT 'Test3','2009-05-04','Weight','180' UNION ALL
SELECT 'Test4','2009-05-05','Height','63' UNION ALL
SELECT 'Test4','2009-05-05','Weight','185'
 
Old May 18th, 2009, 06:48 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Code:
 SELECT Date,Name,
        MAX(CASE WHEN Description='Weight' THEN Result END)
    /POWER(MAX(CASE WHEN Description='Height' THEN Result END),2)
    * 703
  FROM #HeightWeight
 GROUP BY Date,Name
 ORDER BY Date,Name

Last edited by Jeff Moden; May 18th, 2009 at 06:51 PM..
 
Old May 18th, 2009, 06:52 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

p.s. I really hate the way this forum scrambles simple code.
__________________
--Jeff Moden
 
Old May 18th, 2009, 07:40 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...Jeff, he wanted that to ONLY happen when the height and weight were entered ON THE SAME DATE.

I don't see any place in there where he asked for a maximum of either value.

I think what he meant was this:
Code:
SELECT W.Name, W.Date, (703.0 * W.result / POWER(H.result,2) ) AS BMI
FROM HeightWeight AS W, HeightWeight AS H
WHERE H.Date = W.Date
ORDER BY W.Name, W.Date
Except that for some silly reason he made the RESULT field a VARCHAR(20) datatype. HORRIBLE!

So he needs a CAST or CONVERT in there:

Code:
SELECT W.Name, 
       W.Date, 
       (703.0 * CONVERT(FLOAT,W.result) / POWER(CONVERT(FLOAT,H.result),2) ) AS BMI
FROM HeightWeight AS W, HeightWeight AS H
WHERE H.Date = W.Date
ORDER BY W.Name, W.Date
Ugh.

eusanpe : WHY did you make the RESULT field anything other than FLOAT???

p.s.: Since he noted the use of dbo.HeightWeight, this must be SQL Server, so the syntax for both CONVERT and POWER is correct.
The Following User Says Thank You to Old Pedant For This Useful Post:
rstelma (May 18th, 2009)
 
Old May 18th, 2009, 07:52 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thank you both for your help.

It is not a Float field because there are other Descriptions that have values that are text, like yes, no, true. I didn't design the thing.


Thanks again
 
Old May 18th, 2009, 08:19 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
I didn't design the thing.
Oh, the joys of working on legacy applications. I undestand all too well.

Best of luck!
 
Old May 19th, 2009, 05:20 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
Ummm...Jeff, he wanted that to ONLY happen when the height and weight were entered ON THE SAME DATE.
"Must look eye". GROUP BY takes care of that requirement quite nicely.
__________________
--Jeff Moden
 
Old May 19th, 2009, 06:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

SHEESH!

I *completely missed* your GROUP BY! Shame on me!

DOH and double DOH. Yes, a much better solution.
 
Old May 19th, 2009, 08:35 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Heh... not a problem.

For those following this thread, the method I used is nothing more than a "cross-tab". Cross-tabs are one of the fastest ways to "pivot" vertical data from an NVP table (Name Value pair) and it eliminates a lot of joins to do the same thing.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select statement.. help please sarah lee SQL Server 2000 4 May 16th, 2007 09:13 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Calculation using if...then...else statement oliver ASP.NET 1.0 and 1.1 Basics 2 March 7th, 2007 07:29 PM
Skewness calculation in SQL statement akmhasan Oracle 0 July 13th, 2003 04:27 AM





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