A
AJ
Hi all,
I have this monster query (at least i think it is).
SELECT
c.ID, c.Company_Name, p.[level], 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package AS p ON s.Package_ID = p.ID
WHERE
c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20 AND
Company_ID = c.ID)
AND (INT(Start_Date) <= 38911) AND (INT(End_Date) >= 38911)
AND Company_Name LIKE "% Inc% "
ORDER BY p.[level] DESC , c.Company_Name, c.ID
UNION
SELECT
c.ID, c.Company_Name, p.[level], 2 As QueryNbr
FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN Package AS p ON s.Package_ID = p.ID
WHERE c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20
AND Company_ID = c.ID)
AND Company_Name LIKE "%Inc%"
ORDER BY c.Company_Name, c.ID
My issue is, i need it to return distinct companies only; In this case c.ID
is the column i require to be unique.
The UNION clause normally filters out duplicates, but because there is an
extra column needed ('QueryNbr') to indicate which query the results were
retrieve in (1 or 2) this filtering isn't having an effect.
The UNION appears to look for duplicates on a row by row basic rather than a
single column;
Has anyone got any ideas on how to get around this issue?
I am suffering with Access in this problem!!!
Cheers,
Adam
I have this monster query (at least i think it is).
SELECT
c.ID, c.Company_Name, p.[level], 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package AS p ON s.Package_ID = p.ID
WHERE
c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20 AND
Company_ID = c.ID)
AND (INT(Start_Date) <= 38911) AND (INT(End_Date) >= 38911)
AND Company_Name LIKE "% Inc% "
ORDER BY p.[level] DESC , c.Company_Name, c.ID
UNION
SELECT
c.ID, c.Company_Name, p.[level], 2 As QueryNbr
FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN Package AS p ON s.Package_ID = p.ID
WHERE c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20
AND Company_ID = c.ID)
AND Company_Name LIKE "%Inc%"
ORDER BY c.Company_Name, c.ID
My issue is, i need it to return distinct companies only; In this case c.ID
is the column i require to be unique.
The UNION clause normally filters out duplicates, but because there is an
extra column needed ('QueryNbr') to indicate which query the results were
retrieve in (1 or 2) this filtering isn't having an effect.
The UNION appears to look for duplicates on a row by row basic rather than a
single column;
Has anyone got any ideas on how to get around this issue?
I am suffering with Access in this problem!!!
Cheers,
Adam