How to insert selection between an interval in PostgreSQL? - sql

I have a shapefile of airports. You can download it from here.
I've loaded this shapefile to postgreSQL. What I wanted is add points on centroids of each polygon.
1) I've added my point column;
select AddGeometryColumn('public','airport_shp','geom2',4326,'POINT',2);
2)Transformed coordinates from 3857 to 4326;
ALTER TABLE airport_shp ALTER COLUMN geom TYPE geometry(MultiPolygon,4326) USING ST_Transform(ST_SetSRID( geom,3857),4326);
3) I've inserted the centroids as points
insert into airport_shp(geom2) select st_centroid(geom) from airport_shp
The problem is; instead of adding the new values from the first empty row of the column, new values are added to the end of the file.
To make more sense below is representation of my first table;
*****************************************
* id *geom(MULTIPOLYGON)*geom2(POINT)*
*****************************************
* 1 *ADSADF23D31E475424* *
* 2 *2134ADSA0106000020* *
* 3 *214124RD74C5D6A5DE* *
* 4 *SAD23134230E24C5E4* *
*****************************************
I'm expecting this table to be like this after 3. step;
*****************************************
* id *geom(MULTIPOLYGON)*geom2(POINT)*
*****************************************
* 1 *ADSADF23D31E475424*ASDEF245A3RF*
* 2 *2134ADSA0106000020*523RFTYTFFTY*
* 3 *214124RD74C5D6A5DE*324FGSGSD523*
* 4 *SAD23134230E24C5E4*SADFS456V324*
*****************************************
But result is;
*****************************************
* id *geom(MULTIPOLYGON)*geom2(POINT)*
*****************************************
* 1 *ADSADF23D31E475424* *
* 2 *2134ADSA0106000020* *
* 3 *214124RD74C5D6A5DE* *
* 4 *SAD23134230E24C5E4* *
* 5 *ADSADF23D31E475424*ASDEF245A3RF*
* 6 *2134ADSA0106000020*523RFTYTFFTY*
* 7 *214124RD74C5D6A5DE*324FGSGSD523*
* 8 *SAD23134230E24C5E4*SADFS456V324*
*****************************************
Thanks in advance ! !

You need a UPDATE instead of INSERT:
UPDATE airport_shp SET geom2 = st_centroid(geom);

Related

Add and divide 2 rows to get percentage in SQL

From the data in the first table how do I get a simple percentage of total calculation:
two / (two + three) = 2 / (2 + 3) = 0.4
to arrive at:
You can try the below -
select ((case when name='two' then value end)*1.0)/sum(value)
from t
where name in ('two','three')
You would rarely do something like this in SQL. Anyway, you can do:
with
two as (select value from t where name = 'two'),
three as (select value from t where name = 'three')
select 1.0 * two.value / (two.value + three.value) as answer
from two
cross join three
Result:
answer
----------------------
0.40000000000000000000
See running example at DB Fiddle.

How do you calculate using sum in SQL Server

I am trying something like this:
select sum(R_Value) ,((5/cast(sum(R_Value) as int)) * 100.000000000)
from R_Rating
where R_B_Id = 135
But I keep getting value 0, sum(R_Value) = 6, so I just want a percentage out of (5/sum(R_Value)) * 100.
How can I get this?
I have a rating of 5 so each user has a rating they can make select from 1 to 5, so what i need is a formula that takes in the sum and takes into account how many users have rated and give us a result from 1 to 5 in the end
Something like this may work but i need to round up to one decimal place to get a whole number.
select sum(R_Value), ((count(*)/cast(sum(R_Value) as float)) * 10)
from R_Rating
where R_B_Id = 135
To get the average rating you need to force floating point algebra. For example:
select 1.0 * sum(R_Value) / count(*)
from R_Rating
where R_B_Id = 135
Then, if your query selects three rows with the values: 1, 4, and 5, then this query will return 3.33 stars as the average. That is:
= 1.0 * (1 + 4 + 5) / 3
= 1.0 * 10 / 3
= 10.0 / 3
= 3.33333333
I recommend writing this as:
select sum(R_Value) ,
(500.0 / sum(R_Value))
from R_Rating
where R_B_Id = 135;
This avoids an integer division.

SUM(ATR * QTDE) / SUM(QTDE) is the same as SUM(ATR)?

Background:
My boss just send me a query with the following column
ROUND(SUM(ATR * QTDE) / SUM(QTDE), 2)
But then I thought isn't it the same as
ROUND(SUM(ATR), 2)
??
If it was just ATR * QTDE / QTDE I would be certain that it's the same, but with the SUM I'm not sure, looks the same, but I can't just use what I think it's the same without been certain of it. Also I don't want to question my boss about it, so... here I'm!
Question:
SUM(ATR * QTDE) / SUM(QTDE) is the same as SUM(ATR) ?
Wanted results:
Explanation and prove of why it's the same or it's different.
It isn't the same!
If you try with sample data you can see it ...
ATR QTDE
5 1
5 2
SUM(ATR) = 5 + 5 = 10
SUM(ATR * QTDE) / SUM(QTDE) = (5*1 + 5*2) / (1 + 2) = 15 / 3 = 5
No, they are not the same. One is a weighted average and the other is just a sum. It is easy to devise counter
examples.
ATR QTDE
1 100
2 200
SUM(ATR) = 3. The ratio returns 500 / 3 <> 3.
You are probably thinking that these are equivalent:
ROUND(SUM(ATR * QTDE) / SUM(QTDE), 2)
ROUND(AVG(ATR), 2)
That would only be true if SUM(QTDE) = COUNT(ATR) -- which would normally happen if your data is set up this way or if QTDE = 1.

Update column: multiply with a value based on another column's row divided by 2

I need some help with an update statement as my SQL knowledge is limited.
Table Example
MLv | Prob1 | Prob2
------------------------
1 | 0 | 0,080
2 | 0 | 0,075
3 | 0 | 0,060
4 | 0,070 | 0,055
5 | 0,060 | 0,040
For each row, I need to multiply the values of Prob1 & Prob2 with the value from MLv divided by 2. I want to do this without creating a new column to store that multiplier.
My code so far:
USE MyDatabase
DECLARE #MULTIPLIER INT
SET #MULTIPLIER = (SELECT MLv / 2.0 FROM _MyDatabaseTable)
UPDATE _MyDatabaseTable
SET Prob1 = COALESCE (Prob1 * #MULTIPLIER, Prob1)
UPDATE _MyDatabaseTable
SET Prob2 = COALESCE (Prob2 * #MULTIPLIER, Prob2)
That obviously doesn't work, because it doesn't know which MLv value to pick and that's where I'm stuck as I don't know how to do this.
You would just use the column. No variables are needed:
UPDATE _MyDatabaseTable
SET Prob1 = COALESCE(Prob1 * MLV / 2.0, Prob1),
Prob2 = COALESCE(Prob2 * MLV / 2.0, Prob2);
Only one update is necessary as well.
It Just A Single Line Qry, Try It As Shown As Below :
UPDATE _MyDatabaseTable SET Prob1 = COALESCE (ISNULL(Prob1,0) * (MLV / 2) , Prob1),Prob2 = COALESCE (ISNULL(Prob2,0) * (MLV / 2), Prob2);

Update a Column from another Column using SQLite?

I have a DB with two columns with many records and from these two columns I have to do some mathematical operations and create other two columns.
For now I did like this:
I made a SELECT and read the two columns, and put everything in a List
Then I go through the List and UPDATE the table line by line
Do you think there is a faster way to do it? I also tried like this:
UPDATE myTable SET X_GAUSS = (SELECT X FROM myTable ) + 1, Y_GAUSS = (SELECT Y FROM myTable) + 2
(it's only an example)
But in this way every line of the new columns is the same as the line before, instead I want something like:
X Y X_GAUSS Y_GAUSS
1 2 2 4
3 4 4 6
5 6 6 8
...
A subquery like SELECT X FROM myTable returns the first row of the table.
You can simply access the columns of the same row directly:
UPDATE myTable
SET X_GAUSS = X + 1,
Y_GAUSS = Y + 2;