Reply to topic  [ 5 posts ] 
SQL Query Query 
Author Message
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
I have a table which contains two columns, one called Dealers, one called Brands. Any entry in the table represents a dealer stocking a given brand.

What I'm trying to get is a SQL query that allows be to list each dealer that stocks all of a given selection of brands, rather than just returning each dealer that stocks at least one of the aforementioned brands. Anyone got any ideas?

_________________
"The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."


Thu Mar 22, 2012 10:10 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Group by the dealer, return where each item in the subset is there

as a c# linq statement (may need some tweaking)

string[] brands = new string[]{"foo","bar"};
var foo = from t in table
group t by t.Dealer into tDeal
where(t.select(p=>p.Brands).Intersect(brands));

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Thu Mar 22, 2012 10:59 am
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Am I being thick here or isn't the query just select dealer where brand = abc and def and etc?

_________________
I've finally invented something that works!

A Mac User.


Thu Mar 22, 2012 1:03 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
tombolt wrote:
Am I being thick here or isn't the query just select dealer where brand = abc and def and etc?

it is, but as (possibly) a more programmatic approach

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Thu Mar 22, 2012 1:35 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 6:44 pm
Posts: 4141
Location: Exeter
Reply with quote
tombolt wrote:
Am I being thick here or isn't the query just select dealer where brand = abc and def and etc?


Apparently not, having just tried it. Will have a look at the Group By function later.

Edit: Or maybe I was just being an idiot; it's now working.

Sorry for the dumb questions, it's been a while since I used any SQL.

_________________
"The woman is a riddle inside a mystery wrapped in an enigma I've had sex with."


Thu Mar 22, 2012 2:23 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.