Hive ParseException: cannot recognize input near 'create' 'table' - hiveql

I am trying to conduct a hive query of the form
with (
select a,b from some_db.some_table
) as my_subquery
create table some_other_db.new_table as select * from my_subquery
And I am getting the error
cannot recognize input near 'create' 'table' 'some_other_db' in statement
How do I resolve?

The issue is in hive you cannot include create statements after a with statement. They need to be before.
The following query worked:
create table some_other_db.new_table as
with (
select a,b from some_db.some_table
) as my_subquery
select * from my_subquery

Related

pass value from With..as into a function (POSTGRESQL)

In Postgresql, I am trying to pass the result of a query into a function but I am getting
SQL Error [42703]: ERROR: column "pakito" does not exist Position:
176
WITH pakito AS(
select array(select job_id from job)
),
updating_job AS(
select internal_update_job_progress('xavier-tenant', pakito)
)
select * from pakito
Anyone has any idea why it wouldn't work and how to make it work?
Thanks!
You forgot FROM pakito in the second CTE.

For xml - similar function in postgresql [duplicate]

Let say you have a SELECT id from table query (the real case is a complex query) that does return you several results.
The problem is how to get all id return in a single row, comma separated?
SELECT string_agg(id::text, ',') FROM table
Requires PostgreSQL 9.0 but that's not a problem.
You can use the array() and array_to_string() functions togetter with your query.
With SELECT array( SELECT id FROM table ); you will get a result like: {1,2,3,4,5,6}
Then, if you wish to remove the {} signs, you can just use the array_to_string() function and use comma as separator, so: SELECT array_to_string( array( SELECT id FROM table ), ',' ) will get a result like: 1,2,3,4,5,6
You can generate a CSV from any SQL query using psql:
$ psql
> \o myfile.csv
> \f ','
> \a
> SELECT col1 AS column1, col2 AS column2 ... FROM ...
The resulting myfile.csv will have the SQL resultset column names as CSV column headers, and the query tuples as CSV rows.
h/t http://pookey.co.uk/wordpress/archives/51-outputting-from-postgres-to-csv
use array_to_string() & array() function for the same.
select array_to_string(array(select column_name from table_name where id=5), ', ');
Use this below query it will work and gives the exact result.
SELECT array_to_string(array_agg(id), ',') FROM table
Output : {1,2,3,4,5}
SELECT array_agg(id, ',') FROM table
{1,2,3,4}
I am using Postgres 11 and EntityFramework is fetching it as array of integers.

postgresql WITH statement doesn't find auxiliary statements

I have this query:
WITH words_not AS (
SELECT keywords.id
FROM keywords
WHERE keyword = any(array['writing'])
),actes_not AS (
SELECT actes_keywords.acte_id
FROM actes_keywords
WHERE actes_keywords.keyword_id IN (words_not)
)
SELECT actes.id,
actes.acte_date
FROM actes
WHERE actes.id <> all(actes_not);
This returns the following error:
ERROR: column "words_no" does not exist
LINE 1: ...ctes_keywords WHERE actes_keywords.keyword_id IN (mots_non))...
Each auxiliary statement in the WITH query is good (tested) and I thought I was staying pretty close to the manual: https://www.postgresql.org/docs/current/static/queries-with.html
I don't see why the auxiliary statement in the WITH query is not recognised.
You can't use a table reference in an IN (..) clause. You need a sub-query:
WITH words_not AS (
SELECT keywords.id
FROM keywords
WHERE keyword = any(array['writing'])
), actes_not AS (
SELECT actes_keywords.acte_id
FROM actes_keywords
WHERE actes_keywords.keyword_id IN (select id from words_not) --<< HERE
)
SELECT actes.id,
actes.acte_date
FROM actes
WHERE actes.id <> all(select id from actes_not); --<< HERE

Pass String in Postgres Query

I am using Postgresql 9.3 and what I am trying to do is Passing column names as string into my query. For newtable my column number can be dynamic sometimes it might be 3 or more for which I am trying to select column from another table and pssing relut of my query as string in the existing query
Please help how can i do this
select * from crosstab (
'select "TIMESTAMP_S","VARIABLE","VALUE" from archieve_export_db_a3 group by 1,2,3 order by 1,2',
'select distinct "VARIABLE" From archieve_export_db_variables order by 1'
) AS newtable (TIMESTAMP_S int,_col1 integer,_col2 integer);

T-SQL Why can I reffer only once to temporary object?

with tmp_rows as
(
select * from [dbo].[customer]
)
select * from tmp_rows;
select count(*) from tmp_rows;
I can't get the count of the tmp_rows because I get the error: Invalid object name 'tmp_rows'
If I comment the "select *" query everything is OK
I need to select all rows and then get their count, how to do that ?
with tmp_rows as
(
select * from [dbo].[customer]
)
select * from tmp_rows;
select ##rowcount;
By declaring you statement in using with you are declaring a CTE - further information on CTE's can be found here
A temporary object created using the with keyword can only be used once. You can create a temporary table if you want to use it more than once:
select *
into #tmp_tows
from dbo.customer
select * from #tmp_rows
select count(*) from #tmp_rows
drop table #tmp_rows
This works even if you want to do something different with the result twice, for example getting the count before the result.
The CTE starting with WITH ends with the semicolon ; .
But you can have more than 1 CTE in a WITH statement:
with tmp_rows as
(
select * from [dbo].customer
),
count_rows as
(
select COUNT(*) count_rows from tmp_rows
)
select * from count_rows, tmp_rows;
tmp_rows is a Common Table Expression (CTE), and CTEs are scoped at the statement level:
-- 1st statement, works fine.
with tmp_rows as (select * from [dbo].[customer])
select * from tmp_rows;
-- 2nd statement, returns an error, since tmp_rows is out of scope.
select count(*) from tmp_rows;
By the time your second statement executes, tmp_rows is already out of scope.
Be aware that CTEs are like locally-scoped views, not tables. The result set is never materialized. If you need to materialize a result set, use a local temporary table or table variable instead.