SQL Code Help Please

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
roblickleyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
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

0
roblickleyAuthor 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?
0
Pawan KumarDatabase ExpertCommented:
can you provide few rows with column names of  [qry_HorsesANDRidersAssociations] , dbo.[qry_HorsesRidersHaveCompeted]  and tblHideHorse  so that we will give u exact output ?
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Bill PrewCommented:
I think at a minimum, this:

r.ClientID

should have been:

k.ClientID


»bp
0
Pawan KumarDatabase ExpertCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
roblickleyAuthor Commented:
Thank you very much - I would never have gotten there on my own!
0
Pawan KumarDatabase ExpertCommented:
Welcome
glad to help as always :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.