I am working on a project in SpatiaLite and I would like to join together two geometries to create one final geometry that contains both original polygons (Ex. Putting together two Legos). The tricky part is how to make this work in SpatiaLite with the 'Blob' geometry type. I have tried ST_Union but still cannot seem to get any result at all. The result of this will ultimately update a geometry in a table elsewhere.
Here's what I've done so far:
UPDATE table1
SET Shape = (
SELECT ST_Union (a.Shape, b.Shape)
FROM table2 as a
JOIN (
table3 as b
ON a.Shape = b.Shape)
WHERE a.ADDRESS = "" or b.ADDRESS = "");
I realize there are many errors in the syntax of this, I am only just getting used to SQLite and the sytanx versus that of Postgres. Please feel free to make edits.
Related
So, I am doing homework and I am unable to figure this step out.
Right-click the tables you just created, click Design, then add a new column in it, which is called GEOG, datatype is geography. Save the changes to your tables.
Fill the GEOG column with WKT column converted to Line/Polygon data.
For counties table build a new UPDATE query, convert WKT to Polygon and save it into GEOG column.
Table 1 is brazosStreet and table 2 is counties.
Table1 WKT is LINESTRING, and Table2 is POLYGON.
I am stuck on the Table2 Polygon step.
Validate the data and perform an intersection:
Geog.MakeValid();
Geog.STIntersects(Geog1) = 1;
The Validation:
Update [dbo].[Counties]
set Geog = Geog.MakeValid()
Perform the Selection:
SELECT *
FROM [dbo].[counties]
WHERE Geog.STIntersects(geography::STLineFromText(‘LINESTRING(-98.144094 31.915186, -93.914357 30.977930)’, 4326);
No matter what I type for the "Perform the selection" section, I get an error. I tried STPolyFromText and I tried deleting and starting over 3 times.
The only thing I can think of is that I skip over this section because I cannot get it to work:
Validate the data and perform an intersection
Geog.MakeValid();
Geog.STIntersects(Geog1) = 1;
The way I read that was as on overview of what is to come. Since it looks like Geog.MakeValid(); is part of the validation stage and Geog.STIntersects(Geog1) = 1; looks like part of the perform the selection stage.
Why is my syntax not working properly? I am unsure what SQL language exactly this is. Please let me know if I need to explain this situation more. Thanks for the help in advance!
UPDATE [dbo].[brazosStreets]
SET
[Geog] = geography::STLineFromText([WKT],4326);
UPDATE [dbo].[counties]
SET Geog = Geog.MakeValid;
select *
from [dbo].[counties] c
where c.STIntersect(geography::STLineFromText(‘LINESTRING(-98.144097
31.915186, -93.914357 30.977930)’,4326))=1;
I get error 102 when I try to run this.. Error with the 'single quote' placement involving a non-boolean operator
I have been looking all day for a solution that works for my situation. I have found some things that are very similar but don't work for my situation, I tried them.
Here is the scenario; I have two table base and partdetails. I have an asp website (internal ONLY) that has drop down lists to select the parameters for a SQL query that fills a data grid view.
My problem is this, I need to be able, based on the drop down list boxes on the page, assign the column name that the criteria that is entered to be searched for.
Here is the query that I am trying to define: (This one returns 0 rows)
sqlCmd.CommandText = ("Select ba.referenceid, ba.partnum, pd.width, pd.length, CONVERT(varchar(12), pd.dateentered, 101) As [dateentered], ba.partqty, ba.status, ba.material From tbl_dlbase ba Join tbl_partdetails pd On ba.referenceid = pd.referenceid Where Case #field1 When 'part #' Then 'ba.partnum' When 'Spacing' Then 'pd.spacing' When 'Surface' Then 'pd.surface' When 'Height' Then 'pd.height' When 'Thickness' Then 'pd.thickness' End Like '%' + #criteria1 + '%'")
sqlCmd.Parameters.AddWithValue("#field1", ddlSc1.SelectedItem.Text)
sqlCmd.Parameters.AddWithValue("#criteria1", txbCriteria1.Text)
This is the latest version of the SQL statement that I have tried. I need to be able to set the field/column name based on the selection from the drop down list ddlsc1 on the asp page.
I have also been trying the queries in Studio manager to see if maybe I have fat fingered something but it also returns 0 rows so I know something is wrong with the query.
So how can I set the column name field using a parameter for the name. I know this is a huge security concern with SQL injection but this is an internal only site, and more importantly my boss said he wants it done with variables.
I don't really see a problem with this other than you have single quotes around your THEN values. Does this fix it?
SELECT ba.referenceid
,ba.partnum
,pd.width
,pd.length
,CONVERT(VARCHAR(12), pd.dateentered, 101) AS [dateentered]
,ba.partqty
,ba.STATUS
,ba.material
FROM tbl_dlbase ba
JOIN tbl_partdetails pd ON ba.referenceid = pd.referenceid
WHERE CASE #field1
WHEN 'part #'
THEN ba.partnum
WHEN 'Spacing'
THEN pd.spacing
WHEN 'Surface'
THEN pd.surface
WHEN 'Height'
THEN pd.height
WHEN 'Thickness'
THEN pd.thickness
END LIKE '%' + #criteria1 + '%'
Sorry in advance due to being new to Rstudio...
There are two parts to this question:
1) I have a large database that has almost 6,000 tables in it. Many of these tables have no data in them. Is there a code using R to only pull a list of tables names that have data in them?
I know how to pull a list of all table names and how to pull specific table data using the code below..
test<-odbcDriverConnect('driver={SQL Server};server=(SERVER);database=(DB_Name);trusted_connection=true')
rest<-sqlQuery(test,'select*from information_schema.tables')
Table1<-sqlFetch(test, "PROPERTY")
Above is the code I use to access the database and tables.
"test" is the connection
"rest" shows the list of 5,803 tables names.. one of which is called "PROPERTY"
"Table1" is simply pulling one of the tables named "PROPERTY".
I am looking to make "rest" only show the data tables that have data in them.
2) My ultimate goal, which leads to the second question, is to create a table that shows a list of every table from this database in column#1 and then column 2,3,4,etc... would include every one of the column headers that is contained in each table. Any idea how do to that?
Thanks so much!
The Tables object below returns a data frame giving all of the tables in the database and how many rows are in each table. As a condition, it requires that any table selected have at least one record. This is probably the fastest way to get your list of non-empty tables. I pulled the query to get that information from https://stackoverflow.com/a/14163881/1017276
My only reservation about that query is that it doesn't give the schema name, and it is possible to have tables with the same name in different schemas. So this is likely only going to work well within one schema at a time.
library(RODBCext)
Tables <-
sqlExecute(
channel = test,
query = "SELECT T.name TableName, I.rows Records
FROM sysobjects t, sysindexes i
WHERE T.xtype = ? AND I.id = T.id AND I.indid IN (0,1) AND I.rows > 0
ORDER BY TableName;",
data = list(xtype = "U"),
fetch = TRUE,
stringsAsFactors = FALSE
)
This next part uses the tables you found above and then gets the column information from each of those tables. Lastly, it makes on single data frame with all of the column names.
Columns <-
lapply(Tables$TableName,
function(x) sqlColumns(test, x))
Columns <- do.call("rbind", Columns)
sqlColumns is a function in RODBC.
sqlExecute is a function in RODBCext that allows for parameterized queries. I tend to use that anytime I need to use quoted strings in a query.
I need some guidance about making two different but related queries in Access:
Query 1: Table 1 joins on matches in Table 2 using two fields and using OR (i.e. can match on one field or the other).
Query 2: Table 1 joins on non-matches (excludes) in Table 2 using two fields and using OR (i.e. can match on one field or the other)
1: note the parenthesis (you could also do this in join but my preference is in the where statement) This is approximate code, the syntax may be slightly off for Access SQL but it should help point you in the right direction.
WHERE ((table1.fieldA = table2.fieldB
AND table1.fieldA = table2.fieldC) OR
table1.fieldA = table2.fieldD)
2:
FROM table1
LEFT JOIN Table2
ON (table1.fieldA = table2.fieldB
AND table1.fieldA = table2.fieldC)
OR table1.fieldA = table2.fieldD
WHERE (IS NULL table2.fieldB AND
IS NULL table2.fieldC)
OR IS NULL table2.fieldD
What syntax / tables can be used to determine the size (Gbs) of a Netezza table? I am accessing via UNIX SAS (either ODBC or libname engine). I assume there is a view which will give this info?
So you're interested in two system views _v_obj_relation_xdb and _v_sys_object_dslice_info. The first (_v_obj_relation_xdb) contains the table information (name, type, etc.) and the second (_v_sys_object_dslice_info) contains the size per disk information. You probably want to take a look at both of those tables to get a good idea of what you're really after, but the simple query would be:
select objname, sum(used_bytes) size_in_bytes
from _V_OBJ_RELATION_XDB
join _V_SYS_OBJECT_DSLICE_INFO on (objid = tblid)
where objname = 'UPPERCASE_TABLE_NAME'
group by objname
This returns the size of the table in bytes and I'll leave the conversion to GB as an exercise to the reader. There are some other interesting fields there so you might want to check out those views.
You could also use (_v_sys_object_storage_size )
select b.objid
,b.database as db
,lower(b.objname) as tbl_nm
,lower(b.owner) as owner
,b.objtype
,d.used_bytes/pow(1024,3) as used_gb
,d.skew
,cast(b.createdate as timestamp) as createdate_ts
,cast(b.objmodified as timestamp) as objmodified_ts
from _v_obj_relation_xdb b inner join
_v_sys_object_storage_size d
on b.objid=d.tblid
and lower(b.objname) = 'table name'
The size on disk (used_bytes) represents compressed data and includes storage for any deleted rows in the table.
The table rowcount statistic (reltuples) is generally very accurate, but it is just a statistic and not guaranteed to match the "select count(*)" table rowcount.
You can get this information via a catalog query
select tablename, reltuples, used_bytes from _v_table_only_storage_stat where tablename = ^FOOBAR^;