I want to use special characters in BigQuery columns but when using it it is throwing error as : Syntax error: Expected end of input but got "/" at [2:16].
Query :
SELECT *,
buget as budget/bin
FROM `dummy.book1`
Thanks
I don't think that's possible. BQ has column name naming rules so even when you do SELECT x AS y, y still has to follow those rules
Related
I am trying to concat two columns with concat and concat_ws function. Along with concatinating two columns I also want to append a word to this concatination. So I tried to achieve that using below method
SELECT CONCAT(CONCAT("SRID=4326;POINT(", CONCAT_WS(" ",cast(A.longitude as string),cast(A.latitude as string))), ")") as the_geom
FROM test
With above syntax I am getting the following error.
**org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:13 cannot recognize input near 'CONCAT' '(' 'CONCAT' in expression specification**
I was not knowing what wrong I am doing in the above syntax. Is there any alternative way to achieve this.
Expected RESULT : SRID=4326;POINT(127.155104 35.8091378)
I tried all ways using concat and concat_ws, but getting syntax issues.
The problem is semicolon, it breaks the query. Try replacing semicolon with \073, also double backslash may work \\;
Also it seems single concat is enough.
Demo using \073 :
with test as (
select 12134.12345 as longitude, 12134.12345 as latitude
)
SELECT CONCAT("SRID=4326\073POINT(",
CONCAT_WS(" ",cast(A.longitude as string),cast(A.latitude as string))
, ")"
) as the_geom
FROM test A
Result:
SRID=4326;POINT(12134.12345 12134.12345)
I tried full text search by postgresql with this code
SELECT * FROM test_table WHERE MATCH (discription) AGAINST ('remote controller');
name of column is "discription"
keyword is "remote controller"
error message is here
ERROR: syntax error at or near "AGAINST"
1: ...LECT * FROM test_table WHERE MATCH (discription) AGAINST ('...
I can not figure out what's wrong??
As Gordon Linoff said, you are using MySQL syntax in Postrgres. The alternative query for Pstgreses is the following:
SELECT *
FROM test_table
WHERE to_tsvector(discription) ## to_tsquery('remote controller');
In a nutshell... the ts_vector data type is provided for storing preprocessed documents (is a sorted list of distinct lexemes, which are words that have been normalized to merge different variants of the same word). When the tsquery is used to represent processed queries.
more information you can find in the following links:
https://www.postgresql.org/docs/12/textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH
https://www.postgresql.org/docs/12/textsearch.html
Note: to improve the search performance you can use also index on this:
https://www.postgresql.org/docs/12/textsearch-tables.html#TEXTSEARCH-TABLES-INDEX
This query will work:
SELECT * FROM test_table WHERE
difference(discription, 'remote controller') > 2;
The Soundex system is a method of matching similar-sounding names by converting them to the same code.
The soundex function converts a string to its Soundex code. The difference function converts two strings to their Soundex codes and then reports the number of matching code positions. Since Soundex codes have four characters, the result ranges from zero to four, with zero being no match and four being an exact match.
NOTE : the difference function gives the difference between the soundex code of two string.
In Postgresql database I have a column called names where I have some names which need to be parsed using regex to clean up punctuation parts. I am able to get a clean name using regexp_replace as follows:
select regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g')
from tableA
However, I would like to compare with some strings that are also cleaned of punctuation. How can I use similar to with the formed regular expression?
select name
from tableA
where (lower(name) ~ '\.COM|''[A-Za-z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as nameParsed similar to '(fg )%' and
(lower(name) ~ '\.COM|''[A-Za-z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as nameParsed similar to '%( cargo| carrier| cartage )%'
With the previous query I am getting this error:
LINE 3: ...-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)') as namePar...
I have tried in where clause like this and it seems to be working:
select name
from tableA
where (select lower(regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g'))) similar to '(fg )%'
Is this the best approach? The execution time went to 46 seconds :(
Thanks in advance
You're trying to get a column name in a WHERE clause (is a comparison, not a column). So, you can use as follows:
SELECT name
FROM "tableA"
WHERE (regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g') similar to '(fg )%'
OR regexp_replace(name,'\.COM|''[A-Z]|[^a-zA-Z0-9 -]+|\s(?=&)|(?<!\w\w)(?:\s+|-)(?!\w\w)','','g') similar to '%( cargo| carrier| cartage )%');
Alternatively, you can use ilike instead of similar to if you want to find a specific word.
I have this portion of a Oracle SQL query (lots more above it that doesn't apply to the question)
...
authorw as (
select a.id, (sum(p.w)) "theWeightOfTheAuthor"
from ac a, pc p, authorpublication ap
where a.id = ap.aid and ap.pid = p.id
group by a.id)
select authorCount.id "ID", auth.name "NAME", authorCount.c "TOTAL_NUMBER_OF_PUBS",
athw.theWeightOfTheAuthor "W_SCORE",
(authorCount.C / athw.theWeightOfTheAuthor) "MULT"
from ac authorCount, authorw athw, Author auth
where authorCount.id = athw.id and authorCount.id = auth.id
order by TOTAL_NUMBER_OF_PUBS desc;
where I am receiving an error:
ORA-00904: "ATHW"."THEWEIGHTOFTHEAUTHOR": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 404 Column: 22
Line 404 being the fourth from last line:
(authorCount.C / athw.theWeightOfTheAuthor) "MULT"
NOTE: I can access athw.id just fine, and if I execute up to the authorw creation, the table is printed out correctly with the theWeightOfTheAuthor column as expected. What gives?
Either remove the quotes around "theWeightOfTheAuthor" when you define it, or add quotes when you use it. Quoting the name when defining it makes the name case-sensitive, and because Oracle changes all non-quoted identifiers to UPPER CASE, your reference to the field is actually looking for ATHW.THEWEIGHTOFTHEAUTHOR, which doesn't exist.
A basic rule of Oracle programming is - never quote identifiers. It's a pain. Just don't do it.
Best of luck.
You've specified the column alias in double quotes, with mixed case, as "theWeightOfTheAuthor". When you use double quotes for a column name, Oracle preserves case. When you refer to it without quotes, as athw.theWeightOfTheAuthor, Oracle automatically converts it to upper-case. So the two don't match.
My suggestion is to remove the double quotes from the alias, so it will also be interpreted as upper-case. Alternatively, you could use double quotes for all references to this column, but I don't see any benefit to using mixed case in the column name. (You can still write it as mixed case, for readability, but Oracle will see it as all upper case.)
I have a table and one of the columns is named CAST. How can I access this column. I've tried
Select [Cast] AS cast_s FROM tablename without success, Can I use this name or must I reimport all my data into bigquery?
I know that cast is a function. This is the error message:
Error:
Encountered " "CAST" "Cast "" at line 10, column 63. Was expecting: < E O F > (EOF has no spaces, markdown makes it disappear)
Thanks.
The lexical rules for BQ use backticks for this purpose:
select `cast` as cast_s
from tablename;
The documentation is here.
For BigQuery Legacy SQL you CAN use square brackets
SELECT [cast] as cast_s
FROM tablename
From documentation
You can use square brackets to escape reserved words so that you can
use them as field name and aliases. For example, if you have a column
named "prefix", which is a reserved word in BigQuery syntax, the
queries referencing that field will fail with obscure error messages
unless you escape it with square brackets:
SELECT [prefix] FROM ...