Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Further to Question on Cleaning up data

Posted on 2016-07-24
5
Medium Priority
?
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 30

Assisted Solution

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

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1800 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

722 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