I am fairly new to SQL Server and have run into a problem building some joins. I come from an Oracle background so my first example here is what I want to do if I could use Oracle syntax. My problem is, how do I do this in the FROM clause in SQL Server?
SELECT *
FROM A,
B,
C,
D,
E,
F
WHERE A.x = C.x(+)
AND B.y = C.y(+)
AND A.x = D.x(+)
AND B.y = D.y(+)
AND A.x = E.x(+)
AND B.y = E.y(+)
AND A.x = F.x(+)
AND B.y = F.y(+)
What I'm trying to get is everything in A & B regardless if it there is joining values in C,D,E,F. BUT, if it is in C,D,E or F those tables must join on both A & B.
If it each table (C,D,E,F) was only linking to one other table (such as everything linking back to A) then I believe the joins would be this:
SELECT *
FROM A LEFT JOIN C ON A.x = C.x
LEFT JOIN D ON A.x = D.x
LEFT JOIN E ON A.x = E.x
LEFT JOIN F ON A.x = F.x
But when each table (C,D,E,F) have to left join back to A & B both, I'm not sure how to do that. Something similar to this maybe:
SELECT *
FROM A LEFT JOIN C ON A.x = C.x
AND B LEFT JOIN C ON B.y = C.y
LEFT JOIN D ON A.x = D.x
AND LEFT JOIN D ON B.y = D.y
LEFT JOIN E ON A.x = E.x
AND LEFT JOIN D ON B.y = D.y
LEFT JOIN F ON A.x = F.x
AND LEFT JOIN D ON B.y = D.y
This gets a little hairy because I am actually applying this to a SQL statement built off of several inline views. Here is a skeleton of the actual way I will be using the joins:
/**********************************************
** SELECT FIELDS FROM INLINE VIEWS
**********************************************/
SELECT a.processor,
b.date,
c.processor
c.date,
c.total,
d.processor,
d.date,
d.total,
e.processor,
e.date,
e.total,
f.processor,
f.date,
f.total
FROM
/*******************************************
** -------- INLINE VIEW A --------
*******************************************/
( SELECT distinct processor
FROM processors
) A LEFT JOIN
/*******************************************
** -------- INLINE VIEW B --------
*******************************************/
( SELECT date
FROM dates_table
) B LEFT JOIN
/*******************************************
** -------- INLINE VIEW C --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table1
GROUP BY processor,
date
) C
ON B.date = C.date
AND A.processor = C.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW D --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table2
GROUP BY processor,
date
) D
ON B.date = D.date
AND A.processor = D.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW E --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table3
GROUP BY processor,
date
) E
ON B.date = E.date
AND A.processor = E.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW F --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table4
GROUP BY processor,
date
) F
ON B.date = F.date
AND A.processor = F.processor
Any ideas how to get these joins to left join two tables???
Any help is MUCH appreciated!
SELECT *
FROM A,
B,
C,
D,
E,
F
WHERE A.x = C.x(+)
AND B.y = C.y(+)
AND A.x = D.x(+)
AND B.y = D.y(+)
AND A.x = E.x(+)
AND B.y = E.y(+)
AND A.x = F.x(+)
AND B.y = F.y(+)
What I'm trying to get is everything in A & B regardless if it there is joining values in C,D,E,F. BUT, if it is in C,D,E or F those tables must join on both A & B.
If it each table (C,D,E,F) was only linking to one other table (such as everything linking back to A) then I believe the joins would be this:
SELECT *
FROM A LEFT JOIN C ON A.x = C.x
LEFT JOIN D ON A.x = D.x
LEFT JOIN E ON A.x = E.x
LEFT JOIN F ON A.x = F.x
But when each table (C,D,E,F) have to left join back to A & B both, I'm not sure how to do that. Something similar to this maybe:
SELECT *
FROM A LEFT JOIN C ON A.x = C.x
AND B LEFT JOIN C ON B.y = C.y
LEFT JOIN D ON A.x = D.x
AND LEFT JOIN D ON B.y = D.y
LEFT JOIN E ON A.x = E.x
AND LEFT JOIN D ON B.y = D.y
LEFT JOIN F ON A.x = F.x
AND LEFT JOIN D ON B.y = D.y
This gets a little hairy because I am actually applying this to a SQL statement built off of several inline views. Here is a skeleton of the actual way I will be using the joins:
/**********************************************
** SELECT FIELDS FROM INLINE VIEWS
**********************************************/
SELECT a.processor,
b.date,
c.processor
c.date,
c.total,
d.processor,
d.date,
d.total,
e.processor,
e.date,
e.total,
f.processor,
f.date,
f.total
FROM
/*******************************************
** -------- INLINE VIEW A --------
*******************************************/
( SELECT distinct processor
FROM processors
) A LEFT JOIN
/*******************************************
** -------- INLINE VIEW B --------
*******************************************/
( SELECT date
FROM dates_table
) B LEFT JOIN
/*******************************************
** -------- INLINE VIEW C --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table1
GROUP BY processor,
date
) C
ON B.date = C.date
AND A.processor = C.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW D --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table2
GROUP BY processor,
date
) D
ON B.date = D.date
AND A.processor = D.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW E --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table3
GROUP BY processor,
date
) E
ON B.date = E.date
AND A.processor = E.processor
LEFT JOIN
/*******************************************
** -------- INLINE VIEW F --------
*******************************************/
( SELECT COUNT(total) total,
processor,
date
FROM table4
GROUP BY processor,
date
) F
ON B.date = F.date
AND A.processor = F.processor
Any ideas how to get these joins to left join two tables???
Any help is MUCH appreciated!