Sql query

%3CLINGO-SUB%20id%3D%22lingo-sub-3071672%22%20slang%3D%22en-US%22%3ESql%20query%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3071672%22%20slang%3D%22en-US%22%3E%3CP%3ECould%20someone%20help%20me%20construct%20query%20to%20find%20location%20that%20only%20has%20HP%20in%20below%20table%3B%3C%2FP%3E%3CP%3Evendor%20location%3C%2FP%3E%3CP%3EHP%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20TX%3C%2FP%3E%3CP%3ECisco%26nbsp%3B%26nbsp%3B%20TX%3C%2FP%3E%3CP%3EDell%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20TX%3C%2FP%3E%3CP%3EHP%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CA%3C%2FP%3E%3CP%3EHP%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WA%3C%2FP%3E%3CP%3EDell%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WA%3C%2FP%3E%3CP%3EHP%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20OR%3C%2FP%3E%3CP%3ECisco%26nbsp%3B%20OR%3C%2FP%3E%3CP%3EHP%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WI%3C%2FP%3E%3CP%3EDell%26nbsp%3B%26nbsp%3B%20WI%3C%2FP%3E%3CP%3ECisco%20WI%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Could someone help me construct query to find location that only has HP in below table;

vendor location

HP       TX

Cisco   TX

Dell     TX

HP      CA

HP      WA

Dell    WA

HP     OR

Cisco  OR

HP     WI

Dell   WI

Cisco WI

12 Replies

Hi @Alex_Kim245 --

 

Try something like the query below.  Take care.

SELECT
	Vendor
	,VendorLocation
FROM
	Vendor
WHERE
	Vendor = 'HP'

bake13_0-1643200458397.png

 

 

This will not work because TX has Cisco and Dell also. I'm trying to get the ones that only have HP.

Hi @Alex_Kim245 --

 

Apologies, I misunderstood.  Something like the query below might work.  Take care.

SELECT *
FROM (
   SELECT 
		Vendor
		,VendorLocation
        ,row_number() over (partition by VendorLocation order by Vendor) as rownum
  FROM
		Vendor
) t
WHERE
	rownum = 1
	AND
	Vendor = 'HP'	
Getting syntax error in From clause.

Hi @Alex_Kim245 --

 

What is the error, please?

syntax error in From clause.
this section right here;
FROM
Vendor
) t
table name is table1 so Vendor should be table1? and what is t?

Hi @Alex_Kim245 --

 

Would you be able to paste the error message or screenshot of the error that you are receiving?  I tested on SQL 2019 and am not encountering any errors.  Take care.

I'm running it on Access and this is the query;
Select [table1].location
From (
Select [table1].vendor,
[table1].location,
rownum( ) over (partition by [table1].location order by [table1].vendor) as rownum
From [table1].vendor) [table1]
Where rownum = 1 and [table1].vendor = "HP"

Hi @Alex_Kim245 --

 

That query will only work on SQL Server and is incompatible with Access.  Unfortunately I do not know the Access-equivalent syntax and do not have Access available on which to test.  Take care.

I see. Thanks for your help!

@Alex_Kim245 try asking your question in the Access Community