##### SQL query for Heron's formula

Hello i want to implement heron's formula.

s  =  ( a  +  b  +  c  ) / 2

Then, construct the following expression:

s ( s  −  a  ) ( s  −  b  ) ( s  −  c  )

Finally take the square root of the above expression to arrive at the area of the        triangle.

For example, if the given sides are a  =  3 , b  =  4 and c  =  5 , the area is    computed as follows:

s  =  ( a  +  b  +  c  ) / 2  =  ( 3  +  4  +  5 ) / 2  =  12 / 2  =  6

s ( s  −  a  ) ( s  −  b  ) ( s  −  c  ) = 6 ( 6  −  3 ) ( 6  −  4 ) ( 6  −  5 )

=  6 ( 3 ) ( 2 ) ( 1 )  =  36

i have calculated S by SELECT Sum(a+b+c)/2.0) as S from Triangle ;

Triangle is my tbl name.

Please help. how to implement this formula and gt ans

Ranita Biswas
1 Oct 2015 12:47 pm

The query should be something like this:

SELECT SQRT(s*(s−a)*(s−b)*(s−c)) FROM (SELECT a, b, c, (a+b+c)/2.0 AS s FROM Triangle);

You don't need to use SUM function as it is for summing values from a single column.
http://www.w3schools.com/sql/sql_func_sum.asp
Can you please run this query with your table and let us know if it is giving you the desired result?

Zeal
1 Oct 2015 01:02 pm

ORA-00911: invalid character is the error

Ranita Biswas
1 Oct 2015 01:11 pm

Can you please upload the Triangle table by exporting and zipping it?

Zeal
1 Oct 2015 01:14 pm

This is my table
CREATE TABLE Triangle
(
ID NUMBER NOT NULL,
a NUMBER(10),
b NUMBER(10),
c NUMBER(10),
PRIMARY KEY (ID)
);
INSERT INTO Triangle(ID,a,b,c)
VALUES (1,3,4,5);

Ranita Biswas
1 Oct 2015 06:18 pm

Sorry for the delayed reply, I just ran the query.
Here I am attaching the screenshot.

I missed the aliasing last time, otherwise it is working fine.
Hope this helps.