Doubts on SELECT of Hive SQL - sql

I am reading some Hive QL script and found this line:
SELECT 'Start time:',from_unixtime(unix_timestamp());
What does it mean? It does not look like a real "select" statement?

This will not query to DB. This is simply a Print statement.
If we run the command : SELECT 'Start time:',from_unixtime(unix_timestamp());
Out put would be :
Col1: Start time:
Col2: 2017-08-29 01:36:11

HIVE-178 SELECT without FROM should assume a one-row table with no columns
... People use this all the time with SQL Server. I expect people to
like it if it is added to Hive ...
... The Hive Language Manual now states, "As of Hive 0.13.0, FROM is
optional (for example, SELECT 1+1)."
Nothing really new -- that syntax was introduced by Sybase Transact-SQL 30 years ago, before they sold their "SQL Server" code base (and product name) to Microsoft, and long before they sold the rest to SAP.
The alternate syntax uses a DUAL (Oracle) or SYSDUMMY1 (DB2) pseudo-table.

Related

Creating a view in PostgresSQL for a query that worked in SQL Server (COUNT between two DATETIME columns/TIMECODE

I have a problem with Redshift where it throws out a syntax error when trying to create a view similar to the following:
SELECT
*,
(SELECT COUNT(*)
FROM [Leads - leads]
WHERE Receive_Time BETWEEN dbo.[Leads - extended spot summary fla32813].[Plan Air]
AND.dbo.[Leads - extended spot summary fla32813].[Plan End]) AS Leads
INTO
[32813DA]
FROM
.dbo.[Leads - extended spot summary fla32813]
`
This code has numerous errors from a Redshift perspective:
It is using square braces as delimiters. Redshift (and the SQL Standard) is double quotes.
The three-part naming doesn't look right for Redshift.
into is not allowed in views in any of the mentioned databases.
You may have other errors as well, but you need to get the names right before you start with anything related to logic.
The SELECT ... INTO ... syntax is specific to SQL Server and create a table from a SELECT. The ISO SQL syntax is CREATE TABLE AS (SELECT ...) but does not woks with SQL Server.
Use a more simple an sharable syntax like :
CREATE TABLE ??? (..);
INSERT INTO ???
SELECT ...

How to store the result of select statement into the temporary table in Oracle?

We can write select column1,column2 into #temp from tableName in SQL Server. But I am unable to write the same query in an Oracle database.
I want to store the result of select/insert/delete/update or any result set into a local temporary table in oracle database. How I can do this?
I am executing below query in my Oracle sql developer tool:
select * into #temp
from bmi;
but I am getting the error as follow please help to find this error.
when I execute the same query in Microsoft SQL Server it get executed & #temp table get created which is not present in the database but it can hold the data for that particular session. so i want same scenario in ORACLE database.
ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
Error at Line: 1 Column: 15
I want to store the result of select/insert/delete/update or any result set into a local temporary table in oracle database,How I can Do This?
You can't. Oracle doesn't have local temporary tables, it doesn't work like that. But it doesn't need to. Oracle has a very different internal model from SQL Server which means a lot of SQL Server practices are unnecessary in Oracle. (To be fair SQL Server has neat things which Oracle doesn't, like ANSI 92 Joins for DML.)
The key insight is: you don't want to store the result of select/insert/delete/update or any result set into a local temporary table. That is something you had to do in T-SQL to achieve the end goal of implementing some business logic. But what you actually wanted to do in SQL Server and what you want to do in Oracle is write some code which delivers value to your organisation.
So, with that mindset in place, what do you need to do?
If you want to loop round a result set then perhaps a Cursor Loop is what you're looking for?
for rec in ( select * from some_table
where the_date = date '2018-02-01' )
loop
...
If you want to work on some data prior to inserting it into a data then perhaps you should use a PL/SQL collection:
type l_recs is table of some_table%rowtype;
But maybe you just need to understand Oracle's Transaction Management model. A lot of things are possible in pure SQL without any need for procedural framework.
Create temporary table :
create global temporary table
results_temp (column1, column2)
on commit preserve rows;
and then insert to it from your table:
insert into results_temp (column1, column2 )
SELECT column1,column2
FROM source_table
create global temporary table temp_table_name
on commit preserve rows as select column1,column2,columnN from your_table;

Why does a "WITH" clause give a syntax error on Informix?

I am trying a query similar to the below one on Informix, but I get a syntax error (-201: "A syntax error has occurred") every time.
with a_qry (locationnames) as (SELECT * FROM TABLE(LIST{'abc','xyz'}))
select locationnames from a_qry;
Can someone please help with this?
Informix 14.10 and later
Informix 14.10 (released in March 2019) added support for the WITH statement (Common Table Expressions).
Informix 12.10 and earlier
The documentation for the Informix SELECT statement in Informix 12.10 (which was the latest when this question was asked) does not include the WITH clause because the server does not support the WITH clause and common table expressions (CTEs) — a grievous omission, but nevertheless a fact of life.
For your specific example, you could use:
SELECT locationnames
FROM (SELECT * FROM TABLE(LIST{'abc','xyz'})(locationnames));
which would yield:
abc
xyz
though the sub-query isn't necessary here, of course (you could simply use SELECT * FROM TABLE(LIST{'abc','xyz'})(locationnames) to get the same result). In general, though, you'll have to write out each reference to a CTE in full, with the consequential risk that the optimizer doesn't spot the commonality and therefore doesn't optimize as well as it might if it could.
You can try to use a temp table (which doesn't require special permission).
select * from (your query)
into temp myTmpTable;
select * from MyTmpTable;

BigQuery command line tool - append to table using query

Is it possible to append the results of running a query to a table using the bq command line tool? I can't see flags available to specify this, and when I run it it fails and states "table already exists"
bq query --allow_large_results --destination_table=project:DATASET.table "SELECT * FROM [project:DATASET.another_table]"
BigQuery error in query operation: Error processing job '':
Already Exists: Table project:DATASET.table
Originally BigQuery did not support the standard SQL idiom
INSERT foo SELECT a,b,c from bar where d>0;
and you had to do it their way with --append_table
But according to #Will's answer, it works now.
Originally with bq, there was
bq query --append_table ...
The help for the bq query command is
$ bq query --help
And the output shows an append_table option in the top 25% of the output.
Python script for interacting with BigQuery.
USAGE: bq.py [--global_flags] <command> [--command_flags] [args]
query Execute a query.
Examples:
bq query 'select count(*) from publicdata:samples.shakespeare'
Usage:
query <sql_query>
Flags for query:
/home/paul/google-cloud-sdk/platform/bq/bq.py:
--[no]allow_large_results: Enables larger destination table sizes.
--[no]append_table: When a destination table is specified, whether or not to
append.
(default: 'false')
--[no]batch: Whether to run the query in batch mode.
(default: 'false')
--destination_table: Name of destination table for query results.
(default: '')
...
Instead of appending two tables together, you might be better off with a UNION ALL which is sql's version of concatenation.
In big query the comma or , operation between two tables as in SELECT something from tableA, tableB is a UNION ALL, NOT a JOIN, or at least it was the last time I looked.
Just in case someone ends up finding this question in Google, BigQuery has evolved a lot since this post and now it does support Standard.
If you want to append the results of a query to a table using the DML syntax feature of the Standard version, you could do something like:
INSERT dataset.Warehouse (warehouse, state)
SELECT *
FROM UNNEST([('warehouse #1', 'WA'),
('warehouse #2', 'CA'),
('warehouse #3', 'WA')])
As presented in the docs.
For the command line tool it follows the same idea, you just need to add the flag --use_legacy_sql=False, like so:
bq query --use_legacy_sql=False "insert into dataset.table (field1, field2) select field1, field2 from table"
According to the current documentation (March 2018): https://cloud.google.com/bigquery/docs/loading-data-local#appending_to_or_overwriting_a_table_using_a_local_file
You should add:
--noreplace or --replace=false

Strange number comparison behaviour in Oracle 11.2.0.3.0 with existsNode

I'm trying to select all rows of a table where one column contains a specific xml-value.
It is working fine when I run the query against my local database or the one of our internal testservers. However, on the customers testserver, the query does not return any rows.
SELECT * FROM product WHERE existsnode(...) = 1
I found out that if I quote the comparison value, it works on all databases:
SELECT * FROM product WHERE existsnode(...) = '1'
I'd like to understand why this happens and what a good solution for this issue would be.
The database version is 11.2.0.3.0 on all systems.
EDIT:
I did some further research and found out that the returned datatype is actually a number, as stated in the Oracle documentation.
SELECT dump(existsNode(...)) FROM product;
Returns "Typ=2, Len=1: 128" on all databases.
However, I have some double quotes inside of the existsnode query string, and these double quotes seem to get lost on the databases with which I am having the strange comparison problem:
SELECT existsnode(xmltype(attributes), '/attrs/attr[#name="SomeFlag"]') FROM product;
On the databases where the query works, I get the following result as the column name:
EXISTSNODE(XMLTYPE(ATTRIBUTES), '/ATTRS/ATTR[#NAME="SOMEFLAG"]')
On the other databases I get
EXISTSNODE(XMLTYPE(ATTRIBUTES), '/ATTRS/ATTR[#NAME=SOMEFLAG]')
Check out document number, Doc ID 14087914.8 (Bug Number 14087914) on support.oracle.com.
Per the document, try to following to see if the results no produce what you are expecting.
alter session set "_fix_control"='9569678:off
Your other option, involves upgrading to 11.2.0.4.