tags:

views:

28

answers:

2

Taking Sql this quarter and not having any luck with the following question:

  1. The height of players in feet (inches/12). Include their name. Hint: Calculation or derived field. Be sure to give the calculated field a column header.

We're learning the basic Select statment and didn't find any reference on how to make custom data at w3schools. I'm using Microsoft SQL server Management Express Here's my statment so far:

 select nameLast, nameFirst, height
 from Master
 where height    (I assume its something like 'Player_Height' = height/12)         
 order by nameLast, nameFirst, height

Thanks for the help

A: 

Try something like

select nameLast, nameFirst, (height/12) as heightInInches
 from Master
 order by nameLast, nameFirst, height
tschaible
kinda worked it gave me some null values thoughAbbey Charlie NULLAbbott Dan 5.91666666666667Abbott Fred 5.83333333333333
Matt
nevermind I should have looked at height to figure out why I was getting the null...missing values. Thanks for the help
Matt
+2  A: 

If you need that result a lot, you might want to consider putting that into its own, computed column and persist it into your table. That way, it'll be up to date at all times, and you won't have to compute and re-compute it over and over and over again:

ALTER TABLE dbo.YourTable
   ADD HeightInFeet AS ROUND(Height / 12.0, 3) PERSISTED

With this, you can also round your result to e.g. 3 digits after the decimal point (or pick whatever makes sense to you). Each row in your table will now have a new column called HeightInFeet, always computed and always up to date, and you can query on it, you can return it in a SELECT - whatever you like to do! (you just can set a new value to it, since it's being computed by SQL Server itself, based on your Height column)

marc_s