Hello,

I have a table storing bills of materials. I want to select all the products
that contain certain items and don't contain certain other items.

What is the best way? I tentatively guessed at using a HAVING clause but I
was wrong. I think this leaves me with a nasty set of self joins or a less
nasty set of sequential queries:

CREATE CURSOR bom (item C(10), comp C(10))
INSERT INTO bom VALUES ("item1", "comp1")
INSERT INTO bom VALUES ("item1", "comp2")
INSERT INTO bom VALUES ("item1", "comp3")
INSERT INTO bom VALUES ("item1", "comp4")
INSERT INTO bom VALUES ("item1", "comp5")

INSERT INTO bom VALUES ("item2", "comp1")
INSERT INTO bom VALUES ("item2", "comp3")
INSERT INTO bom VALUES ("item2", "comp4")
INSERT INTO bom VALUES ("item2", "comp5")

INSERT INTO bom VALUES ("item3", "comp1")
INSERT INTO bom VALUES ("item3", "comp7")
INSERT INTO bom VALUES ("item3", "comp8")
INSERT INTO bom VALUES ("item3", "comp9")
INSERT INTO bom VALUES ("item3", "comp10")
INSERT INTO bom VALUES ("item3", "comp11")
INSERT INTO bom VALUES ("item3", "comp15")

I want all the items that contain, say,
a ("comp1" or a "comp3")
not a ("comp6" or a "comp7")
and a "comp2"

Any ideas? [it should just be "item1" in the above data]

* this is the kind of thing I'd hoped would work but wasn't too surprised
when it didn't:
SELECT item FROM bom ;
HAVING (comp="comp1" OR comp="comp3") ;
AND !(comp="comp6" OR comp="comp7") ;
AND comp="comp2" ;
GROUP BY item

In my actual data I have to test many more components present / not present
than that example.
I'm thinking a series of EXISTS and NOT EXISTS queries is going to be
easiest at the moment.

--
TIA
Andrew Howell

Re: SQL, which items have certain refs and don't have certain other refs? by Stefan

Stefan
Thu Nov 25 05:53:36 CST 2004

Hi Andrew,

Does this work for you? (ahm, in Fox 2.x, right? :-)

SELECT item FROM bom ;
WHERE item in ( ;
SELECT item FROM bom WHERE comp in("comp1","comp3") ) ;
AND item NOT in ( ;
SELECT item FROM bom WHERE comp in ("comp6","comp7") ) ;
AND item in ( ;
SELECT item FROM bom WHERE comp = "comp2" ) ;
group by 1

hth
-Stefan

"Andrew Howell" <ajh@work> schrieb im Newsbeitrag
news:ellh1zt0EHA.1564@TK2MSFTNGP09.phx.gbl...
> Hello,
>
> I have a table storing bills of materials. I want to select all the products
> that contain certain items and don't contain certain other items.
>
> What is the best way? I tentatively guessed at using a HAVING clause but I
> was wrong. I think this leaves me with a nasty set of self joins or a less
> nasty set of sequential queries:
>
> CREATE CURSOR bom (item C(10), comp C(10))
> INSERT INTO bom VALUES ("item1", "comp1")
> INSERT INTO bom VALUES ("item1", "comp2")
> INSERT INTO bom VALUES ("item1", "comp3")
> INSERT INTO bom VALUES ("item1", "comp4")
> INSERT INTO bom VALUES ("item1", "comp5")
>
> INSERT INTO bom VALUES ("item2", "comp1")
> INSERT INTO bom VALUES ("item2", "comp3")
> INSERT INTO bom VALUES ("item2", "comp4")
> INSERT INTO bom VALUES ("item2", "comp5")
>
> INSERT INTO bom VALUES ("item3", "comp1")
> INSERT INTO bom VALUES ("item3", "comp7")
> INSERT INTO bom VALUES ("item3", "comp8")
> INSERT INTO bom VALUES ("item3", "comp9")
> INSERT INTO bom VALUES ("item3", "comp10")
> INSERT INTO bom VALUES ("item3", "comp11")
> INSERT INTO bom VALUES ("item3", "comp15")
>
> I want all the items that contain, say,
> a ("comp1" or a "comp3")
> not a ("comp6" or a "comp7")
> and a "comp2"
>
> Any ideas? [it should just be "item1" in the above data]
>
> * this is the kind of thing I'd hoped would work but wasn't too surprised
> when it didn't:
> SELECT item FROM bom ;
> HAVING (comp="comp1" OR comp="comp3") ;
> AND !(comp="comp6" OR comp="comp7") ;
> AND comp="comp2" ;
> GROUP BY item
>
> In my actual data I have to test many more components present / not present
> than that example.
> I'm thinking a series of EXISTS and NOT EXISTS queries is going to be
> easiest at the moment.
>
> --
> TIA
> Andrew Howell
>
>


Re: SQL, which items have certain refs and don't have certain other refs? by Andrew

Andrew
Thu Nov 25 09:18:03 CST 2004

Stefan Wuebbe wrote:
> Hi Andrew,
>
> Does this work for you? (ahm, in Fox 2.x, right? :-)
>
> SELECT item FROM bom ;
> WHERE item in ( ;
> SELECT item FROM bom WHERE comp in("comp1","comp3") ) ;
> AND item NOT in ( ;
> SELECT item FROM bom WHERE comp in ("comp6","comp7") ) ;
> AND item in ( ;
> SELECT item FROM bom WHERE comp = "comp2" ) ;
> group by 1

Almost - "SQL - too many subqueries" - apparently it's limited to 2
subqueries per SQL.

Thanks though - in the end I did the subqueries into separate cursors and
then queried those. And sorry for forgetting the "FPW2.6" info which you
correctly deduced :-)

--
Regards
Andrew Howell



Re: SQL, which items have certain refs and don't have certain other refs? by Stefan

Stefan
Thu Nov 25 09:45:47 CST 2004


"Andrew Howell" <ajh@work> schrieb im Newsbeitrag
news:%23nI52Hw0EHA.1264@TK2MSFTNGP12.phx.gbl...
> Stefan Wuebbe wrote:
>> Hi Andrew,
>>
>> Does this work for you? (ahm, in Fox 2.x, right? :-)
>
> Almost - "SQL - too many subqueries" - apparently it's limited to 2
> subqueries per SQL.

Oh yes, I tried and saw the error meanwhile. Works in Vfp8/9 though.
While interestingly your previous Having example does it the other
way around and gives an error in Vfp8 but not in FP2x


Regards
-Stefan


Re: SQL, which items have certain refs and don't have certain other refs? by Andrew

Andrew
Thu Nov 25 09:57:09 CST 2004

Stefan Wuebbe wrote:
> Oh yes, I tried and saw the error meanwhile. Works in Vfp8/9 though.
> While interestingly your previous Having example does it the other
> way around and gives an error in Vfp8 but not in FP2x

I get no records though ;) [which is fair enough, I can see why]

--
Regards
Andrew Howell



Re: SQL, which items have certain refs and don't have certain other refs? by Anders

Anders
Thu Nov 25 12:03:31 CST 2004

Chapter 26, Trees, in Joe Celko's book SQL for Smarties, goes into this at
some length. There have been plenty of discussions in NG's that you can
google for.
-Anders

"Andrew Howell" <ajh@work> wrote in message
news:ellh1zt0EHA.1564@TK2MSFTNGP09.phx.gbl...
> Hello,
>
> I have a table storing bills of materials. I want to select all the
products
> that contain certain items and don't contain certain other items.
>
> What is the best way? I tentatively guessed at using a HAVING clause but I
> was wrong. I think this leaves me with a nasty set of self joins or a less
> nasty set of sequential queries:
>
> CREATE CURSOR bom (item C(10), comp C(10))
> INSERT INTO bom VALUES ("item1", "comp1")
> INSERT INTO bom VALUES ("item1", "comp2")
> INSERT INTO bom VALUES ("item1", "comp3")
> INSERT INTO bom VALUES ("item1", "comp4")
> INSERT INTO bom VALUES ("item1", "comp5")
>
> INSERT INTO bom VALUES ("item2", "comp1")
> INSERT INTO bom VALUES ("item2", "comp3")
> INSERT INTO bom VALUES ("item2", "comp4")
> INSERT INTO bom VALUES ("item2", "comp5")
>
> INSERT INTO bom VALUES ("item3", "comp1")
> INSERT INTO bom VALUES ("item3", "comp7")
> INSERT INTO bom VALUES ("item3", "comp8")
> INSERT INTO bom VALUES ("item3", "comp9")
> INSERT INTO bom VALUES ("item3", "comp10")
> INSERT INTO bom VALUES ("item3", "comp11")
> INSERT INTO bom VALUES ("item3", "comp15")
>
> I want all the items that contain, say,
> a ("comp1" or a "comp3")
> not a ("comp6" or a "comp7")
> and a "comp2"
>
> Any ideas? [it should just be "item1" in the above data]
>
> * this is the kind of thing I'd hoped would work but wasn't too surprised
> when it didn't:
> SELECT item FROM bom ;
> HAVING (comp="comp1" OR comp="comp3") ;
> AND !(comp="comp6" OR comp="comp7") ;
> AND comp="comp2" ;
> GROUP BY item
>
> In my actual data I have to test many more components present / not
present
> than that example.
> I'm thinking a series of EXISTS and NOT EXISTS queries is going to be
> easiest at the moment.
>
> --
> TIA
> Andrew Howell
>
>


Re: SQL, which items have certain refs and don't have certain other refs? by Andrew

Andrew
Mon Nov 29 07:22:29 CST 2004

Anders Altberg wrote:
> Chapter 26, Trees, in Joe Celko's book SQL for Smarties, goes into
> this at some length. There have been plenty of discussions in NG's
> that you can google for.
> -Anders

Thanks, I think I'll look this book up.

At least I don't deel so dim knowing it's in chapter 26 of SQL for Smarties
rather than chapter 2 of SQL for Dummies ;)

--
Regards
Andrew Howell



Re: SQL, which items have certain refs and don't have certain other refs? by Andrew

Andrew
Mon Nov 29 07:30:05 CST 2004

Andrew Howell wrote:
> Anders Altberg wrote:
>> Chapter 26, Trees, in Joe Celko's book SQL for Smarties, goes into
>> this at some length. There have been plenty of discussions in NG's
>> that you can google for.
>> -Anders
>
> Thanks, I think I'll look this book up.
>
> At least I don't deel so dim knowing it's in chapter 26 of SQL for
> Smarties rather than chapter 2 of SQL for Dummies ;)

Actually, I see he has since written an entire book on Trees & Hierarchies:
"Joe Celko's Trees and Hierarchies in SQL for Smarties (Data Management
Systems S.)"

--
regards
Andrew Howell



Re: SQL, which items have certain refs and don't have certain other refs? by Anders

Anders
Mon Nov 29 08:30:40 CST 2004

Hi
You'ed better chack that the Trees suff is still in the Smaties book. I
think there are new editions and books with the contents rearranged.
-Anders

"Andrew Howell" <ajh@work> wrote in message
news:#LUK9Zh1EHA.3000@TK2MSFTNGP15.phx.gbl...
> Anders Altberg wrote:
> > Chapter 26, Trees, in Joe Celko's book SQL for Smarties, goes into
> > this at some length. There have been plenty of discussions in NG's
> > that you can google for.
> > -Anders
>
> Thanks, I think I'll look this book up.
>
> At least I don't deel so dim knowing it's in chapter 26 of SQL for
Smarties
> rather than chapter 2 of SQL for Dummies ;)
>
> --
> Regards
> Andrew Howell
>
>