Link to home
Start Free TrialLog in
Avatar of PtboGiser
PtboGiserFlag for Canada

asked on

SQL - Math Question

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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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

Avatar of PtboGiser

ASKER

Msg 8134, Level 16, State 1, Line 3
Divide by zero error encountered.
 I tried that code earlier. Then switched it.
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

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
there is no "%" type as such, but you can diplay accordingly:

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

this should do
Thank you kind sir! Have a good day.