Guest User

Untitled

a guest
May 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. CREATE TABLE [dbo].[people](
  2. [id] [int] NOT NULL,
  3. [motherId] [int] NULL,
  4. [fatherId] [int] NULL,
  5. [name] [varchar](30) NOT NULL,
  6. [age] [int] NOT NULL,
  7. PRIMARY KEY CLUSTERED
  8. (
  9. [id] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12. GO
  13.  
  14. ALTER TABLE [dbo].[people] WITH CHECK ADD FOREIGN KEY([fatherId])
  15. REFERENCES [dbo].[people] ([id])
  16. GO
  17.  
  18. ALTER TABLE [dbo].[people] WITH CHECK ADD FOREIGN KEY([motherId])
  19. REFERENCES [dbo].[people] ([id])
  20. GO
  21.  
  22. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (1, NULL, NULL, N'Adam', 50)
  23. GO
  24. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (2, NULL, NULL, N'Eve', 50)
  25. GO
  26. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (3, 2, 1, N'Cain', 30)
  27. GO
  28. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (4, 2, 1, N'Seth', 20)
  29. GO
  30. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (5, NULL, NULL, N'Saman', 48)
  31. GO
  32. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (6, NULL, NULL, N'Thulsi', 45)
  33. GO
  34. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (7, 6, 5, N'Jehan', 23)
  35. GO
  36. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (8, 6, 5, N'Nithin', 22)
  37. GO
  38. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (9, 3, 8, N'Chuti Baba', 7)
  39. GO
  40. INSERT [dbo].[people] ([id], [motherId], [fatherId], [name], [age]) VALUES (10, 4, 7, N'Loku baba', 8)
  41.  
  42. SELECT DISTINCT pp.motherId,pp.fatherId, pp.id
  43. FROM people AS pp
  44. WHERE pp.motherId IS NOT NULL AND pp.fatherId IS NOT NULL
  45.  
  46. SELECT FR.motherId,FR.fatherId
  47. from (SELECT motherId, fatherId, age
  48. FROM people AS pp
  49. WHERE pp.motherId IS NOT NULL AND pp.fatherId IS NOT NULL
  50. Group By motherId, fatherId, age) AS FR
Add Comment
Please, Sign In to add comment