Solved

How Do I Show Both Parents of The Same Child

Posted on 2011-03-08
3
223 Views
Last Modified: 2012-05-11
I have two tables, one with the names of adults and the other with the name of children. In the Children's table there are two columns, ParentID (Not Nullable) and OtherParentID (Nullable). I would like to query the tables so that when either of the parents IDs are presented as parameters, the other can show as the spouse or partner along with the related children from the Players table.

Is this possible with the tables I have? Or do I need to take a different approach?

USE [DBName]
GO

/****** Object:  Table [dbo].[ParentGuardian]    Script Date: 03/09/2011 00:00:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ParentGuardian](
	[ParentID] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [nvarchar](20) NOT NULL,
	[LastName] [nvarchar](20) NOT NULL,
	[Gender] [nvarchar](6) NOT NULL,
	[Birthdate] [date] NOT NULL,
	[Ethnicity] [nvarchar](50) NOT NULL,
	[Phone1] [nvarchar](14) NOT NULL,
	[Phone2] [nvarchar](14) NULL,
	[Phone3] [nvarchar](14) NULL,
	[Address1] [nvarchar](50) NOT NULL,
	[Address2] [nvarchar](50) NULL,
	[City] [nvarchar](50) NOT NULL,
	[State] [nvarchar](2) NULL,
	[Zip] [nvarchar](11) NULL,
	[Email] [nvarchar](175) NULL,
	[Education] [nvarchar](50) NULL,
	[Income] [nvarchar](50) NULL,
 CONSTRAINT [PK_ParentGuardian] PRIMARY KEY CLUSTERED 
(
	[ParentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ParentGuardian] ADD  CONSTRAINT [DF_ParentGuardian_State]  DEFAULT (N'TX') FOR [State]
GO



USE [DBName]
GO

/****** Object:  Table [dbo].[Players]    Script Date: 03/09/2011 00:03:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Players](
	[PlayerID] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [nvarchar](20) NOT NULL,
	[LastName] [nvarchar](20) NOT NULL,
	[Gender] [nvarchar](6) NOT NULL,
	[Birthdate] [date] NOT NULL,
	[Ethnicity] [nvarchar](20) NOT NULL,
	[ParentID] [int] NOT NULL,
	[OtherParentID] [int] NULL,
 CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED 
(
	[PlayerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_ParentGuardian] FOREIGN KEY([ParentID])
REFERENCES [dbo].[ParentGuardian] ([ParentID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_ParentGuardian]
GO

Open in new window

0
Comment
Question by:wdarnellg
  • 2
3 Comments
 

Author Comment

by:wdarnellg
ID: 35079634
I have an asp.net vb project in vs 2010 where I am trying show this data in 2 gridview controls. One has a list of Adult names and the other uses the ParentID to display the names of the Junior Players along with the Other Parent. I can get them to work on ONE parent, but not the OtherParentID. I need the Junior Players to show no matter which parent name is selected along with the ParentID or OtherParentID aliased as "Spouse or Partner".
0
 
LVL 9

Accepted Solution

by:
kaminda earned 500 total points
ID: 35079673
Hi,
 
This should give you the desired results.


select p.Firstname as Child, pa.FirstName as Parent, ps.FirstName as Spouse
FROM 
Players p 
INNER JOIN ParentGuardian pa 
ON p.parentid = pa.parentid 
LEFT OUTER JOIN ParentGuardian ps 
ON p.OtherParentID = ps.parentid

Open in new window

0
 

Author Closing Comment

by:wdarnellg
ID: 35080295
Cool! That does work, even when I add the WHERE and OR for the parameters
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 66
Query Optimization 14 43
SQL: launch actions one before the other 10 23
Linked Server Issue with SQL2012 3 25
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question