开发者

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 learni开发者_开发技巧ng 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


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)


Try something like

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜