Symple query

  • Thread starter Thread starter sergiogroups
  • Start date Start date
S

sergiogroups

If It have a table T_Contacts:

Name Gender FamilyID
John M 1
Paul M 2
Anna F 1
Kate F 2

And want to create a table T_Couples:

Husband Wife
John Anna
Paul Kate

which is the query to do that?
 
TRy this --

TRANSFORM First(T_Contacts.Name) AS FirstOfName
SELECT T_Contacts.FamilyID
FROM T_Contacts
GROUP BY T_Contacts.FamilyID
PIVOT IIf([Gender]="M","Husband","Wife");
 
If It have a table T_Contacts:

Name Gender FamilyID
John M 1
Paul M 2
Anna F 1
Kate F 2

And want to create a table T_Couples:

Husband Wife
John Anna
Paul Kate

which is the query to do that?

There are several; Karl's elegant Crosstab is one, another uses a Self
Join:

SELECT [T_Contacts_M].[Name] AS Husband, [T_Contacts_F].[Name] AS Wife
FROM [T_Contacts] AS [T_Contacts_M]
INNER JOIN [T_Contacts] AS [T_Contacts_F]
ON [T_Contacts_M].[FamilyID] = [T_Contacts_F].[FamilyID]
WHERE [T_Contacts_M].[Gender] = "M"
AND [T_Contacts_F].[Gender] = "F"
ORDER BY Wife, Husband;

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top