Postgresql confuses property as table - sql

I'm trying to use a property that has a .(d0t) in the the propery name, but it seems like postgresql is confused and thinks that i'm trying to access a table property.
Here is the sql query i'm trying to run
select count(metadata.username), metadata.username from appointment join appointment_wrapper a on a.id=appointment_wrapper_id where property='state' and string_value='Kano' and timestamp > '2014-08-01' and timestamp < '2014-11-01' group by metadata.username;
When i run that query it says
ERROR: missing FROM-clause entry for table "metadata"
When i add quotes it says
ERROR: non-integer constant in GROUP BY
Any help on how to solve this?
Thank you

What you're looking for is a "quoted identifier" that you obtain by enclosing your column name in doublequotes to prevent it from being interpreted as a keyword:
SELECT count("metadata.username") from app group by "metadata.username";
Just a reminder that you really, really want to avoid the confusion that having reserved characters in your column or table names bring.

Related

Snowflake tables with TO, FROM as column names

Looks like we've loaded some snowflake tables via ELT with "TO, FROM" as column names and they are both classic functions in any sql tool
Whenever I run a query for specifically those columns, there's always an error - how do I fix it apart from changing column names? Don't want to change column names as ELT process always happens from mongoDB via log based replication (stitch data)
select * - works perfectly , all other columns work too. Just "to" , "from" is the issue - should that never be used a columns?
select to, from from table limit 10 ; // tested [to, "to", 'to'] - none work
Error: SQL compilation error: error line 1 at position 7 invalid identifier '"to"'
Any ideas how to fix this apart from source column change or snowflake column changes?
Snowflake uses the standard double quotes to escape identifiers. However, when identifiers are escaped, the case of the letters matters, So, these are not the same:
select "to"
select "To"
select "TO"
You need to choose the one that is correct for your column names.
In addition spaces matter, so these are not the same:
select "to "
select " to"
select "to"
That is, what looks like to might be something else. You need to know what that is to escape the name properly.
If you can't figure them out, there is a trick to create a view to give the table reasonable names. Something like this:
create view v_t (to_date, from_date, . . .) as
select *
from t;
You need to be sure to include all the column names in the table in the column name list, in the same order as they are in the table. Then you can use the view with reasonable names.

psql column doesn't exist but it does

I am trying to select a single column in my data table using raw SQL in a postgresql database from the psql command line. I am getting an error message that says the column does not exist. Then it gives me a hint to use the exact column that I referenced in the select statement. Here is the query:
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
Here is the error message:
ERROR: column insider_app_ownershipdocument.transactiondate does not exist
SELECT insider_app_ownershipdocument.transactionDate FROM in...
HINT: Perhaps you meant to reference the column "insider_app_ownershipdocument.transactionDate".
I have no idea why this is not working.
(Postgres) SQL converts names automatically to lower case although it support case-sensitive names. So
SELECT insider_app_ownershipdocument.transactionDate FROM insider_app_ownershipdocument;
will be aquivalent to:
SELECT insider_app_ownershipdocument.transactiondate FROM insider_app_ownershipdocument;
You should protect the column name with double quotes to avoid this effect:
SELECT insider_app_ownershipdocument."transactionDate" FROM insider_app_ownershipdocument;

A column named "date" in SQL

I have a query like this on sql
SELECT DISTINCT panel_pn_side, serial_nb, date, panel_stts
FROM table
and problem is, it doesn't get that date is a column name, maybe because to it, date is a keyword and not a column. I get the following error :
ERROR: column "date" does not exist
Do you guys have a solution for me ?
Thanks
Actually, even though it was shown as "date" in the database, after thorough research it appears that there were two columns, "date_timestampInUTC" and "date_originalTimezone" and that's why SQL didn't recognize the column named date. I have no idea what happened. Thank you all for the answers

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

SQL Queries "00904. 00000 - "%s: invalid identifier"

Hi I have the following code
SELECT entertainer_id,
entertainer_groupname
FROM casestudy_entertainer
INNER JOIN casestudy_availability ON
casestudy_entertainer.entertainer_id
= CASESTUDY_AVAILABILITY.AVAILABILITY_ENTERTAINERID
INNER JOIN casestudy_calendardates ON
CASESTUDY_AVAILABILITY.AVAILIBILITY_CALENDARDATEID
= casestudy_calendardates.calendar_Id
WHERE entertainer_type = '&Entertainer_TYPE'
AND casestudy_calendardates.calendar_date = '&Event_date'
And I don't seem to be able to figure out what its not liking when I run this.
It gives me the following error
ORA-00904: "CASESTUDY_AVAILIBILITY"."AVAILIBILITY_CALENDARDATEID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 7 Column: 4
I do have all the tables in place with all the correct rows.
The only thing is I have no data as of yet, Could this possibly be the issue?
You should try the lower case for the table/column identifiers(like in from/inner join clauses):
SELECT entertainer_id,
entertainer_groupname
FROM casestudy_entertainer
INNER JOIN casestudy_availability ON casestudy_entertainer.entertainer_id = casestudy_availability.availability_entertainerid
INNER JOIN casestudy_calendardates ON casestudy_availability.availibility_calendardateid = casestudy_calendardates.calendar_id
WHERE entertainer_type = '&Entertainer_TYPE'
AND casestudy_calendardates.calendar_date = '&Event_date'
This Error is caused by a special character in one of the columns of the database table. DBA will be able to help you.
You have same tablenames in your tables while left joining.
Change tablename of one , it will work.
no data is not the issue, you won't simply get a null result. ORA-00904 indicates, that you used a column that does not exist or does not comply to the Oracle specification.
Please check for correct naming. You might be better of with shorter names or at least table aliasses to get to code more readable.
Without knowing your table structure I cannot tell you where the error is. do a describe table_name;
It would also help to have the oracle version number SELECT * FROM V$VERSION or SELECT version FROM V$INSTANCE
and your client software you are using
What is interesting, Oracle gives that message not only if the name of the column is bad, but also if the name of the table/alias is bad - not defined alias, twice defined (as mentioned #PrajwalRai), error in the table name.
Old question, but maybe it helps:
sometimes you copy and paste text from a source that has a different character set ...
Type it over in sql-dev for instance and see if the error is gone
I was able to clear this error in an Oracle DB using Oracle SQL Developer by placing strings in single quotes instead of double quotes