Solved

SQL - Math Question

Posted on 2014-01-09
7
329 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
  • 4
  • 3
7 Comments
 
LVL 142

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 142

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 142

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 142

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now