Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL: Distinct, Group By, or Exists?
Message
 
 
To
04/06/2003 17:44:38
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00796420
Message ID:
00796429
Views:
11
This message has been marked as a message which has helped to the initial question of the thread.
I would do it in one of two ways (in the order of my personal preferences)
SELECT PrimaryKey, DataItem ;
  FROM TableA ;
  WHERE PrimaryKey IN ( SELECT PrimaryKey FROM TableB WHERE Condition = .T.)
* 
SELECT PrimaryKey, DataItem ;
  FROM TableA ta ;
  WHERE EXISTS ( SELECT * FROM TableB tb ;
        WHERE ta.PrimaryKey = tb.PrimaryKey AND tb.Condition = .T.)
You may want to take a look at recent discussion which one is faster. Re: How to use select... where EXISTS ... ? Thread #793069.

>Not having strong experience with SQL, I need a little help to get the right result set for a new view.
>TableA: PrimaryKey(I), DataItem(C 20)
>  1, Apples
>  2, Oranges
>  3, Pears
>  4, Bananas
>TableB: ForeignKey(I), Condition(L)
>  1, .T.
>  1, .T.
>  2, .F.
>  4, .T.
>  4, .T.
>  4, .F.
>
>Result: PrimaryKey(I), DataItem(C 20)
>  1, Apples
>  4, Bananas
>(One result from Table A only if there's a True Condition in table B.)
>Normally, I'd try to join the tables and use DISTINCT PrimaryKey, or GROUP BY PrimaryKey. Shouldn't those give the same result? Which is better? Does VFP8's change to GROUP BY syntax change this? Would an EXISTS clause (which I've never used before) work?
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform