Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Problem
Message
 
To
04/02/2011 14:35:30
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01498808
Message ID:
01498819
Views:
44
>>>Hi All:
>>>
>>>Consider the following table:
>>>
>>>
>>>Artnum    Catnum
>>>111         INT
>>>111         ISR
>>>111         GRP
>>>222         INT
>>>222         RSA
>>>222         GRP
>>>
>>>
>>>I want a select statement that says 'give me all the records for a particular artnum WHERE the artnum belongs to a catnum of BOTH 'INT' and 'ISR'.
>>>
>>>This would produce the following list:
>>>
>>>
>>>111         INT
>>>111         ISR
>>>111         GRP
>>>
>>>
>>>Thanks,
>>>
>>>Yossi
>>
>>This is the problem that is called Relational Divison Problem. Peter Larsson has a very interesting blog about it.
>>
>>http://sqlblog.com/blogs/peter_larsson/archive/2010/06/30/relational-algebra.aspx
>>
>>one of the possible solutions:
>>
>>select ArtNum from YourTable
>>where CartNum IN ('INT','ISR')
>>group by ArtNum
>>having MIN(CatNum) = 'INT' and MAX(CatNum) = 'ISR'
>
>This produces only one record. What do you think of Sergey's 2 solutions?


:-)
SELECT YourTable.*
FROM YourTable
INNER JOIN (select ArtNum 
                   from YourTable
            where CartNum IN ('INT','ISR')
            group by ArtNum
            having MIN(CatNum) = 'INT' and MAX(CatNum) = 'ISR') Tbl1
      ON YourTable.ArtNum = Tbl1.ArtNum 
> What do you think of Sergey's 2 solutions?

They are great (as always)
But Naomi here gives you just another way. Sometimes you must know all possible ways so you can choose the best.
Turn Actual Execution Plan ON and run all queries, then analyse the result :-)
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform