Hi all,
I have a challenge (not a problem). Simplifing it as far as i could, i
have this situation:
Table Persons
Id (PK)
Name
Table Scores
FK_Person (PK)
Period (PK)
Value
Now, i want to make a stored procedure that returns the scores for all
people in a certain period. So i have:
CREATE PROCEDURE [dbo].[ScorePerson]
(@Period Int)
AS
SELECT dbo.People.Name , dbo.Scores.[Value]
FROM dbo.People LEFT OUTER JOIN
dbo.Scores ON dbo.People.Id =
dbo.Scores.FK_p erson
WHERE dbo.Scores.Peri od = @Period
GO
BUT: if a person has no score for a certain period, he will not show
up in the result set at all, but i do want him to. So what i need to
do is first get the subset for a period from the table Scores and THEN
Left join that to the persons, so i always get all persons and only a
value for the the score if there is one.
How can i do this? I could store the results in a temporary table but
that just feels 'unpure', wrong. Is there any way i can get this in a
single pass? Can i for example join two stored procedures with
parameters together in another SP that passes these params on to them?
TIA,
Gert-Jan
I have a challenge (not a problem). Simplifing it as far as i could, i
have this situation:
Table Persons
Id (PK)
Name
Table Scores
FK_Person (PK)
Period (PK)
Value
Now, i want to make a stored procedure that returns the scores for all
people in a certain period. So i have:
CREATE PROCEDURE [dbo].[ScorePerson]
(@Period Int)
AS
SELECT dbo.People.Name , dbo.Scores.[Value]
FROM dbo.People LEFT OUTER JOIN
dbo.Scores ON dbo.People.Id =
dbo.Scores.FK_p erson
WHERE dbo.Scores.Peri od = @Period
GO
BUT: if a person has no score for a certain period, he will not show
up in the result set at all, but i do want him to. So what i need to
do is first get the subset for a period from the table Scores and THEN
Left join that to the persons, so i always get all persons and only a
value for the the score if there is one.
How can i do this? I could store the results in a temporary table but
that just feels 'unpure', wrong. Is there any way i can get this in a
single pass? Can i for example join two stored procedures with
parameters together in another SP that passes these params on to them?
TIA,
Gert-Jan
Comment