|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2009-09-01 : 10:57:10
|
| declare @test table(month int, serialno int, statusID int)insert into @test select 1, 2220,1 union allSELECT 2, 2220,1 UNION ALLSELECT 3, 2220,3 UNION ALLSELECT 4, 2220,1 UNION ALL SELECT 5, 2220,1 UNION ALLSELECT 6, 2220,1 UNION ALLSELECT 7, 2220,1 UNION ALLSELECT 8, 2220,1 UNION ALLSELECT 1, 4440,2 UNION ALLSELECT 2, 4440,3 UNION ALLSELECT 3, 4440,1 UNION ALLSELECT 4, 4440,1 UNION ALLSELECT 5, 4440,1 UNION ALLSELECT 6, 4440,2 UNION ALLSELECT 7, 4440,2 UNION ALLSELECT 1, 9990,1 UNION ALLSELECT 2, 9990,1 UNION ALLSELECT 3, 9990,2 UNION ALLSELECT 4, 9990,3 UNION ALLSELECT 5, 9990,2 UNION ALLSELECT 6, 9990,2 select * from @testData should get split in 2 tables.As soon as a value of 3 is found for a given month the reminaing rows after that month should be removed from @testand put into another table or data be split into 2 tables. for each serialno there is only 1 value of 3 always.So we need to split the above table in 2 tables which should look like as following tables.this can be done using SQL 2005, 2008.declare @test1 table(month int, serialno int, statusID int)insert into @test1 select 1, 2220,1 union allSELECT 2, 2220,1 UNION ALLSELECT 3, 2220,3 UNION ALLSELECT 1, 4440,2 UNION ALLSELECT 2, 4440,3 UNION ALLSELECT 1, 9990,1 UNION ALLSELECT 2, 9990,1 UNION ALLSELECT 3, 9990,2 UNION ALLSELECT 4, 9990,3 select * from @test1declare @test2 table(month int, serialno int, statusID int)insert into @test2 SELECT 4, 2220,1 UNION ALL SELECT 5, 2220,1 UNION ALLSELECT 6, 2220,1 UNION ALLSELECT 7, 2220,1 UNION ALLSELECT 8, 2220,1 UNION ALLSELECT 3, 4440,1 UNION ALLSELECT 4, 4440,1 UNION ALLSELECT 5, 4440,1 UNION ALLSELECT 6, 4440,2 UNION ALLSELECT 7, 4440,2 UNION ALLSELECT 1, 9990,1 UNION ALLSELECT 5, 9990,2 UNION ALLSELECT 6, 9990,2 select * from @test2-----------------------------------------------------------------------------------------------Ashley Rhodes |
|