Error in use PostGIS SQL function with two select statement - sql

I am new to PostGIS and SQL with a lot of things I need to learn.
I tried this SQL query:
Select ST_AsText(Select line from pathway1f)
I got errors.
Select line from pathway1f
This query works fine; thousands of lines are returned.
I want to decode the query result using ST_AsText().
It seems like I can't use two select statements. What's the correct syntax?

You can use the geometry column name directly
Select ST_AsText(line)
from pathway1f;

Related

Using Regex to determine what kind of SQL statement a row is from a list?

I have a large list of SQL commands such as
SELECT * FROM TEST_TABLE
INSERT .....
UPDATE .....
SELECT * FROM ....
etc. My goal is to parse this list into a set of results so that I can easily determine a good count of how many of these statements are SELECT statements, how many are UPDATES, etc.
so I would be looking at a result set such as
SELECT 2
INSERT 1
UPDATE 1
...
I figured I could do this with Regex, but I'm a bit lost other than simply looking at everything string and comparing against 'SELECT' as a prefix, but this can run into multiple issues. Is there any other way to format this using REGEX?
You can add the SQL statements to a table and run them through a SQL query. If the SQL text is in a column called SQL_TEXT, you can get the SQL command type using this:
upper(regexp_substr(trim(regexp_replace(SQL_TEXT, '\\s', ' ')),
'^([\\w\\-]+)')) as COMMAND_TYPE
You'll need to do some clean up to create a column that indicates the type of statement you have. The rest is just basic aggregation
with cte as
(select *, trim(lower(split_part(regexp_replace(col, '\\s', ' '),' ',1))) as statement
from t)
select statement, count(*) as freq
from cte
group by statement;
SQL is a language and needs a parser to turn it from text into a structure. Regular expressions can only do part of the work (such as lexing).
Regular Expression Vs. String Parsing
You will have to limit your ambition if you want to restrict yourself to using regular expressions.
Still you can get some distance if you so want. A quick search found this random example of tokenizing MySQL SQL statements using regex https://swanhart.livejournal.com/130191.html

Google BigQuery standart SQL insert nested row which has nested rows

I'm trying to figure out how to run an INSERT (i guess update?) query to insert into rows which are nested, but latter rows also have a nested schema in themselves.
For instance i have something like
What would a general template query look like?
I know that for the lines it would look something like
Managed to solve it with:
UPDATE test
SET lines=ARRAY(
SELECT line FROM UNNEST(lines) AS line
UNION ALL
SELECT (
'::someValue::,
ARRAY<STRUCT<STRING,STRING,FLOAT64>>[('someId','someString',1.0)]
)
WHERE id='someId'

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25

How can I get the column names returned by a dbo function

When I look at a view/table, I can get the column names using the following SQL query:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table'
Obviously, this will work to show columns for tables; but it does not work for functions. Is there an easy way (besides getting one record from a function, and manually parsing it) to get the column names returned by a function?
Does this help?
SELECT *
FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS
I'm unsure if this works for inline TVFs

SQL query where clause is too long

I've written an SQL query with a lot of 'or's in the 'where' clause:
i.e.
"SELECT * FROM myTable WHERE col1='a' or col1='b' or col1='c'...etc"
I'm trying to run a query in access via vb.net, but I keep getting "Query is too complex" error message.
I'm guessing I've hit some maximum limit. Anyone know a way around this, other than just to break it down into multiple queries?
How about using the IN operator instead?
SELECT Field1, Field2
FROM Table1
WHERE Field1 IN('Val1','Val2', ....)
If you query is that simple would you not be better using
SELECT * FROM myTable WHERE col1 in ('a','b','c')
but it would help to post the actual query so we can give a accurate answer
You could use SQL IN operator instead having multiple OR conditions.
You can use IN as in -
SELECT * FROM myTable WHERE col1 IN ('a','b','c','d');
Many have stated that you should use the IN-operator instead, but when that is used together with constants I believe the optimiser simply converts that to an OR-operator.
Instead you could load a temporary table with your constants and then use the IN-operator with that table.