SQL Server Query with Derived Table Produces Syntax Error - sql

Hi I have a situaltion similar to this
SELECT *
FROM
(
SELECT *
FROM
Table1
)
I wonder why this gives an error
Incorrect syntax near ')'.
Any help ? Thanks in Advance..

SELECT *
FROM
(
SELECT *
FROM
Table1
) x
You need to give your derived table a name.

Because you need to add an alias. Run it this way:
SELECT * FROM (
SELECT * FROM Table1
) T
Just for the record, MySQL displays the following error given the same situation :)
Every derived table must have its own alias

And you can use the AS keyword to make it more readable
SELECT * FROM ( SELECT * FROM table1 ) as table2

Related

Create new table using WITH clause

Is it possible to create a table based on a query that is using WITH clause?
The query:
with test as (
select 999 as col1
)
select * from test;
I tried :
select * into newtable from
(
with test as(
select 999 as col1
)
select * from test
) as newtable
But I get this error:
Msg 319, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'with'. If this statement is a common table expression, an
xmlnamespaces clause or a change tracking context clause
NOTE: My real query is more complex so I cannot remove the WITH clause.
You can't stuff a CTE inside a subquery like you're trying to do. Rather the insert syntax should be:
with test as (
select 999 as col1
)
select *
into newtable
from test;

CTE incorrect syntax near ")"

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...

Creating a table in Oracle using a query that contains a WITH clause within it

I am basically trying to run a create table statement using a query that has a with clause within it, but I am getting an error. Is there a different way to run this? The query statement is something like this:
CREATE TABLE DATA_TABLE AS
(
WITH X AS
(.....)
SELECT * FROM X
)
I would appreciate any help. Thanks.
Here's what you want.
CREATE TABLE t
AS
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
SELECT *
FROM some_data

SQL condition on unioned tables

I'm trying to execute a query with union and then put an additional condition on the result. Basically, I want to have something like
SELECT * FROM (A UNION B) WHERE condition
Is this possible to do? When I try to execute
SELECT * FROM (A UNION B)
SQL Management Studio complains about the closing bracket:
Incorrect syntax near ')'.
SELECT *
FROM ( SELECT * FROM A
UNION
SELECT * FROM B
) temp
WHERE condition
Here is the correct syntax:
SELECT *
FROM (select from A
UNION
select from B
) ab
WHERE condition;
I agree that the original syntax "looks right" for a from statement, but SQL doesn't allow it. Also, you need an alias on the subquery (requirement of SQL Server). Finally, you might consider using union all, if you know there are no duplicates in tables.
how about with CTE:
with tbl as (
select * from a
union
select * from b)
select * from tbl where condition
Use alias for derived table
SELECT * FROM (select * from A UNION select * from B) as t WHERE condition

SQL IN Operator with Select

I want to use the IN Operator in SQL with a select statement but I get the following error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
The code that I am using looks as follows:
select * from Table where ID in (select Units from Table2)
You have use WHEN instead of WHERE
SELECT * FROM `Table` WHERE `ID` IN (SELECT `Units` FROM `Table2`)
You should use where in place of when in your query. The correct query will be:
select * from Table where ID in (select Units from Table2)
You should try to select specific ID columns in your second query, something like this:
Select*from Table where ID IN (Select ID from Table2)