boardtc
Programmer
- Dec 1, 2004
- 22
I'm sorry I'm not able to simplify this to make my question easier to understand.
In the below query there are can be more than 1 conditions records returned for each person (if there are duplicate max COND_TrainingStatusRO). I only want 1 per person. So say there are 10 people and for 1 person there are 2 conditions and 1 for the rest, this returns 11 records instead of 10.
If there are more than 1, I can distinguish them by getting the Max LastUpdated (commented out below). I can't get LastUpdated to equate to the max for each person...using the example above I might get 4 records with the below rather than 10.
I completely realise this might make no sense...but any ideas?
The query may well be optimisable but this is what my sql 'skills' are giving me!
query :
select
Per_FirstName,Per_LastName, COND_TrainingStatusRO
from Players
INNER JOIN Persons ON Players.OID = Persons.OID
INNER JOIN AssPlayerGroupPlayers ON Players.OID = AssPlayerGroupPlayers.APYGP_PlayerOID
INNER JOIN PlayerGroups ON AssPlayerGroupPlayers.APYGP_PlayerGroupOID = PlayerGroups.OID
inner Join PlayerCustoms ON PlayerCustoms.Owner_OID = Players.OID
LEFT OUTER Join Consultations ON PlayerCustoms.OID = Consultations.Owner_OID
LEFT OUTER Join Conditions ON Consultations.OID = Conditions.Owner_OID
where
(Players.Owner_OID = '{00000000-0000-0000-0000-000000000000}') and
(
(
Consultations.Cons_Date in (
( Select Max(Cons_Date)
from Consultations
where Owner_Oid = Players.OID
)
) or
Consultations.Cons_Date is null
) and
(
Conditions.Cond_TrainingStatusRO in (
( Select Max(Cond_TrainingStatusRO) from Conditions
where Owner_Oid = Consultations.OID
)
) or
Conditions.Cond_TrainingStatusRO is null
) and
-- conditions.LastUpdated = (
-- ( select Max(LastUpdated)
-- from conditions
-- where Owner_Oid = Consultations.OID
-- )
-- )
-- or
-- Conditions.LastUpdated is null
-- and
(PlayerGroups.PYG_Title = 'Development 1') and
(AssPlayerGroupPlayers.APYGP_Status = 0)
)
In the below query there are can be more than 1 conditions records returned for each person (if there are duplicate max COND_TrainingStatusRO). I only want 1 per person. So say there are 10 people and for 1 person there are 2 conditions and 1 for the rest, this returns 11 records instead of 10.
If there are more than 1, I can distinguish them by getting the Max LastUpdated (commented out below). I can't get LastUpdated to equate to the max for each person...using the example above I might get 4 records with the below rather than 10.
I completely realise this might make no sense...but any ideas?
The query may well be optimisable but this is what my sql 'skills' are giving me!
query :
select
Per_FirstName,Per_LastName, COND_TrainingStatusRO
from Players
INNER JOIN Persons ON Players.OID = Persons.OID
INNER JOIN AssPlayerGroupPlayers ON Players.OID = AssPlayerGroupPlayers.APYGP_PlayerOID
INNER JOIN PlayerGroups ON AssPlayerGroupPlayers.APYGP_PlayerGroupOID = PlayerGroups.OID
inner Join PlayerCustoms ON PlayerCustoms.Owner_OID = Players.OID
LEFT OUTER Join Consultations ON PlayerCustoms.OID = Consultations.Owner_OID
LEFT OUTER Join Conditions ON Consultations.OID = Conditions.Owner_OID
where
(Players.Owner_OID = '{00000000-0000-0000-0000-000000000000}') and
(
(
Consultations.Cons_Date in (
( Select Max(Cons_Date)
from Consultations
where Owner_Oid = Players.OID
)
) or
Consultations.Cons_Date is null
) and
(
Conditions.Cond_TrainingStatusRO in (
( Select Max(Cond_TrainingStatusRO) from Conditions
where Owner_Oid = Consultations.OID
)
) or
Conditions.Cond_TrainingStatusRO is null
) and
-- conditions.LastUpdated = (
-- ( select Max(LastUpdated)
-- from conditions
-- where Owner_Oid = Consultations.OID
-- )
-- )
-- or
-- Conditions.LastUpdated is null
-- and
(PlayerGroups.PYG_Title = 'Development 1') and
(AssPlayerGroupPlayers.APYGP_Status = 0)
)