3

There is an exact question like this for SQL server but I'm using Postgres 9.3 and the ',' didn't work

I am making a comparison between 2 separate years. e.g. 2016 and 2015

I check the user if he received any bad grades with same subjects over a course of 2 years.

My code goes as follows

with currentyeargrade ( 
      select ....
      from (...
             ...)t)

with previousyeargrade(
      select ....
      from (...
             ...)y)

select *
from currentyeargrade cyg
inner join previousyeargrade pvg on pvg.userid = cyg.userid

I am an SQL developer and I do not have admin privileges to create a temp table.

0

1 Answer 1

7

When you use CTE you only need to write with in the first CTE and use , comma to connect multiple CTE.

with currentyeargrade as ( 
      select ....
      from (...
             ...)t),
previousyeargrade as(
      select ....
      from (...
             ...)y)
select *
from currentyeargrade cyg
inner join previousyeargrade pvg on pvg.userid = cyg.userid

Here is a sample for you

CREATE TABLE T(col int);

insert into T values (1);

Query 1:

with currentyeargrade as  (SELECT * FROM T), 
previousyeargrade as (SELECT * FROM T)
select *
from previousyeargrade cyg
CROSS JOIN currentyeargrade pvg 

Results:

| col | col |
|-----|-----|
|   1 |   1 |
Sign up to request clarification or add additional context in comments.

3 Comments

the difference was you removed the with on the second line, trying that now!
Awesome. Perfect Answer! Slight difference between SQL Server but removing the with worked! Thanks
@Tito No problem glad to help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.