Matt
Matt

Reputation: 557

Calculation of derived field in sql

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

Upvotes: 0

Views: 3896

Answers (2)

marc_s
marc_s

Reputation: 755491

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)

Upvotes: 2

tschaible
tschaible

Reputation: 7695

Try something like

select nameLast, nameFirst, (height/12) as heightInInches
 from Master
 order by nameLast, nameFirst, height

Upvotes: 0

Related Questions