When I do this query, it gives me the error "incorrect syntax near t1".
The query works if it's not within the CTE. How can you do this in CTE?
WITH CTE
AS (
SELECT DISTINCT t1.id
FROM ticket t1
WHERE...
UNION
SELECT t1.id
FROM ticket t1
,ticket t2
WHERE t1.id = t2.matching_ticket_id
AND t2.id NOT IN (
SELECT ticket_id
FROM aa
,pa
WHERE aa.id = pa.account_id
)
) t1 --> incorrect syntax near t1
JOIN (
SELECT DISTINCT t1.id
FROM ticket t1
WHERE t1.id NOT IN (
SELECT ticket_id
FROM aa
,pa
WHERE aa.id = pa.account_id
)
) t2 ON t2.id = t1.id --> incorrect syntax near t2
UNION
(
SELECT DISTINCT t1.id
FROM cd_ticket t1
WHERE...
UNION
SELECT t1.id
FROM cd_ticket t1
,cd_ticket t2
WHERE t1.id = t2.matching_ticket_id
AND t2.id NOT IN (
SELECT ticket_id
FROM cd_aa
,cd_pa
WHERE aa.id = pa.account_id
)
) t1 --> incorrect syntax near t1
JOIN (
SELECT DISTINCT t1.id
FROM cd_ticket t1
WHERE t1.id NOT IN (
SELECT ticket_id
FROM cd_aa
,cd_pa
WHERE aa.id = pa.account_id
)
) t2 ON t2.id = t1.id --> incorrect syntax near t2
You are confusing "table expressions" with "common table expressions", so you are mixing the syntax of both of them, into something that is not a legal SQL statement.
See the difference, and make up your mind on which one to use:
Table Expression (aka "derived table", "inline view", etc.):
select *
from (
select ... -- my complex select #1 here
) a
join (
select ... -- my complex select #2 here
) b on a.column_a = b.column_b
Table expressions appear in the FROM clause and they are named just after the closing parenthesis. The SELECT statement is written at the beginning.
Common Table Expression (CTE):
with
a as (
select ... -- my complex select #1 here
),
b as (
select ... -- my complex select #2 here
)
select * from a join b on a.column_a = b.column_b
As you see Common Table Expressions are declared first, the aliases are defined before the open parenthesis, and they are separated by commas. The main SELECT is written at the end. One advantage of CTEs is that you can use the common table expressions many times, in subsequent CTEs, and in the final/main SELECT.
It looks like you are trying to JOIN directly to the CTE; that is not how they work.
If you want join to a CTE:
WITH CTE AS ( somequery )
SELECT stuff
FROM aTable AS a INNER JOIN CTE AS c ON a.somefield = CTE.somefield
If you want the CTE to represent two joined subqueries, they need to be within the CTE:
WITH CTE AS (
SELECT stuff
FROM (
subquery1
) AS q1
INNER JOIN (
subquery2
) AS q2 ON ....)
SELECT stuff
FROM CTE
You declare a CTE but then go straight to a union which requires a preceding SELECT.
Your second query has a unncessary trailing bracket with a t1 alias but I assume you want that as a derived table so It will need an opening bracket before the SELECT
You probably want something like this
WITH CTE AS
(
select distinct t1.id from ticket t1 where ...
)
SELECT id FROM CTE
union
SELECT * FROM
(
select t1.id
from ticket t1,
ticket t2
where t1.id = t2.matching_ticket_id and
t2.id not in ( select ticket_id from aa, pa where aa.id = pa.account_id )
) t1
join
etc......
Also, as a side, you should steer away from the old JOIN syntax:
SELECT ticket_id
FROM aa, pa
WHERE aa.id = pa.account_id
and re-write as
SELECT ticket_id
FROM aa
JOIN oa
ON aa.id=pa.account_id
Related
I have about 10 tables that I make one big nested tables by rounds with the following query:
R1 AS(
SELECT ANY_VALUE(Table1).*, ARRAY_AGG(( SELECT AS STRUCT Table2.* EXCEPT(ID))) AS Table2
FROM Table1 LEFT JOIN Table2 USING(ID)
GROUP BY Table1.ID),
R2 AS(
SELECT ANY_VALUE(R1).*, ARRAY_AGG(( SELECT AS STRUCT Table3.* EXCEPT(ID))) AS Table3
FROM R1 LEFT JOIN Table3 USING(ID)
GROUP BY R1.ID),
...
SELECT ANY_VALUE(R9).*, ARRAY_AGG(( SELECT AS STRUCT Table10.* EXCEPT(ID))) AS Table10
FROM R9 LEFT JOIN Table10 USING(ID)
The thing is that for example in my first table I can have two records with the same ID but some other fields will be different and I want to consider them as two distinct records and thus group by all the fields of the table while I join.
Then I want to do the same with all the "sub-table" (the R tables in the query), so I will able to group by all the fields of the nested tables.
How can I do it easily ?
I tried GROUP BY Table1.* but it doesn't work...
Thank you in advance
Try to_json_string:
...
FROM Table1 t1
...
GROUP BY to_json_string(t1)
You seem to want something like this:
select *
from table1 t1 left join
(select t2.*
from table2 t2
where true
qualify row_number() over (partition by t2.id order by t2.id) = 0
) t2
using (id)
This uses qualify instead of group by to fetch one row.
If you don't want all rows from from table1, you can whittle them down as well:
select *
from (select t1.*
from table1 t1
where true
qualify row_number() over (partition by id, col1, col2 order by id) = 1
) t1 left join
(select t2.*
from table2 t2
where true
qualify row_number() over (partition by t2.id order by t2.id) = 0
) t2
using (id)
How to Group By all fields ...?
I tried GROUP BY Table1.* but it doesn't work...
Consider below example
SELECT ANY_VALUE(t1).*,
ARRAY_AGG(( SELECT AS STRUCT t2.* EXCEPT(ID))) AS Table2
FROM Table1 t1 LEFT JOIN Table2 t2 USING(ID)
GROUP BY FORMAT('%t', t1)
Hi I have small query and I am asking you after much googling. please let me know the solution.
Here my query is, I have joined two tables and make it the result as a table with alias name and that table (result) can i use it to join with another table inside the subquery ?
select *
from (select t1.col1, t1.col2, t2.col3
from tbl1 t1 join
tbl2 t2
on t1.col1 = t2.col1
) cte1
where round(sysdate - cte1.col3) * 24 > (120 +
(select count(distinct holidat_date)
from holiday_tbl join
cte1.location = hm.location
where hm.location = 'ABC'
) * 24
)
When i try to use common table variable c1 inside the sub query i am getting the error. If the query is not possible let me know the solution please...
Here my functionality is hours between the current date and cte1.col3 (it is a date), is there any holidays if yes how many?
Try modifying your query as below
select * from
(select t1.col1,t1.col2,t2.col3 from tbl1 t1 join tbl2 t2 on t1.col1=t2.col1) cte1
where round(sysdate-cte1.col3)*24>(120+(
select count(distinct holidat_date) from holiday_tbl hm where cte1.location=hm.location
and hm.location="ABC")*24)
First, cte1 is not returning anything called location. So, that is necessary.
Second, hm is not defined.
Third, the syntax isn't quite right in the subquery in the where:
select *
from (select t1.col1, t1.col2, t2.col3, ?.location
from tbl1 t1 join
tbl2 t2
on t1.col1 = t2.col1
) cte1
where round(sysdate - cte1.col3) * 24 >
(120 +
(select count(distinct holidat_date)
from holiday_tbl hm join
where hm.location = cte1.location and
hm.location = 'ABC'
) * 24
)
select *
from
(select *
from
(select t1.stdid
from t1) as table1
cross join
select *
from
(select t2.Subid
from t2) as table2
) as table3
Try the query below. You included an addition select * from that you should have left out.
select *
from (
select *
from (
select t1.stdid
from t1
) as table1
cross join
-- select * from : This line is extraneous and is causing your error.
(
select t2.Subid
from t2
) as table2
) as table3
Alternatively, you will get the same result if your query was written as:
select *
from (
select t1.stdid
from t1
) as table1
cross join
(
select t2.Subid
from t2
) as table2
Please don't use * in the query. Just select the column which you want.Please see below:
select ID
from
(select t1.id as ID
from
(select t1.id
from t1) as t1
cross join
(select t2.id
from t2) as t2
) as table3
I have a stored procedure which has multiple sql statments,
SELECT X,ID FROM TABLE1
SELECT Y,ID FROM TABLE2
SELECT Z,ID FROM Table3
WHEN I EXECUTE the above statements form procedure
My result set should be
ID, X,Y,Z will the columns and 1,10,20,30 will be the values of 1st row , 6, 40, 50, 60 will be the values of second row, ... goes on
I am using SQL SERVER 2005
Is it possible to do so ?
If there's only 1 row in each table you can just do the following:
select
(select X from table1) X,
(select Y from table2) Y,
(select Z from table3) Z;
Example SQLFiddle
For your second example, you can merely use join:
select
t1.id,
t1.X,
t2.Y,
t3.Z
from
table1 t1
inner join
table2 t2
on t1.id = t2.id
inner join
table t3
on t2.id = t3.id;
If the tables are really place holders for much larger queries, it might read more easily to use with
;with t1 as (
select id, X from table1
), t2 as (
select id, Y from table2,
), t3 as (
select id, Z from table3
) select
t1.id,
t1.X,
t2.Y,
t3.Z
from
t1
inner join
t2
on t1.id = t2.id
inner join
t3
on t2.id = t3.id;
to check the subquery having multiple select statement inside 'not in' condition
Eg.
select id from tbl where
id not in (select id from table1) and
id not in (select id from table2) and
id not in (select id from table3)
instead of repeating the same id 'not in' condition , i need the subquery which will check in one shot from multiple tables..
pls help..
Your query is better expressed as:
SELECT id
FROM tbl t
LEFT JOIN table1 t1 on t1.id = t.id
LEFT JOIN table2 t2 on t2.id = t.id
LEFT JOIN table3 t3 on t3.id = t.id
WHERE t1.id IS NULL AND t2.id IS NULL AND t3.id IS NULL
You could use a union, so you just have one in:
select id
from tbl
where id not in
(
select id from table1
union all select id from table2
union all select id from table3
)
Note: not in does not work well with nullable columns, but I assume id is not nullable here.
use union all
like this -->
select f.FIRST_NAME from farmer f where f.ID in (select v.ID from Village v where v.ID in (1,2) union all select s.ID from state s where s.ID in (3,4) )