I am trying to execute nested select statements in Hive -
select col1, (select COUNT(*) as cnt from table2) , col2 from table1;
When I run above query I am getting below exception -
FAILED: ParseException line 1:8 cannot recognize input near 'select' 'COUNT' '(' in expression specification
I also tried assigning select count(*) to hivevar and using it in the query. But, Still I am getting same issue.
set hivevar:cnt=select COUNT(*) as cnt from table2;
select col1, ${hivevar:cnt} , col2 from table1;
You may use a WITH clause and CROSS JOIN with the main table.
WITH t AS (SELECT COUNT (*) AS ct FROM table2)
SELECT s.col1, t.ct, s.col2
FROM table1 s CROSS JOIN t
Related
In SSMS we have a view, which is returning distinct columns by the following command,
Create View [VIEWNAME] As
`Select distinct [Col1],[Col2], Max(TimeDate) as TimeDate
from [Table]
Group By [Col1],[Col2]`
I want the column [Col3] as well from the table in view.
I tried the following so far but unfortunately, it didn't work for me
Select Distint on [Col1],[Col2] * from [Table]
Error: Incorrect syntax near 'on'.
Also,
select [Col1],[Col2],[Col3],Max[TimeDate] from [Table]
Group by [Col1],[Col2],[Col3],[TimeDate]
Error: Column TABLE.Col3 is invalid in the select list because it is not contained in either an aggregated function or GROUP BY clause.
Below is the original table sample.
enter image description here
Desired table image
enter image description here
Thanks.
You can use distinct on in Postgresql.
select distinct on (col1, col2) *
from the_table
order by col1, col2, timedate desc;
I guess that your RDBMS is SQL Server. The query below uses the SQL server equivalent of Postgresql distinct on ().
select col1, col2, col3, timedate
from
(
select *, row_number() over (partition by col1,col2 order by timedate desc) as rn
from the_table
) as t
where rn = 1;
SELECT
(SELECT COUNT(*) FROM table1) AS count1 WHERE date='2019-06-12',
(SELECT COUNT(*) FROM table2) AS count2 WHERE date='2019-06-12'
why is this statement causing a syntax error at or near ","?
need to add where clause inside subquery
SELECT
(SELECT COUNT(*) FROM table1 WHERE date='2019-06-12') AS count1 ,
(SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2
Try this-
SELECT
(SELECT COUNT(*) FROM table1 WHERE date='2019-06-12') AS count1 ,
(SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2
Let's rewrite the statement replacing the SELECT in parenthesis with a simple value:
SELECT
1 AS count1 WHERE date='2019-06-12',
2 AS count2 WHERE date='2019-06-12'
Now it's easy to see that up to the comma you have a valid SQL query, but you're appending further values that you want to select, which is invalid.
I assume what you want is to have the WHEREs inside the subqueries:
SELECT
(SELECT COUNT(*) FROM table1 WHERE date='2019-06-12') AS count1,
(SELECT COUNT(*) FROM table2 WHERE date='2019-06-12') AS count2
I want to select everything from mytable1 and combine that with just as many rows from mytable2. In my case mytable1 always has fewer rows than mytable2 and I want the final table to be a 50-50 mix of data from each table. While I feel like the following code expresses what I want logically, it doesn't work syntax wise:
Syntax error: Expected "#" or integer literal or keyword CAST but got
"(" at [3:1]
(SELECT * FROM `mytable1`)
UNION ALL (
SELECT * FROM `mytable2`
LIMIT (SELECT COUNT(*) FROM`mytable1`)
)
Using standard SQL in bigquery
The docs state that LIMIT clause accept only literal or parameter values. I think you can ROW_NUMBER() the rows from second table and limit based on that:
SELECT col1, col2, col3
FROM mytable1
UNION ALL
SELECT col1, col2, col3
FROM (
SELECT col1, col2, col3, ROW_NUMBER() OVER () AS rn
FROM mytable2
) AS x
WHERE x.rn <= (SELECT COUNT(*) FROM mytable1)
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
As your mytable1 always less column than mytable2 so you have to put same number of column by selection
select col1,col2,col3,'' as col4 from mytable1 --in case less column you can use alias
union all
select col1,col2,col3,col4 from mytable2
Can any one please help me regarding that error in SQL server
" SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
select * from
(select row_number() over ( order by (select null) ) rn,
(select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1)
)
where rn between 0 and 100
Try this:
select * from
(select row_number() over ( order by (select null) ) rn,a.test1,a.test2,a.test3
from (select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1) a
) b
where b.rn between 0 and 100
There are multi errors in your query:-
First one:-
Incorrect syntax near the keyword 'where'
Fix: Type alias for derived table (I am gonna type myTable as an alias below)
second one:-
No column name was specified for column 2 of 'myTable'
Fix: Type alias for derived column (I am gonna type myCol as an alias below)
Third one:- (Thant you mentioned in your question)
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Reason: You can't return two (or multiple) columns in your subquery.
The resolved Query:-
select * from
(
select
row_number() over ( order by (select null) ) rn,
(
select distinct test1
from table1
where table1.test1= 1
EXCEPT
select distinct test1
from table2
where table2.test1= 1
) myCol
) myTable
where rn between 0 and 100
Maybe the result is not what you need, but unless this a working query and now you have the keys for handling yours as your needs.
I'm trying to use a sql query that has the following structure:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE),
,anotherCTE...
I'm getting an "Incorrect syntax error near ')' near "myCTE)".
The error is pretty clear. You have an extra ):
myCTE)
should be
myCTE
As you already found out, the extra ) shouldn't be there:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE
The second error you reported is because you are attempting to do another CTE after selecting from the above CTE. You aren't allowed to do that, but end the CTE with a SELECT statement.
If you somehow need AnotherCTE for anything, you could resolve that by declaring it prior to the SELECT you already did, like so:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
,anotherCTE...
SELECT * FROM myCTE
However, that would imply you don't really need anotherCTE to begin with. If you do need it, simply move down the SELECT statement to be the final statement in your query.
you have to add a ; before WITHstatement
there is an extra ")" on SELECT * FROM myCTE
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE),
,anotherCTE...