选择带有幼儿

时间:2018-05-23 05:16:19

标签: sql sql-server

我有下表

CREATE TABLE [dbo].[people](
    [id] [int] NOT NULL,
    [motherId] [int] NULL,
    [fatherId] [int] NULL,
    [name] [varchar](30) NOT NULL,
    [age] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [id] 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].[people]  WITH CHECK ADD FOREIGN KEY([fatherId])
REFERENCES [dbo].[people] ([id])
GO

ALTER TABLE [dbo].[people]  WITH CHECK ADD FOREIGN KEY([motherId])
REFERENCES [dbo].[people] ([id])
GO

并插入以下详细信息

INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (1, NULL, NULL, N'Adam', 50)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (2, NULL, NULL, N'Eve', 50)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (3, 2, 1, N'Cain', 30)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (4, 2, 1, N'Seth', 20)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (5, NULL, NULL, N'Saman', 48)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (6, NULL, NULL, N'Thulsi', 45)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (7, 6, 5, N'Jehan', 23)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (8, 6, 5, N'Nithin', 22)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (9, 3, 8, N'Chuti Baba', 7)
GO
INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (10, 4, 7, N'Loku baba', 8)

我很难写下面的查询

选择所有父母的姓名及其最小孩的年龄

到目前为止我尝试过跟踪,但似乎这曾经是一个递归查询,我不知道如何做到这一点

SELECT DISTINCT pp.motherId,pp.fatherId, pp.id
FROM people AS pp
WHERE pp.motherId IS NOT NULL AND pp.fatherId IS NOT NULL

SELECT FR.motherId,FR.fatherId
from (SELECT motherId, fatherId, age
FROM people AS pp
WHERE pp.motherId IS NOT NULL AND pp.fatherId IS NOT NULL
Group By motherId, fatherId, age) AS FR

7 个答案:

答案 0 :(得分:2)

您可以尝试如下查询 See live demo

select 
    MotherName= m.name,
    FatherName= f.name,
    YoungestChildAge=min(y.age)
from
people y join people m
    on y.motherid=m.id
join people f
    on y.fatherid=f.id
where coalesce(y.motherid,y.fatherid) is not null
group by
    m.name,f.name

答案 1 :(得分:1)

此查询将有所帮助。

Select motherid, fatherid, min(age)  as [youngest child age] from [people]
where motherid is not null and fatherid is not null
group by motherid, fatherid

答案 2 :(得分:0)

您需要为此编写子查询。内部查询选择子项的母亲和父母,然后外部查询获取父母的姓名。

select name from people
where id in 
(
  select  parentid from
  (Select motherid as parentid,  min(age)  as [youngest child age] from [people]
  where motherid is not null and fatherid is not null
  group by motherid, fatherid
  union
  Select fatherid,  min(age)  as [youngest child age] from [people]
  where motherid is not null and fatherid is not null
  group by motherid, fatherid) as x
)

这是SQLFiddle

答案 3 :(得分:0)

另一种解决方案:

select distinct motherid, fatherid, 
first_value(age) over (partition by motherid,fatherid order by age) as youngest
from people where motherid is not null and fatherid is not null

答案 4 :(得分:0)

    ;with cte 
    AS
    (
        Select [motherId],[fatherId], min([age])  as [youngest child age] 
        from [dbo].[people]
        group by fatherid,motherId
    )

    Select pm.Name AS MotherName, pf.Name As FatherName,t.[youngest child age] 
    from cte t 
    JOIN [dbo].[people] pf on t.fatherId=pf.id
    JOIN [dbo].[people] pm on t.motherId=pm.id

答案 5 :(得分:0)

此查询将为您提供帮助。

  SELECT M.NAME AS MOTHER , F.NAME AS FATHER ,P1.AGE AS [YOUNGEST CHILD AGE] 

  FROM #PEOPLE M

 INNER JOIN 
 (
  SELECT MOTHERID ,FATHERID,MIN(AGE)AGE FROM #PEOPLE WHERE MOTHERID IS NOT NULL AND FATHERID IS NOT NULL
  GROUP BY MOTHERID ,FATHERID 
 ) 
  P1 ON M.ID =P1.MOTHERID  
  INNER JOIN #PEOPLE F ON F.ID=P1.FATHERID 

答案 6 :(得分:0)

可能的解决方案:

SELECT p.name as name, MIN(pp.age) as age
FROM people as p
LEFT JOIN people as pp ON (p.id = pp.motherId OR p.id = pp.fatherId)
WHERE p.motherId IS NULL AND p.fatherId IS NULL
GROUP BY p.id
ORDER BY pp.age