0

I have a table in which there are two rows From_ID and To_ID. I need rows grouped on based of having same Work_ID and the grouping should start from From_ID and should take its To_ID and look for the row having From_ID as To_ID of that row. These cannot be in sequence. And the group should end only when there is no row having From_ID as To_ID. I think table definition and screenshot will clear my query.

CREATE TABLE [dbo].[tblttt](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Work_ID] [nvarchar](max) NULL,
[From_ID] [int] NULL,
[To_ID] [int] NULL,
[Share] [float] NULL,
CONSTRAINT [PK_tblttt] PRIMARY KEY CLUSTERED 
(
   [ID] ASC
))
GO
SET IDENTITY_INSERT [dbo].[tblttt] ON 
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (1, N'051111Clus', 30500004, 30500005, 0.025)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (2, N'051111Clus', 30500004, 30500007, 0.025)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (3, N'051111Clus', 30500004, 30500009, 0.25)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (4, N'051111Clus', 30500004, 30500027, 0.025)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (5, N'051111Clus', 30500004, 30500029, 0.45)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (6, N'051111Clus', 30500005, 30500006, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (7, N'051111Clus', 30500007, 30500008, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (8, N'051111Clus', 30500009, 30500010, 0.6)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (9, N'051111Clus', 30500009, 30500024, 0.4)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (10, N'051111Clus', 30500010, 30500011, 0.33333333333333331)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (11, N'051111Clus', 30500010, 30500017, 0.666666666666667)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (12, N'051111Clus', 30500011, 30500012, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (13, N'051111Clus', 30500012, 30601745, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (14, N'051111Clus', 30500013, 30500014, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (15, N'051111Clus', 30500015, 30500016, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (16, N'051111Clus', 30500017, 30601746, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (17, N'051111Clus', 30500018, 30500019, 0.66666666666666663)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (18, N'051111Clus', 30500021, 30500022, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (19, N'051111Clus', 30500022, 30500023, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (20, N'051111Clus', 30500024, 30500025, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (21, N'051111Clus', 30500025, 30500026, 0.5)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (22, N'051111Clus', 30500027, 30601747, 1)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (23, N'051111Clus', 30500029, 30500030, 0.5)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (24, N'051111Clus', 30601745, 30500013, 0.5)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (25, N'051111Clus', 30601745, 30500015, 0.5)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (26, N'051111Clus', 30601746, 30500018, 0.75)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (27, N'051111Clus', 30601746, 30500021, 0.25)
GO
INSERT [dbo].[tblttt] ([ID], [Work_ID], [From_ID], [To_ID], [Share]) VALUES (28, N'051111Clus', 30601747, 30500028, 1)
GO
SET IDENTITY_INSERT [dbo].[tblttt] OFF
GO

The output is this:

Table

As clear from the screenshot, first group would be made having From_ID as 30500004 and To_ID as 30500005. The value of share should be 1 as this first row of this group. Next entry in this group would be row having From_ID as 30500005 and To_ID as 30500006 and share same as 1 of that row. Now, as there is no row with From_ID as 30500006, this group would end only and new group would again start having From_ID as 30500004 and To_ID as 30500007.

I tried following but its nowhere my requirement:

SELECT
TR.*
FROM tbltttT
LEFT JOIN tblttt TR
ON T.From_ID = TR.To_ID
WHERE T.work_id = '051111Clus' AND TR.work_id = '051111Clus'

So how can I achieve this?

Hemant Sisodia
  • 488
  • 6
  • 23
  • Can you show your expected output? – SqlZim Mar 09 '17 at 14:12
  • Try reading up on recursive CTE at https://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx - is this the sort of thing you're after? – Skippy Mar 09 '17 at 22:45
  • @SqlZim I want the output having new column as grp in the screenshot: [link](http://imgur.com/KaFxpeM). As visible from the screenshot, I am putting the rows having same To_ID and From_ID with same Group and stop it when no row is found with same From_ID as To_ID of any prev row. Then I increase the group number. – Hemant Sisodia Mar 15 '17 at 07:15
  • @SqlZim Is it possible to get output like this in the [screenshot ](http://imgur.com/V2lol8U). Here I have changed the output table to have only a State_ID as To_ID alongwith its Share and the required BranchNumber. – Hemant Sisodia Apr 05 '17 at 08:17

1 Answers1

0

I solved this by Looping instead of Recursive queries, starting from all the leaf nodes, then to their parent node till top node.

Hemant Sisodia
  • 488
  • 6
  • 23