Solved

Further to Question on Cleaning up data

Posted on 2016-07-24
5
78 Views
Last Modified: 2016-07-24
In this link I was  trying to create a stored procedure that will allow me to just show one record for each person/team position.

I know need to link this to an additional table (table b)  using a "left outer join" i.e allow me to see where I don't have records in the second table, the key fields are id and teammember_id and whenever I have tried modifying the existing stored procedure it seems to do a cross join i.e showing a row of data in table a for every entry in table b.

The existing query is:

SELECT d.id,d.ClubId, d.TeamId, d.FirstName, d.Surname, d.TeamPosition, d.status, d.Seasonid, c.position, d.showit
FROM (SELECT * , ROW_NUMBER() OVER (PARTITION BY firstname, surname, teamposition
                             ORDER BY ID DESC) AS rn
FROM teammembers
) AS d, Static_Position as c
WHERE d.rn = 1 and (d.ClubId =@clubid ) AND (TeamPosition <> 5)and teamid = @teamid and TeamPosition = c.posid

I need to add a the following field Cost  from the second table and filter on the seasonid value in this table.

The table has 3 fields Teammember_id, Cost and Seasonid

Help!

Mark
0
Comment
Question by:markej
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41726451
this should do

SELECT d.id,d.ClubId, d.TeamId, d.FirstName, d.Surname, d.TeamPosition, d.status, d.Seasonid, c.position, d.showit
, t.cost
FROM (SELECT * , ROW_NUMBER() OVER (PARTITION BY firstname, surname, teamposition
                             ORDER BY ID DESC) AS rn
FROM teammembers
) AS d
join Static_Position as c
on 1=1
,left join table3 t
on t.teammemberid = d.id
and t.seasonid = d.seasonid

WHERE d.rn = 1 and (d.ClubId =@clubid ) AND (TeamPosition <> 5)and teamid = @teamid and TeamPosition = c.posid
0
 

Author Comment

by:markej
ID: 41726460
Hi When I try that I'm getting the following error "Incorrect syntax near the keyword 'left'."

Also I've discovered that the seasonid has not been fully populated can I use in the where clause t.seasonid = @seasonid and remove "and t.seasonid = d.seasonid" ?
0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 50 total points
ID: 41726461
remove the comma before left.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 41726462
I agree

SELECT d.id,d.ClubId, d.TeamId, d.FirstName, d.Surname, d.TeamPosition, d.status, d.Seasonid, c.position, d.showit
, t.cost
FROM (SELECT * , ROW_NUMBER() OVER (PARTITION BY firstname, surname, teamposition
                             ORDER BY ID DESC) AS rn
FROM teammembers
) AS d
join Static_Position as c
on 1=1
Left join table3 t
on t.teammemberid = d.id


WHERE d.rn = 1 and (d.ClubId =@clubid ) AND (TeamPosition <> 5)and teamid = @teamid and TeamPosition = c.posid
0
 

Author Comment

by:markej
ID: 41726492
Thank you gents definitely saved me.
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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

735 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