I am trying to create table using the query below. If I do not create table, but just run the part from SELECT *, the query can be run.
(SELECT *
FROM "MyDatabase"."2007" A
WHERE A."column name a" NOT IN ('U','A+','A','A-')
AND A."column name b" NOT IN ('SHH','CTP')
AND NOT EXISTS
(SELECT *
FROM "MyDatabase"."2008" B
WHERE (B."column name a" = A."column name a"
AND B."column name b" = A."column name b"
AND B."column name c" = A."column name c")))
The error message is "GENERIC_INTERNAL_ERROR: field ended by ';': expected ';' but got 'partOfAColName' at line 1:..."
From google search, space in column names seems to be the problem. But I am not sure. I have space in column names. The column names are automatically detected by Glue Crawler. So I am not sure if I can do anything about it. I have around 20 columns, all having space in the middle though. Could someone suggest a fix? Thanks.
When you execute CREATE TABLE AS...you are telling Athena to create a table with the same column names in your SELECT, but in this case those column names contain spaces, and Athena won't allow you to create a column name with a space. To avoid this you can create the table with column names that adhere to the Athena's specifications then populate that table with INSERT INTO SELECT...FROM
Related
I have a table that has just a code for the individual column name like "A1G", "Z8H" etc.
This table is a really huge table. Therefore it would not help to manually add the alias name in the SELECT Statement like.
The description for each column code is stored in a different table:
Now I would like to SELECT * from the first table but with the right column header name.
This is stored in the second table within the filters Schema = 'ABC' and Table = 'A'.
That would be desired output:
How would you do that in SQL?
How can I change the column name?
You would be better off just creating a view with all the columns aliased to your preferred names. Once that's done you can select from the view and get the data back with the headings you want.
Look into Inner Join
Or Left Join.
I'm very new to AWS & Athena. I'm using Athena to query a data file (CSV) from S3 using glue crawlers to create catalog and then querying that info. I have the catalog table created by glue, containing fName, sName, mName info. I'm trying to search a regexp pattern from all the rows and columns with a single query.
I have created a second table containing the column names of the primary table, i.e. fName, sName, mName.
I would like to loop through the second table rows -> using each value in my regexp_like function to search for any names starting with 'B'
e.g.
where regexp_like(fname,'^B')
where regexp_like(sname,'^B')
where regexp_like(mname,'^B')
and display all of them.
Is this possible? I have not been able to get the first query working even when hardcoding the search criteria
e.g.
select * from primary_table
where regexp_like((Select column from secondary_table where column_name='fname'),'^B')
above SQL -> Select column from secondary_table where column_name='fname' resolves to fname as string, not fname column in primary table.
Storing column names and table names as data is generally not recommended. In order to use the information, you need to use dynamic SQL -- that is, construct the query as a string and execute that.
You can get something similar using a lot of logic. But you have to check for each column explicitly:
select p.*
from primary p join
secondary s
on s.column_name = 'fname' and regexp_like(p.fname, '^B') or
s.column_name = 'sname' and regexp_like(p.sname, '^B') or
s.column_name = 'mname' and regexp_like(p.mname, '^B') ;
I have a column name "CustomerIDClass" and I need to find the table it's associated with within an entire Oracle database.
I've run this to determine the owner and name of the table where this column name appears:
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CustomerIDClass%';
and I'm getting this response:
I don't have enough reputation to post the image, so here's the link: http://i.imgur.com/a7rcKoA.png
I have no idea how to access this (BIN$Csew==) table. When I try to use it as a table name I get errors or messages saying that no rows were returned.
My main goal here is to write a simple statement that lets me search the database for the "CustomerIDClass" and view the table that contains this column name.
This table is in the recycle bin. You have to issue FLASHBACK TABLE "Customer1"."BIN$Csew==$0" TO BEFORE DROP command, given you have the appropriate privileges.
Doc: http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9012.htm
Do note that in oracle the column names are stored in capital but you are using mixed case in your like statement therefore the select clause will not return any result
Try the below
select * from DBA_TAB_COLUMNS
where COLUMN_NAME LIKE '%CUSTOMERIDCLASS%';
How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'
I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'