Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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

0
PtboGiser
Asked:
PtboGiser
  • 4
  • 3
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
PtboGiserAuthor Commented:
Msg 8134, Level 16, State 1, Line 3
Divide by zero error encountered.
 I tried that code earlier. Then switched it.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
PtboGiserAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
PtboGiserAuthor Commented:
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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now