SQL Code Help Please

roblickley
roblickley used Ask the Experts™
on
I have the following query producing a recordset drawn from 2 tables.

SELECT
*
FROM
dbo.[qry_HorsesANDRidersAssociations]
Group By ClientID, HorseID, HorseName
UNION ALL
SELECT * FROM 
dbo.[qry_HorsesRidersHaveCompeted]
Group BY ClientID, HorseID, HorseName

Open in new window


It produces a list of horses that a rider "wants" to have associated on their account and also a list of horses they have actually ridden.

I have been asked for a facility to hide horses from a riders account - which is more applicable to the ones they have actually ridden rather than associated (as they can easily remove this association)

A new table tblHideHorse has been created with HorseID and ClientID along with HorseHideID (identity)

How can I combine this into the above query to produce the list minus the horses they select to not show?

I know it is something along the lines of

where clientid not in (select ClientID from tblHideHorse) but im afraid that is the limit of my knowledge.


Thanks in advance

MSSQL server 2014
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
This is what you need-

Use NOT EXISTS instead of NOT IN. NOT IN does not consider NULLs and slow also.

SELECT * FROM 
(
	SELECT
	*
	FROM
	dbo.[qry_HorsesANDRidersAssociations]
	Group By ClientID, HorseID, HorseName
	UNION ALL
	SELECT * FROM 
	dbo.[qry_HorsesRidersHaveCompeted]
	Group BY ClientID, HorseID, HorseName
)k
WHERE NOT EXISTS ( select NULL from tblHideHorse h WHERE h.ClientID = r.ClientID )

Open in new window

Author

Commented:
Sorry must be being thick - pasted your code and it doesnt work.

Multi-part identifier r.ClientId could not be bound?

in the horsehide table it has to be an exact match for horse and rider together. Not just any horse or any rider existing in that table?

does that make sense?
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
can you provide few rows with column names of  [qry_HorsesANDRidersAssociations] , dbo.[qry_HorsesRidersHaveCompeted]  and tblHideHorse  so that we will give u exact output ?
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I think at a minimum, this:

r.ClientID

should have been:

k.ClientID


»bp
Database Expert
Awarded 2016
Top Expert 2016
Commented:
Updated.
Please try ..changed alias name and added HorseID condition also.

SELECT * FROM 
(
	SELECT
	*
	FROM
	dbo.[qry_HorsesANDRidersAssociations]
	Group By ClientID, HorseID, HorseName
	UNION ALL
	SELECT * FROM 
	dbo.[qry_HorsesRidersHaveCompeted]
	Group BY ClientID, HorseID, HorseName
)r
WHERE NOT EXISTS ( select NULL from tblHideHorse h WHERE h.ClientID = r.ClientID  AND h.HorseID = r.HorseID )

Open in new window

Author

Commented:
Thank you very much - I would never have gotten there on my own!
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Welcome
glad to help as always :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial