Teradata Geospatial - Multipolygon / polygon table creation - teradata

Dears,
I have table with points for each id and we need to create a multipolygon or polygon table in teradata with this data
table :
id x-point y -points
100 10 25
100 12 28
100 100 250
101 110 125
101 120 129
need to create a table in teradata for each id with multipolygon or polygon as geometry datatype
the output table should be
id Geometry
100 multipolygon ( (10 25,12 28, 100 250) )
101 polygon(( 110 125, 120 129))
could any one please help me with the geospatial query to achieve this results

There's an AggGeom table operator, which can be used to combine multiple rows with geospatial objects:
SELECT id, geom
FROM AggGeom
( ON
(
SELECT new ST_Geometry('point(' || trim(x_point) ||' ' || trim (y_point) || ')') as geom, id
FROM vt
)
PARTITION BY id
USING Operation('Union')
) AS dt;
But this will return a MULTIPOINT, not a POLYGON, least of all a MULTIPOLYGON
geom.st_ConvexHull() is pobably the closest you can get.

Related

Count distinct count over a large SAS dataset

I have a SAS dataset for 200million records with Date, Id and amount for year 2022
Have:
ID Date Amount
101 1/31/2022 50
102 1/31/2022 100
101 2/28/2022 25
.....................
I am trying to get the unique # of accounts for each month but the it is taking more than an hour to give me the results
proc sql;
create table unique as select distinct
date,
count(distinct id) as uniqid
from Have
group by date
quit;
Is there an alternate method to have the results quickly?
One alternate available method is to use proc freq with the nlevels option and access its ods output. The one catch is that your data must be sorted by date. If it is not, you'll need to sort your 200M row table by date which could take much longer than a SQL statement alone. If you have to do this, try keeping only the two variables of interest in a temporary table.
proc sort data = have(keep=date id)
out = have_sorted;
by date;
run;
ods select none;
proc freq data=have_sorted nlevels;
by date;
tables id;
ods output nlevels=unique_count(keep = date nlevels
rename = (nlevels = uniqid)
);
run;
Other options include trying to tune your bufno and bufsize system options. Check out this case study from SANrd on how this affects I/O.
Also consider looking into the %freqyeah macro which parallelizes proc freq: From FREQing Slow to FREQing Fast: Facilitating a Four-Times-Faster FREQ with Divide-and-Conquer Parallel Processing
This probably won't be faster by may help you judge SQL performance.
data amount;
input ID :$3. Date :mmddyy. Amount;
format date mmddyy10.;
cards;
101 1/31/2022 50
102 1/31/2022 100
101 2/28/2022 25
101 3/31/2022 50
102 3/31/2022 100
103 4/30/2022 25
103 5/31/2022 50
102 5/31/2022 100
101 6/30/2022 25
101 1/31/2022 50
102 1/31/2022 100
101 2/28/2022 25
101 3/31/2022 50
102 3/31/2022 100
103 4/30/2022 25
103 5/31/2022 50
102 5/31/2022 100
101 6/30/2022 25
;;;;
run;
proc summary data=amount nway;
class date id;
output out=out1(drop=_type_ rename=(_freq_=obs));
run;
proc summary data=out1 nway;
class date;
output out=out2(drop=_type_) sum(obs)=;
run;
proc print;
run;

column delta values

I am planning to store query data in sqlite3 database.
I have these fields in sqlite3
UNIX_EPOCH, CUMULATIVE_QUERY_RATE
1452128581, 150
1452128582, 190
1452128583, 220
1452128584, 270
I want to get queries-per-second column as below:
QPS
0
40
30
50
how do I do it in sqlite3.
You simply have to subtract the value of the previous second:
SELECT unix_epoch,
(SELECT T1.cumulative_query_rate - T2.cumulative_query_rate
FROM SuperSecretTableName AS T2
WHERE T1.unix_epoch - 1 = T2.unix_epoch
) AS qps
FROM SuperSecretTableName AS T1;

Count two different columns values from the same table using SQLite Database table in android

I am trying to select multiple values from the same table. I need to select count values of the column rfid and process_status from the posts table and station_title from stations table.
Here are the two tables:
Posts table :
Id ownerId rfid stationId stationType process_status
1 107 rfid1 raj1222681607 like pending
2 107 rfid1 raj1222681607 like pending
3 107 rfid1 raj1125396157 like pending
4 107 rfid1 raj1222681607 like
5 107 rfid2 raj1222681607 like pending
6 107 rfid3 raj1222681607 like
Stations table :
Id title ownerId stationId stationLike stationPic
1 Check-in one 107 raj1125396157 1 0
2 nfc station 01 107 raj1222681607 1 0
From these two tables I want to fetch data as
Total RFIDs : 5
Total Pending : 3
Station Title : nfc station 01
The where clause conditions are : ownerId = 107 and the stationId = 'raj1222681607' and the process_status = 'pending'
So far I can achieve the total rfids, station title value; but i am not able to get the total pending value counting the process status.
My query snippet:
SELECT
COUNT(p.rfid) as TotalTap,
COUNT(p.process_status) as TotalPending,
s.title
FROM posts p
inner join
stations s
on p.stationId = s.stationId
WHERE
p.ownerId = 107 AND p.stationId = 'raj1222681607'
AND p.process_status = 'pending';
But this is giving the wrong output as :
Total RFIDs : 3 (THIS IS WRONG!!)
Total Pending : 3
Station Title : nfc station 01
To compute how many rows match a condition, use SUM over a boolean expression:
SELECT COUNT(*) AS TotalTap,
SUM(process_status = 'pending') AS TotalPending
FROM Posts
WHERE ownerId = 107
AND stationId = 'raj1222681607';
It does not really make sense to try to compute the third value in the same query; just use a separate, much simpler query:
SELECT title
FROM Stations
WHERE stationId = 'raj1222681607';
Android has a helper function for this:
String title = DatabaseUtils.stringForQuery(db,
"SELECT title FROM Stations WHERE stationId = ?",
new String[]{ "raj1222681607" });

copying data from one table to another table

MY tables
table name:emp
Name Null Type
------------------------------ -------- --------------------------------------------------------------- NUMBER
EMP_NAME VARCHAR2(10)
ADDRESS VARCHAR2(15)
PH_NO NUMBER(10)
DPT_NO NUMBER
result:
1 ram ctr 8892939927 100
2 mohan ptr 7569936347 101
3 mallu ppt 9553438342 102
4 scoot dmc 9874563210 103
5 jim plr 9236548875 104
6 ravi tpt 8562398756 105
7 manju hyd 7562398742 106
8 manoj hyd 869523654 107
9 sarath ctr 9632158769 108
10 hemanth mpk 9632147852 109
table anme: emp_department
Name Null Type
------------------------------ -------- -------------------------------------------------------------
EMP_NO NUMBER
DPT_NO NUMBER
PERIOD VARCHAR2(10)
now how to exatract(copy) emp_no and dpt_no values to emp_ department
I think you mean you want to populate emp_department from the data in the emp table - hopefully so the dpt_no column in that table can be removed. You haven't shown the emp_no column in emp, but it was in your earlier questions so I assume that's a typo. Your data model still seems a bit strange.
So to insert values from another table:
insert into emp_department (emp_no, dpt_no)
select emp_no, dpt_no
from emp;
You seem to be asking vey basic questions without showing much research. Please read the documentation or at least some tutorials and show what you've tried and what specific problems you're having.

SQLite Query to group continuous range of numbers into different grouping sets

I want get the islands of this table below:
Group MemberNo
A 100
A 101
A 200
A 201
A 202
A 203
X 100
X 101
A 204
X 301
X 302
A 500
A 600
I want get this results using SQL (the islands):
Group FromMemberNo ToMemberNo
A 100 101
A 200 204
X 100 101
X 301 302
A 500 500
A 600 600
I have seen a lot of codes/forums for this but not working with SQLite because SQLite doesn't have CTEs.
100-101 is continuous so that it will be group into one.
Does anyone know how to do it in SQLite?
The fastest way to do this would be to go through the ordered records of this table in a loop and collect the islands manually.
In pure SQL (as a set-oriented language), this is not so easy.
First, we find out which records are the first in an island. The first record does not have a previous record, i.e., a record with the same group but with a MemberNo one smaller:
SELECT "Group",
MemberNo AS FromMemberNo
FROM ThisTable AS t1
WHERE NOT EXISTS (SELECT 1
FROM ThisTable AS t2
WHERE t2."Group" = t1."Group"
AND t2.MemberNo = t1.MemberNo - 1)
To find the last record of an island, we have to find the record with the largest MemberNo that still belongs to the same island, i.e., has the same group, and where all MemberNos in the island are continuous.
We detect continuous MemberNos by computing the difference between their values in the first and last records.
The last MemberNo of the island with group G and first MemberNo M can be computed like this:
SELECT MAX(MemberNo) AS LastMemberNo
FROM ThisTable AS t3
WHERE t3."Group" = G
AND t3.MemberNo - M + 1 = (SELECT COUNT(*)
FROM ThisTable AS t4
WHERE t4."Group" = G
AND t4.MemberNo BETWEEN M AND t3.MemberNo)
Finally, plug this into the first query:
SELECT "Group",
MemberNo AS FromMemberNo,
(SELECT MAX(MemberNo)
FROM ThisTable AS t3
WHERE t3."Group" = t1."Group"
AND t3.MemberNo - t1.MemberNo + 1 = (SELECT COUNT(*)
FROM ThisTable AS t4
WHERE t4."Group" = t1."Group"
AND t4.MemberNo BETWEEN t1.MemberNo AND t3.MemberNo)
) AS LastMemberNo
FROM ThisTable AS t1
WHERE NOT EXISTS (SELECT 1
FROM ThisTable AS t2
WHERE t2."Group" = t1."Group"
AND t2.MemberNo = t1.MemberNo - 1)

Resources