SYNTAX_ERROR: line 1:1: Column name not specified at position 1 - amazon-athena

i am trying to return the below query into a table but i get this error SYNTAX_ERROR: line 1:1: Column name not specified at position 1.
but whenever i just select it like SELECT replace(Pharma.Parent_Company, '"', '')
FROM Pharma it doesnt have error
CREATE TABLE PHARMA_NEW AS SELECT replace(Pharma.Parent_Company, '"', '')
FROM Pharma

As the error message says, you haven't specified a name for the column, only the expression used to create it. In some contexts, that's OK and you'll just get a result column with no name or a generated name, but in this context Athena wants you to choose the name yourself.
So change replace(Pharma.Parent_Company, '"', '') to replace(Pharma.Parent_Company, '"', '') AS whatever_column_name_you_want.

Related

How to use LISTAGG redshift

SELECT Distinct 'DROP TABLE IF EXISTS deleted.' || LISTAGG("table_name",',') || ';' FROM svv_all_columns WHERE schema_name = 'sn' and database_name='db';
ERROR:One or more of the used functions must be applied on at least one user created tables. Examples of user table only functions are LISTAGG, MEDIAN, PERCENTILE_CONT, et
SELECT Distinct 'DROP TABLE IF EXISTS deleted.' || LISTAGG("table_name",',') || ';' FROM svv_all_columns WHERE schema_name = 'sn' and database_name='db';
It concatanetes all table names in one drop statement
That's what LISTAGG() does - it aggregates strings from multiple rows together. It can work in conjunction with GROUP BY to combine strings from only within a group.
It sounds like you just want to have individual table names concatenated with the static text. Like this:
SELECT Distinct 'DROP TABLE IF EXISTS deleted.' || "table_name" || ';' FROM svv_all_columns WHERE schema_name = 'sn' and database_name='db'
Which will put each DROP statement on its own row. If instead you want one block of text you can use LISTAGG() over the combined strings like this:
SELECT LISTAGG('DROP TABLE IF EXISTS deleted.' || "table_name" || ';',',') FROM svv_all_columns WHERE schema_name = 'sn' and database_name='db';
Now DISTINCT doesn't make sense in this case as there is only one string. This could result in extra DROP statement so if you really need the minimum number of DROP statements AND for all of this to be in 1 string:
WITH drops AS (
SELECT Distinct 'DROP TABLE IF EXISTS deleted.' || "table_name" || ';' AS statement
FROM svv_all_columns WHERE schema_name = 'sn' and database_name='db'
)
SELECT LISTAGG(statement,' ')
FROM drops;
Note that I took the ',' out as the text separator as this doesn't make sense in a block of SQL.
Hopefully these examples will give you enough info to understand the basics of LISTAGG().

parameters binding in snowflake

I have below python method which insert data in to a table. the first column is json_data and the 2nd column is file name. I am getting both the values to this function while calling this method from main.
def insert(sf_handler,data,file_name):
query = """INSERT INTO my_table (DATA,FILE_NAME)
(select (PARSE_JSON('%s'),'%s'))""" % {json.dumps(data),file_name)}
pd.read_sql(query,sf_handler)
But while executing this i am getting below error. Can someone help on this.
TypeError: not enough arguments for format string
I got the answer. Just type cast the file name and remove the flower bracket with parenthesis it will work.
query = """INSERT INTO my_table (DATA,FILE_NAME)
(select (PARSE_JSON('%s'),'%s'))""" % (json.dumps(data),str(file_name))
pd.read_sql(query,sf_handler)

How do I link which will take me to the page along with filter

I'm trying to provide link which will take me to Page 2 along with filter in the column 'Managed_by' applied as 'Internal'
Select 'Open' label,
round(count(*),0) value,
'f?p='||:APP_ID||':2:'||:APP_SESSION||'::2::'||MANAGED_BY||':Internal'
from DATA
ORA-20999: Failed to parse SQL query! ORA-06550: line 2, column : ORA-00923: FROM keyword not found where expected
You can use APEX_PAGE.GET_URL or APEX_UTIL.PREPARE_URL function for build right URL.
You can use below query:
Select 'Open' label,
round(count(*),0) value,
CASE WHEN MANAGED_BY = 'internal' then
'<A HREF="f?P=&APP_ID.:2:&SESSION.::NO:RP,2:P2_internal:'||MANAGED_BY||'">
</A>'
ELSE null end as MANAGED_BY_link
From table_name;
By this Case statement, if 'internal' data is in MANAGED_BY column, it will create link and on click, it will redirect to page 2 item p2_internal, else link will be disabled.

Why is Django connection.cursor query failing?

I am getting the following error message when trying to execute a cursor query against a SQLite3 database:
My code is as follows:
qry = ('select balancer_security.id, balancer_security.name, balancer_security.symbol, '
'balancer_securityprice.at_dt, balancer_securityprice.price, balancer_securityprice.notes '
'from balancer_security LEFT OUTER JOIN balancer_securityprice '
'ON (balancer_security.id = balancer_securityprice.security_id '
'AND balancer_securityprice.at_dt="?") '
# 'AND balancer_securityprice.at_dt="%s") '
'ORDER BY balancer_security.name')
from django.db import connection
cursor = connection.cursor()
cursor.execute(qry, [date])
solution = cursor.fetchall()
The error occurs on the cursor.execute line. date is a string containing the value 2017-10-05
Also, is the parameter put into the query within django or is it passed to SQLite (i.e. should my parameter placeholder be %s or ?)?
Thanks!
Just use it without quotes. Change "%s" to just %s.

some order by in doctrine query language

I have a query as below :
SELECT t.id, t.name, count(i.id) FROM Acme\TestBundle\Entity\Tags t
LEFT JOIN t.items i WITH i.status=1 AND (SELECT count(img.id) FROM Acme\TestBundle\Entity\ItemsImages img WHERE img.item=i.id) > 0
GROUP BY t.id
ORDER BY COUNT (i.id) DESC
This query works fine without ORDER BY clause. Whenever I add this clause it gives me error :
[Syntax Error] line 0, col 297: Error: Expected end of string, got '('
ORDER BY works with column names but with columns like count(i.id) is not working
To order by an aggregated value, you need to SELECT it first and then use it to ORDER BY:
SELECT
t.id,
t.name,
count(i.id) AS tags_count
FROM
Acme\TestBundle\Entity\Tags t
...
ORDER BY
tags_count DESC