'<EOF>' in subquery source in Hive query - sql

I am running a query on Hive similar to:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
);
the error is:
cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in subquery source

Adding a table alias for your subquery is necessary for Hive. Below I use 't1' as the alias:
SELECT *
FROM (SELECT a
FROM b
WHERE
sex = 'M'
AND degree = 'Bs'
AND age = 15
AND name LIKE 'L%'
) t1 ;

All the down-votes are unjustified. Hive often does not produce correct error and throws lazy "EOF" at you. In this case you just need to specify table alias for your sub-query. SELECT * FROM (.....) tbl_alias

Related

How to write sub query with where Condition and match the columns values

the following is my query
select * from tbl_incometax_master where (select Slabtitle=Gender,SlabSubTitle=Senior_CTZN_Type FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987)
when try to execute got following error in sql server 2008 r2 :
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Perhaps this is what you intend to do (Uses EXISTS):
select *
from tbl_incometax_master
where exists (
select 1
from etds.dbo.tbl_Employee_Master
where employee_id = 1218
and company_id = 1987
and Slabtitle = Gender
and SlabSubTitle = Senior_CTZN_Type
)
It's a correlated subquery, where the outer query checks if there is at least one row exists in the subquery for current row.
You dont have any column in where clause of parent query, it is important to know that only column criteria can be defined with subquery.
Sample query...
select * from tbl_incometax_master where <column> (select <subquery column> FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987)
You have a select clause in your Where with no operator (=,<,>,IN) ! And '=' inside the Select!
None of them are allowed in a SQL Query.
select * from tbl_incometax_master table1 LEFT JOIN
(
select Gender,Senior_CTZN_Type FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987
) table2
ON
table1.Slabtitle=table2.Gender
AND table1.SlabSubTitle=table2.Senior_CTZN_Type
WHERE table2.Slabtitle IS NOT NULL

SQL Error - Column does not exist (in SELECT as)

I am joining two tables: breeds + breed_characteristics (bc)
But I'm getting the following error:
PG::UndefinedColumn: ERROR: column "val" does not exist LINE 11
I'm not sure what's wrong, here is my SQL:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND val BETWEEN 4 AND 5
ORDER BY val DESC
(Executing this query on Postgres through Active Record)
You can't use expression alias val in where clause like that.
It's because there is an order in which SQL is executed specified in the SQL standard. Here, the WHERE clause is evaluated before SELECT and hence, the WHERE clause is not aware of the alias you created in the SELECT. The ORDER BY comes after the SELECT and hence can utilize aliases.
Just replace the alias with the actual case expression like this:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND CASE WHEN bc.user_val is NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END BETWEEN 4 AND 5
ORDER BY val DESC
However, you can use alias in order by clause.
One option to avoid restating the CASE expression in multiple places is to use a subquery:
SELECT *
FROM
(
SELECT b.*,
bc.characteristic_id,
CASE WHEN bc.user_val IS NULL THEN bc.value
ELSE (bc.value + (bc.user_val / 2)) / 2
END AS val
FROM breed_characteristics bc
INNER JOIN breeds b
ON breeds.id = bc.breed_id
) t
WHERE t.characteristic_id = 45 AND
t.val BETWEEN 4 AND 5
ORDER BY t.val DESC

Is it possible to use Informix NVL with two subqueries?

I want to get a parameter. The priority for getting that parameter is that I have to look for it in Table1, but if it doesn't exist there, I have to look for it in Table2. If not, so that parameter is null (this situation should not happen, but, well, there is always an edge case).
I wanted to try something like this:
SELECT NVL(
SELECT paramValue from Table1
where paramName = "paramName" and Id = "id",
SELECT paramValue from Table2
where paramName = "paramName" and Id = "id")
But it gives me a syntax error.
Is there any way of doing something like this?
Enclose the sub-queries in their own set of parentheses, like this:
SELECT NVL((SELECT Atomic_Number FROM Elements WHERE Name = 'Tungsten'),
(SELECT Atomic_Number FROM Elements WHERE Name = 'Helium'))
FROM sysmaster:informix.sysdual;
74
SELECT NVL((SELECT Atomic_Number FROM Elements WHERE Name = 'Wunderkind'),
(SELECT Atomic_Number FROM Elements WHERE Name = 'Helium'))
FROM sysmaster:informix.sysdual;
2
SELECT NVL((SELECT Atomic_Number FROM Elements WHERE Name = 'Wunderkind'),
(SELECT Atomic_Number FROM Elements WHERE Name = 'Helios'))
FROM sysmaster:informix.sysdual;
 
The last query generated a NULL (empty line) as output, which is mimicked by a non-breaking space on the last line.
Granted, I'm not selecting from two tables; that's immaterial to the syntax, and the sub-queries would work on two separate tables as well as on one table.
Tested with Informix 12.10.FC6 and CSDK 4.10.FC6 on Mac OS X 10.11.5.
There's another way:
SELECT * FROM (
SELECT paramValue from Table1
where paramName = "paramName" and Id = "id"
union all
SELECT paramValue from Table2
where paramName = "paramName" and Id = "id"
) x
LIMIT 1
Which is IMHO easier to read.

HIVE Create Table from SubQuery

this is my code:
create table sw_tmp6_gar_crm as
SELECT * FROM(
select as_fid_x_gara.dat_fine_perio as dat_fine_perio,
as_fid_x_gara.cod_soc as cod_soc,
as_fid_x_gara.cod_kto_gar as cod_kto_gar,
as_fid_x_gara.cod_fido as cod_fido,
fid.dat_delib as dat_delib,
fid.dat_scad as dat_scad
from it_soc_x_fv,
as_fid_x_gara ,
rt_fidi
where it_soc_x_fv.flg_tp_soc in ('C','N')
and as_fid_x_gara.dat_fine_perio = 2008-03-06
and as_fid_x_gara.cod_soc = it_soc_x_fv.cod_soc
and rt_fidi.dat_fine_perio = as_fid_x_gara.dat_fine_perio
and rt_fidi.cod_soc = as_fid_x_gara.cod_soc
and rt_fidi.cod_fido_tecnico = as_fid_x_gara.cod_fido
)
;
I receive the following error:
error while compiling statement: failed: parseexception line 10:9
cannot recognize input near 'it_soc_x_fv' ',' 'as_fid_x_gara' in from
source
Can you help me in that?
You need to give an alias name for the sub-query. The below one should work.
create table sw_tmp6_gar_crm as
SELECT * FROM(
select as_fid_x_gara.dat_fine_perio as dat_fine_perio,
as_fid_x_gara.cod_soc as cod_soc,
as_fid_x_gara.cod_kto_gar as cod_kto_gar,
as_fid_x_gara.cod_fido as cod_fido,
fid.dat_delib as dat_delib,
fid.dat_scad as dat_scad
from it_soc_x_fv,
as_fid_x_gara ,
rt_fidi
where it_soc_x_fv.flg_tp_soc in ('C','N')
and as_fid_x_gara.dat_fine_perio = 2008-03-06
and as_fid_x_gara.cod_soc = it_soc_x_fv.cod_soc
and rt_fidi.dat_fine_perio = as_fid_x_gara.dat_fine_perio
and rt_fidi.cod_soc = as_fid_x_gara.cod_soc
and rt_fidi.cod_fido_tecnico = as_fid_x_gara.cod_fido
) tmp
;
But as mentioned in the comments, you don't need a sub-query.
You haven't aliased the tables correctly. In your from statement you haven't mentioned any aliases for tables and in your select columns you are saying fid.dat_delib, fid.dat_scad.
But there is no fid table or alias in your query.
As mentioned earlier, there is no need of sub query, you can directly write a query with out sub query.

Oracle Condition inside IN CLAUSE

Hi I am writing oracle query to support select all for in clause my query goes something like this
SELECT * FROM country
WHERE
country_id in( IF('test' = 'test',(1,2,3),true) )
If condition ('test' = 'test') is true then it should fire query like
SELECT * FROM country WHERE country_id in(1,2,3)
Else it should fire query
SELECT * FROM country WHERE country_id in(true)
If I understand correctly what you need you will have to split it into 2 conditions:
SELECT *
FROM country
WHERE(('test'='test')AND(country_id IN (1,2,3)))
OR(('test'<>'test')AND(country_id<>0))