Solved

SQL - Math Question

Posted on 2014-01-09
7
338 Views
Last Modified: 2014-01-09
I have an old document with the following syntax. This Query will Set a column to "YES" in my Master Hex table if 85% of the Households are included from the Test table.

I would like to change the query to give me summery of all Hex's above 75% and not update my Master Hex Table.
Old Code
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF

update dbo.Master_Hex
set dbo.Master_Hex.[EORN_served] = 'Yes'
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.85
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null 

Open in new window


New Code would be a select Query - I'm Doing this wrong (I'm a novice level in SQL)
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null

Open in new window

0
Comment
Question by:PtboGiser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768156
you where close:
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) percentag
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75 

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 39768160
Msg 8134, Level 16, State 1, Line 3
Divide by zero error encountered.
 I tried that code earlier. Then switched it.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768173
well, that error :)
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) percentag
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households <> 0
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75 
                                            

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39768180
btw, you may want to read up this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html

your query would become:
select mh.GISHEXID
, cast( t.Total_HH as 
float) / cast( mh.Total_Households as float) percentag
from dbo.test t
join dbo.Master_Hex mh
  on t.[GISHEXID] = mh.[GISHEXID]
and t.Total_HH is not null
and mh.Total_Households <> 0
and cast( t.Total_HH as float)/cast( mh.Total_Households as float) >=0.75 

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 39768195
Thanks
Aliases i'm split on. Since we're  lower lever programmers at the point and our queries are usually pretty simple we have not used them much as it seems to cause more confusion then what's it worth at this point. I totally see there benefit though and have used them on occasion
Thanks


Can I change the Percetage column to a % type?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768218
there is no "%" type as such, but you can diplay accordingly:

cast( round( 100.00 * ( a / b ) , 1 )  as varchar(20)) + '%'

this should do
0
 

Author Closing Comment

by:PtboGiser
ID: 39768244
Thank you kind sir! Have a good day.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question