Postgresql: Merge multiple geometries into single geometry using Join - sql

Say I have two tables, geom_levels and taz_geoms where taz_geoms has the columns as follows:
taz_geoms
id(int) state(int) county(int) taz(int) geom(geometry(MultiPolygon,4326))
and geom_levels looks like this:
geom_levels
TAZ COUNTY STATE DISTRICT
1 1 29 1
2 1 29 1
3 1 29 1
4 2 29 2
5 2 29 2
6 2 29 2
7 2 29 3
8 3 29 3
9 3 29 3
10 3 29 4
11 3 29 4
12 3 29 4
13 4 29 5
14 4 29 5
15 4 29 5
16 4 29 6
17 4 29 6
How would I go about combining these taz geometries into county, state, and district geometries? I would like to have a county_geoms, state_geoms, and district_geoms table. I have see that you can use ST_UNION with a geom array, but how would I generate such an array for counties or districts?
I was thinking something like this for counties:
SELECT ST_UNION(SELECT geom from taz_geoms GROUP BY county);
and for districts:
SELECT ST_UNION(SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district);
But those options do not see possible.
Ideas?

try with:
SELECT ST_UNION( ARRAY( 'YOUR SELECT geoms QUERY' ) );
in your case:
SELECT ST_UNION(ARRAY( (SELECT geom from taz_geoms t LEFT JOIN geom_levels gl ON gl.taz = t.taz GROUP BY district) ));
I had the same problem and got it to work with postgre usin the ARRAY() function ;)

Related

Match data values for two tables in Teradata Sql

Using Teradata :I have two tables with 10 records and 3 variables. All columns and values are same expect for three values in one variable.
My task is to make code changes for table2 where both records are matched, by not hard coding any value.
The second table was created by the first table , so there is no way to pick values by join etc .
Code :
Create multiset table table2 as (
Select * from table1 )
With data primary index(var1);
Eg:
Var1
Var2
Var3
1
Abc
20
2
Cde
30
3
kgk
87
4
kjj
98
5
gvy
67
6
jbn
78
7
hvb
56
8
ihg
62
9
jhn
22
10
hbn
34
Var1
Var2
Var3
1
Abc
20
2
Cde
30
3
kgk
87
4
kjj
98
5
gvy
67
6
jbn
78
7
hvb
56
8
ihg
77
9
jhn
56
10
hbn
23
Not sure what you want but you can find all the matching records using exists as follows:
select t.* from table2 t
where exists
(select 1 from table1 tt
where t.var1 = tt.var1 and t.var2 = tt.var2)

To prepare a dataframe with elements being repeated from a list in python

I have a list as primary = ['A' , 'B' , 'C' , 'D']
and a DataFrame as
df2 = pd.DataFrame(data=dateRange, columns = ['Date'])
which contains 1 date column starting from 01-July-2020 till 31-Dec-2020.
I created another column 'DayNum' which will contain the day number from the date like 01-July-2020 is Wednesday so the 'DayNum' column will have 2 and so on.
Now using the list I want to create another column 'primary' so that the DataFrame looks as follows:
In short, the elements on the list should repeat. You can say that this is a roster to show the name of the person on the roster on a weekly basis where Monday is the start (day 0) and Sunday is the end (day 6).
The output should be like this:
Date DayNum Primary
0 01-Jul-20 2 A
1 02-Jul-20 3 A
2 03-Jul-20 4 A
3 04-Jul-20 5 A
4 05-Jul-20 6 A
5 06-Jul-20 0 B
6 07-Jul-20 1 B
7 08-Jul-20 2 B
8 09-Jul-20 3 B
9 10-Jul-20 4 B
10 11-Jul-20 5 B
11 12-Jul-20 6 B
12 13-Jul-20 0 C
13 14-Jul-20 1 C
14 15-Jul-20 2 C
15 16-Jul-20 3 C
16 17-Jul-20 4 C
17 18-Jul-20 5 C
18 19-Jul-20 6 C
19 20-Jul-20 0 D
20 21-Jul-20 1 D
21 22-Jul-20 2 D
22 23-Jul-20 3 D
23 24-Jul-20 4 D
24 25-Jul-20 5 D
25 26-Jul-20 6 D
26 27-Jul-20 0 A
27 28-Jul-20 1 A
28 29-Jul-20 2 A
29 30-Jul-20 3 A
30 31-Jul-20 4 A
First compare column for 0 by Series.eq with cumulative sum by Series.cumsum for groups for each week, then use modulo by Series.mod with number of values in list and last map by dictioanry created by enumerate and list by Series.map:
primary = ['A','B','C','D']
d = dict(enumerate(primary))
df['Primary'] = df['DayNum'].eq(0).cumsum().mod(len(primary)).map(d)

Repetation of column when using join between two table

As per using select query in postgres along 8 or 9 table using join found output as
1. A 2 34
2. A 2 56
3. B 3 34
4. B 3 56
whereas i required output in two form either
1. A 2 34
2. A 2 34
3. B 3 56
4. B 3 56
or
A 2 34
B 3 56
what can i do?
Using distinct?
select distinct * from table

PostgreSQL: Select particular column and its total row count

I will explain my problem with an sample example
create table foo(id int,idx int,idy int,fld int,fldx varchar);
insert into foo values (1,2,3,55,'AA'),(2,3,4,77,'AB'),(3,4,8,55,'AX'),(9,10,15,77,'AR'),
(3,4,8,11,'AX'),(3,4,8,65,'AX'),(3,4,8,77,'AX');
id,idx,idy, fld,fldx
1 2 3 55 AA
2 3 4 77 AB
3 4 8 55 AX
9 10 15 77 AR
3 4 8 11 AX
3 4 8 65 AX
3 4 8 77 AX
I need to select only column fld and its total count of each column(fld) in descending order
Expected Result :
fld count
---------
77 3
55 2
11 1
65 1
select fld
,count(fld) rw_count
from foo
group by fld
order by rw_count desc
Group By
select fld,count(*) from foo group by 1 order by 2 desc ;

Query showing ID instead of description

It works fine untill I start using UNION ALL!
Have to use classified here because I'm not allowed to share too much information
It should show the Reden Uitstroom and NOT the ID. I have really no clue why this happens...
Look-up Table=
ID Description
1 classified
2 classified
3 classified
4 classified
5 classified
6 classified
7 classified
8 classified
9 classified
Code =
SELECT Count(Hertoetsing.[Reden uitstroom1]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom1]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom1]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom1]
UNION ALL
SELECT Count(Hertoetsing.[Reden uitstroom2]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom2]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom2]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom2]
UNION ALL
SELECT Count(Hertoetsing.[Reden uitstroom3]) AS [Aantal Uitstroom 2014],
(Hertoetsing.[Reden uitstroom3]) AS [Reden Uitstroom]
FROM Klantinformatie
INNER JOIN Hertoetsing
ON Klantinformatie.KlantID=Hertoetsing.Klantid
WHERE (((Year(Hertoetsing.[Datum uitstroom3]))=2014))
GROUP BY Hertoetsing.[Reden uitstroom3];
Result =
Aantal Uitstroom 2014 Reden Uitstroom
27 1
25 2
46 3
1 4
3 5
9 6
17 8
3 9
4 1
2 2
5 3
1 4
1 6
1 1
1 2
1 3
2 8
Correcting Group By =
SELECT Sum([Aantal Uitstroom 2014]) AS [Uitstroom 2014],
[Reden Uitstroom] AS [Uitstroom]
FROM [Reden Uitstroom1 2014]
GROUP BY [Reden Uitstroom];
Result =
Uitstroom 2014 Uitstroom
32 1
28 2
52 3
2 4
3 5
10 6
19 8
3 9
EDIT - Another Issues with another query =
Query runs properly but once I put it into a report then it starts showing the ID from the look-up table instead of description.... This is so weird....