Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server Join

Status
Not open for further replies.

ShaimaaT

Programmer
Aug 18, 2014
16
EG
Hi all,

Can someone help explain to me how when I have 12 rows in table A and 10 in B and I do an inner join , I would get more rows than

in both A and B ?

Same with left and right joins...

This is just a simplified example. Let me share one of my issues with you

I have 2 views ; which was originally SQL on 2 base tables Culture and Trials.

And then when attempting to add another table Culture Steps, one of the team members separated the SQL into 2 views

Since this produces an error when updating(modification cannot be done as it affects multiple base tables), I would like to get

back to changing the SQL such that I no longer use the views but achieve the same results.

One of the views has

SELECT some columns
FROM dbo.Culture RIGHT JOIN
dbo.Trial ON dbo.Culture.cultureID = dbo.Trial.CultureID LEFT OUTER JOIN
dbo.TrialCultureSteps_view_part1 ON dbo.Culture.cultureID = dbo.TrialCultureSteps_view_part1.cultureID


The other TrialCultureSteps_view_part1 view

SELECT DISTINCT dbo.Culture.cultureID,
(SELECT TOP (1) WeekNr
FROM dbo.CultureStep
WHERE (CultureID = dbo.Culture.cultureID)
ORDER BY CultureStepID) AS normalstartweek
FROM dbo.Culture INNER JOIN
dbo.CultureStep AS CultureStep_1 ON dbo.Culture.cultureID = CultureStep_1.CultureID


So how can I combine the joins the achieve the same results using SQL only on tables without the need for views?
 
A view simply executes the query, which defines it. So you would do exactly these queries and get the same data, wouldn't you?

To do the query in the part2 view using the part1 view, you'd simply define a CTE instead.

Code:
WITH part1 AS
(SELECT DISTINCT dbo.Culture.cultureID,
(SELECT TOP (1) WeekNr[URL unfurl="true"]http://tek-tips.com/viewthread.cfm?qid=1736521[/URL]
FROM dbo.CultureStep
WHERE (CultureID = dbo.Culture.cultureID)
ORDER BY CultureStepID) AS normalstartweek
FROM dbo.Culture INNER JOIN
dbo.CultureStep AS CultureStep_1 ON dbo.Culture.cultureID = CultureStep_1.CultureID
)
SELECT some columns
FROM dbo.Culture RIGHT JOIN
dbo.Trial ON dbo.Culture.cultureID = dbo.Trial.CultureID LEFT OUTER JOIN
part1 ON dbo.Culture.cultureID = part1.cultureID

Bye, Olaf.
 
Don't ask me how the tek tips URL came into the code section, remove it of course.

Bye, Olaf.
 
Hi Olaf,

Would I include part1.normalstartweek in the select some columns part?
 
You would put the same columns instead of "some columns" as you have in your old code.

ShaimaaT said:
SELECT some columns
FROM dbo.Culture RIGHT JOIN

This is from your own original post.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top