--Create temp table to hold the dummy dataif object_id('tempdb..#IDs') is not nulldrop table #IDscreate table #IDs (InvoiceId int not null,BookingId int not null) on [PRIMARY]goalter table #IDs add constraint PK_IDs primary key clustered ( InvoiceId ,BookingId ) with ( STATISTICS_NORECOMPUTE = off ,IGNORE_DUP_KEY = off ,ALLOW_ROW_LOCKS = on ,ALLOW_PAGE_LOCKS = on ) on [PRIMARY]goinsert #IDs ( InvoiceId, BookingId) select * from (values (1,9), (1,10), (1,11), (2,11), (3,11), (3,12), (3,13), (4,14), (5,14)) data(InvoiceId,BookingId)select * from #IDs
SELECT *FROM (VALUES(1,9),(1,10),(1,11),(2,11),(3,11),(3,12),(3,13)) x([InvoiceId],[BookingId])
--Create temp table to hold the dummy dataif object_id('tempdb..#IDs') is not null drop table #IDsCREATE TABLE #IDs ( InvoiceId int not null, BookingId int not null, primary key clustered (InvoiceId, BookingId));INSERT #IDs VALUES (1,9), (1,10), (1,11), (2,11), (3,11), (3,12), (3,13), (4,14), (5,14)GODECLARE @val int = 1;WITH x AS( SELECT i.InvoiceId, i.BookingId from ( SELECT InvoiceId, BookingId from #IDs WHERE InvoiceId = @val ) a JOIN #IDs i ON a.BookingId = i.BookingId)SELECT DISTINCT i.* FROM xRIGHT JOIN #IDs i ON x.InvoiceId = i.InvoiceIdWHERE x.InvoiceId IS NOT NULL
CREATE TABLE #IDsParents (InvoiceId int not null,BookingId int not null,ParentInvoiceID INT)insert #IDsParents ( InvoiceId, BookingId) select * from (values (1,9), (1,10), (1,11), (2,11), (3,11), (3,12), (3,13), (4,14), (5,14)) data(InvoiceId,BookingId);WITH Parent AS(SELECT a.InvoiceId, b.InvoiceId AS Parent FROM #IDsParents a LEFT JOIN #IDsParents b ON a.BookingId = b.BookingId AND a.InvoiceId > b.InvoiceIdWHERE b.InvoiceId IS NOT NULL)UPDATE b SET ParentInvoiceID = a.Parent FROM Parent a RIGHT JOIN #IDsParents b ON a.InvoiceId = b.InvoiceIdSELECT * FROM #IDsParents
--am trying to write a query against a table of invoices, returning all relevant rows for a particular InvoiceId. Heres some sample data:--Create temp table to hold the dummy dataif object_id('tempdb..#IDs') is not nulldrop table #IDscreate table #IDs (InvoiceId int not null,BookingId int not null) on [PRIMARY]goalter table #IDs add constraint PK_IDs primary key clustered ( InvoiceId ,BookingId ) with ( STATISTICS_NORECOMPUTE = off ,IGNORE_DUP_KEY = off ,ALLOW_ROW_LOCKS = on ,ALLOW_PAGE_LOCKS = on ) on [PRIMARY]goinsert #IDs ( InvoiceId, BookingId) select * from (values (1,9), (1,10), (1,11), (2,11), (3,11), (3,12), (3,13), (4,14), (5,14)) data(InvoiceId,BookingId)select * from #IDsDECLARE @InvoiceID INTSET @InvoiceID = 1;WITH rCTE AS(SELECT InvoiceID, BookingID, 1 AS HierarchyLevelFROM #IDsWHERE InvoiceID = @InvoiceIDUNION ALL SELECT #IDs.InvoiceID, ids2.BookingID, rCTE.HierarchyLevel + 1 AS HierarchyLevel FROM rCTE JOIN #IDs ON #IDs.BookingID = rCTE.BookingID JOIN #IDs AS ids2 ON #IDs.InvoiceID = ids2.InvoiceIDWHERE rCTE.HierarchyLevel + 1 )SELECT * FROM rCTE
--am trying to write a query against a table of invoices, returning all relevant rows for a particular InvoiceId. Heres some sample data:--Create temp table to hold the dummy dataif object_id('tempdb..#IDs') is not nulldrop table #IDsif object_id('tempdb..#TempStore') is not nulldrop table #TempStorecreate table #IDs (InvoiceId int not null,BookingId int not null) on [PRIMARY]goalter table #IDs add constraint PK_IDs primary key clustered ( InvoiceId ,BookingId ) with ( STATISTICS_NORECOMPUTE = off ,IGNORE_DUP_KEY = off ,ALLOW_ROW_LOCKS = on ,ALLOW_PAGE_LOCKS = on ) on [PRIMARY]goinsert #IDs ( InvoiceId, BookingId) select * from (values (1,9), (1,10), (1,11), (2,11), (3,11), (3,12), (3,13), (4,14), (5,14)) data(InvoiceId,BookingId)select * from #IDsDECLARE @InvoiceID INT, @Rowcount INTSELECT @InvoiceID = 1, @Rowcount = 1CREATE TABLE #TempStore (InvoiceID INT, BookingID INT)INSERT INTO #TempStoreSELECT InvoiceID, BookingIDFROM #IDsWHERE InvoiceID = @InvoiceID-- Set this here, might as well not hit the loop if no records to work from.SELECT @Rowcount = @@ROWCOUNTWHILE @Rowcount <> 0BEGIN INSERT INTO #TempStore SELECT ids2.InvoiceID, ids2.BookingID FROM #IDs JOIN (SELECT DISTINCT BookingID FROM #TempStore) AS drv ON #IDs.BookingID = drv.BookingID JOIN #IDs AS ids2 ON #IDs.InvoiceID = ids2.InvoiceID WHERE #IDs.InvoiceID NOT IN (SELECT DISTINCT InvoiceID FROM #TempStore) SET @Rowcount = @@ROWCOUNTENDSELECT * FROM #TempStore
INSERT #IDs VALUES (1,9), (1,10), (1,11), (2,11), (2,12), (3,12), (3,13), (4,14), (5,14)GO