?
Solved

SQL - How to combine two table in Common table expression (cte)

Posted on 2012-09-07
6
Medium Priority
?
593 Views
Last Modified: 2012-09-08
I try to have a union of two tables in cte, it compain with errors
Is there any way around this problem or some other mind to combine two table like union.
0
Comment
Question by:tommym121
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38378747
show an example of your syntax ... if not the actual SQL.
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38379149
Same here. Doubt on the error itself :)
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38379155
Have a look at the example below.  I have done a UNION inside a CTE and, just in case, a UNION of two CTE's.  Without the SQL and/or the actual error message ... I would guessing what at the problem and solution would be.
IF OBJECT_ID('tempdb..#tempTable1') IS NOT NULL
	DROP TABLE #tempTable1;
create table #tempTable1 (
    id  int,
    val varchar(20)
);
insert into #tempTable1 values (1,'a'),(2,'b'),(3,'c'),(4,'d');
IF OBJECT_ID('tempdb..#tempTable2') IS NOT NULL
	DROP TABLE #tempTable2;
create table #tempTable2 (
    id  int,
    val varchar(20)
);
insert into #tempTable2 values (1,'x'),(2,'y'),(3,'z');

;with cte_with_union as (
select id, val
from #tempTable1
union all
select id, val
from #tempTable2
)
select *
  from cte_with_union;

;with cte_1 as (
select id, val
from #tempTable1
), cte_2 as (
select id, val
from #tempTable2
)
select *
  from cte_1
union all
select *
  from cte_2;

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:tommym121
ID: 38380008
This is the statement I try to make.  If I execute only the SQL statement within the cte. It works.  But if I have cte as below.  I got this error
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '{'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.
----------------------------------------------------------------------------
with cte As
{
SELECT [Sales Rep]
      FROM [SalesRepsTable]
      GROUP BY [Sales Rep]
      Having min([Rep Status]) < 1
      
union      
-- Give me a list of INactive advisor
      SELECT [Sales Rep]
      FROM [SalesRepsTable]
      GROUP BY [Sales Rep]
      Having min([Rep Status]) = 1
)
select * from cte
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 38380119
You have used the incorrect bracket to open the cte ... { instead of ( ... try
with cte As
(
SELECT [Sales Rep] 
      FROM [SalesRepsTable] 
      GROUP BY [Sales Rep]
      Having min([Rep Status]) < 1
      
union      
-- Give me a list of INactive advisor
      SELECT [Sales Rep] 
      FROM [SalesRepsTable] 
      GROUP BY [Sales Rep]
      Having min([Rep Status]) = 1
)
select * from cte

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 38380334
Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question