I have a table that I'm inserting GPS coordinates (lat/lon) on each record.
table schema is like: (Id, Time, Lat, Lon)
Is it possible to calculate distance of two continuous records using Calculated columns?
Something that schema become like this: (Id, Time, Lat, Lon, Distanceof( ID -1, ID ))
Note: I know how to calculate distance of two points, but I dont know if its possible to access multiple rows data on a calculated column.
It is impossible to directly use values of other rows in calculated column definition, but one can create a user-defined function and use it:
CREATE FUNCTION dbo.CalcDistance(#prev_row_id INT, #row_id INT)
AS
...
and then define calculated column expression as dbo.CalcDistance(Id - 1, Id).
you can create a function Distanceof for calculating the distance.
and you can select as
Select id, time, Lat,Lon, Distanceof(Lat, Lng,radius) from the table.
it will calculate for each row. But it will timeconsuming if you are doing many rows
Related
I am working with a data set where i have to get Min or Max for different text fields. My dataset can have thousands of rows so below is a simpler example. So I have 3 categories having multiple values and I can put this dataset in GDS to build a table where I select Category as dimention and Value as Max(Value) in metric.
Now I need to see the sum of all those values too. But like the pivot table in excel, the subtotal in GDS shows the Max out of all the max listed above. So instead of 65, it shows 30 in GDS. Is there a way I can get it to show the sum?
To reach the desired result you will need:
Make a data combination, not being necessary to insert a second base, just so that a current base is defined as a data combination.
In the combination use the Category dimension and define the Max Value metric. The combination is only necessary for the metric to be used in the table as a dimension (this is a property resulting from the combination of data).
Configure the table with the Category dimension and Include the metric with the Value sum option. Remember that now Value is the maximum value (as defined in the data combination).
Finally, display the Summary line. And the desired result is obtained
I have a thousand coordinates based on Places Name, for example, as in picture
I have to create a polygon from coordinates separated by comma based on Places Name. My formula example in row E1.
But it will be tiring if I have to type the formula one by one while I have to determine Latitude Longitude based on the Places
Are there the fastest way to solve while I can create a list of coordinates separated by comma based on a variable (Places Name) in column A?
=ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(QUERY(QUERY(
IF(D2:D<>"", {A2:A&"♠", D2:D&","}, ),
"select max(Col2) where Col1 is not null group by Col2 pivot Col1")
,,999^99)), "♠")), ",$", ))
I have two tables. One table contains two columns RepresentativeID and SalesAmount.
The second Table contains three columns RepresentativeID, BaseAmount and RateCommision.
A representative gets a commission of let say 1% all sales over 1000.
I need to compute total commission for each representative.
How can I aggregate one table first and then connect it with another?
Welcome to Stack Overflow, Iryna!
First, you need to connect your tables on RepresentativeID.
For simplicty, I will call your first table "Sales", and the second table "Rep".
Your data model then should look something like that:
Note that Rep table should be on 1 side, and Sales table on many side (1:*)
Then, in Sales table add measure:
Rep Sale = SUM(Sales[Amount])
and another measure:
Rep Commission = SUMX( Rep, ( [Rep Sale] - Rep[Base]) * Rep[Rate])
How it works:
to calculate commissions, you need to iterate by each representative. SUMX function iterates over table Rep, calculates sales for each of them, then calculates their commissions, and then sums them up.
In my Postgres 9.5 database with PostGis 2.2.0 installed, I have a table buildings with geometric data (points) centroid. The table contains about 3 million buildings, of which about 300.000 contain special information.
Now, for each buildings.gid I want to know, how many other buildings of the same table are within a certain radius (I want to test this for different radiuses: 20meter, 50meter, 100m, 200m, 500m if it can be done in an adequat amount of time) and add this information to a column of buildings. The related columns are N20, N50,...
Query
I figured out to use something like:
UPDATE buildings
SET N50=sub.N
FROM (SELECT Count(n.gid) AS N
FROM buildings n, buildings b
WHERE ST_DWithin(b.centroid, n.centroid, 50) -- distance in meter
) sub
Related to this solution of #ErwinBrandstetter, where there is a coordinate given, around with the radius is produced. But even when testing for only one gid, I did not recieve an result in an acceptable amount of time.
The difference to my problem is, that I want this to be done for every building.
Table definitions
CREATE TABLE public.buildings
(
gid integer NOT NULL DEFAULT nextval('buildings_gid_seq'::regclass),
osm_id character varying(11),
name character varying(48),
type character varying(16),
geom geometry(MultiPolygon,4326),
centroid geometry(Point,4326),
gembez character varying(50),
gemname character varying(50),
krsbez character varying(50),
krsname character varying(50),
pv boolean,
gr numeric,
capac double precision,
instdate date,
pvid integer,
dist double precision,
gemewz integer,
n50 integer,
n100 integer,
n200 integer,
n500 integer,
n1000 integer,
IBASE numeric,
CONSTRAINT buildings_pkey PRIMARY KEY (gid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.buildings
OWNER TO postgres;
CREATE INDEX build_centroid_gix
ON public.buildings
USING gist
(st_transform(centroid, 31467));
CREATE INDEX buildings_geom_idx
ON public.buildings
USING gist
(geom);
Advanced Problem
(The following might be another problem, hence should be another question on stackoverflow, but there might be the chance to implement this in the first question)
Furthermore, referring to the "special information", 268238 of the buildings contain information about dist,instdate,capac. These columns of the remaining buildings are NULL.
instdate is the date, at which a building had a "PV" installed. I need to transform the table buildings to a panel datatype table, which means that for each period (in my case 11 periods) exists one row for the same building.
Now I need to check, how many other buildings within the radius already had a "PV" installed.
To do so, I want to query all buildings within a radius (like in first question) where for example capac IS NOT NULL, but now the buildings shall not be counted, but their information about dist,instdate,capac shall be added as a string to IBASE.
Try building an index on a geography cast, which can be used for ST_DWithin (so you can calculated metric distances with geographic data)
CREATE INDEX buildings_geog_idx ON buildings USING gist (geom::geography);
UPDATE buildings SET n50=c.count
FROM (
SELECT a.gid, count(b.gid)
FROM buildings a
LEFT JOIN buildings b ON ST_DWithin(a.geom::geography, b.geom::geography, 50.0)
AND a.gid <> b.gid
GROUP BY a.gid
) c
WHERE c.gid = buildings.gid;
You could also try calculating on a sphere for faster performance, but potential errors from spheroid distances:
ST_DWithin(a.geom::geography, b.geom::geography, 50.0, false)
from some reasons I need to insert an artificial(dummy) column into a mdx expression. (the reason is that i need to obtain a query with specific number of columns )
to ilustrate, this is my sample query:
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All],[Measures].[IC_without_material],[Measures].[Nonconformance_PO],[Measures].[Nonconformance_GPT],[Measures].[PM_GPT_Weighted_Targets],[Measures].[PM_PO_Weighted_Targets], [Measures].[AVG_LC_Costs],[Measures].[AVG_MC_Costs]} ON COLUMNS,
([dim_ProductModel].[PLA].&[SME])
* ORDER( {([dim_ProductModel].[Warranty Group].children)} , ([Measures].[Nonconformance_GPT],[Dim_Date].[Date Full].&[2014-01-01]) ,desc)
* ([dim_ProductModel].[PLA Text].members - [dim_ProductModel].[PLA Text].[All])
* {[Dim_Date].[Date Full].&[2013-01-01]:[Dim_Date].[Date Full].&[2014-01-01]} ON ROWS
FROM [cub_dashboard_spares]
it is not very important, just some measures and crossjoined dimensions. Now I would need to add f.e. 2 extra columns, I don't care whether this would be a measure with null/0 values or another crossjoined dimension. Can I do this in some easy way without inserting any data into my cube?
In sql I can just write Select 0 or select "dummy1", but here it is not possible neither in ON ROWS nor in ON COLUMNS part of the query.
Thank you very much for your help,
Regards,
Peter
ps: so far I could just insert some measure more times, but I am interested whether there is a possibility to insert really "dummy" column
Your query just has the measures dimension on columns. The easiest way to extend it by some columns would be to repeat the last measure as many times that you get the correct number of columns.
Another possibility, which may be more efficient in case the last measure is complex to calculate would be to use
WITH member Measures.dummy as NULL
SELECT {[Measures].[AFR],[Measures].[IB],[Measures].[IC All],[Measures].[IC_without_material],[Measures].[Nonconformance_PO],[Measures].[Nonconformance_GPT],[Measures].[PM_GPT_Weighted_Targets],[Measures].[PM_PO_Weighted_Targets], [Measures].[AVG_LC_Costs],[Measures].[AVG_MC_Costs],
Measures.dummy, Measures.dummy, Measures.dummy
}
ON COLUMNS,
([dim_ProductModel].[PLA].&[SME])
* ORDER( {([dim_ProductModel].[Warranty Group].children)} , ([Measures].[Nonconformance_GPT],[Dim_Date].[Date Full].&[2014-01-01]) ,desc)
* ([dim_ProductModel].[PLA Text].members - [dim_ProductModel].[PLA Text].[All])
* {[Dim_Date].[Date Full].&[2013-01-01]:[Dim_Date].[Date Full].&[2014-01-01]}
ON ROWS
FROM [cub_dashboard_spares]
i. e. adding a dummy measure that should not need much computation as many times as you need it to the end of the columns.