Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL syntax help with a subquery
Message
De
30/11/2006 19:22:09
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows NT
Database:
MS SQL Server
Divers
Thread ID:
01173944
Message ID:
01173947
Vues:
13
>Hello all,
>
>I'm hoping to get some assistance on a specific SQL query. Here's a simplified version of the two tables it is working off:
>
>Table A (parent)
>a_pk (pk)
>
>Table B (child)
>b_pk (pk)
>b_afk (fk to parent)
>b_area (area)
>b_name (non-unique name)
>
>Problem: I am trying to write a query that returns the name (b_name) of the largest child (b_area) for all parent records.
>
>Any help is most appreciated!

If I understand correctly, you want the largest in each group. I think it would be something like this:
select b_pk, max(b_area) from ChildTable;
  group by b_pk;
  into cursor Tmp
select Tmp.*, b.b_name;
  from Tmp join ChildTable b on Tmp.b_pk = b.b_pk and Tmp.b_area = b.b_area;
  into cursor Tmp
I am not sure whether this can be done with a single query (with subquery perhaps); I have always done the subqueries joining on a single field, and I don't know if it can be done with more than one field.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform