Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2010-08-11 : 03:53:51
|
HiI have a table @tbl1 that has years and gradesYear Grade2007 Grade12007 Grade 22008 Grade 12008 Grade 22008 Grade 32009 Grade 12009 Grade 22009 Grade 4Now my problem is i need to know the distinct grades that were missing in previous year and introduced in new years.Like in 2007 grade 3 and grade 4 was not introduced.My resultant query result would look like2008 Grade 32009 Grade 4And then i need to insert the missing grades in previous years like in 2007 grade 3 and grade 4 would be inserted and in 2008 grade 4 would be inserted.Any help would appreciated.ThanksMuzaffar Ali |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-11 : 05:08:23
|
What happens if year 2009 has grade 5?declare @tbl as table(yr int,grade varchar(30))insert into @tblselect 2007,'Grade 1' union allselect 2007,'Grade 2'union allselect 2008 ,'Grade 1' union allselect 2008 ,'Grade 2' union allselect 2008 ,'Grade 3' union allselect 2009 ,'Grade 1'union allselect 2009 ,'Grade 2'union allselect 2009 ,'Grade 4'--insert into @tblselect * from @tbl;with cteas(select t1yr ,t1grade from(select t1.yr as t1yr,t2.yr as t2yr,t1.grade as t1grade,t2.grade as t2grade from @tbl t1left join @tbl t2 on t1.yr=t2.yr+1 and t1.grade=t2.grade)t where t1yr not in(select top 1 yr from @tbl order by yr)and t2yr is null)insert into @tblselect distinct yr,t1grade from @tbl cross apply(select top 100 percent * from cte where t1grade<>grade and t1yr>=yr+1 order by yr)t select * from @tbl order by yr,grade Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2010-08-11 : 06:30:49
|
Thanks for the quick reply.If 2009 has grade 5 that will be added in 2008 and 2007 too and any other year if in data.also i found some problems in the solution you provided if you can fix please.declare @tbl as table(yr int,grade varchar(30))insert into @tblselect 2007,'Grade 1' union allselect 2007,'Grade 2'union allselect 2007 ,'Grade 5' union allselect 2008 ,'Grade 1' union allselect 2008 ,'Grade 2' union allselect 2008 ,'Grade 3' union allselect 2009 ,'Grade 1'union allselect 2009 ,'Grade 2'union allselect 2009 ,'Grade 4' union allselect 2009 ,'Grade 5'--insert into @tbl--Select * from @tbl--select * from @tbl;with cteas(select t1yr ,t1grade from(select t1.yr as t1yr,t2.yr as t2yr,t1.grade as t1grade,t2.grade as t2grade from @tbl t1 left join @tbl t2 on t1.yr=t2.yr+1 and t1.grade=t2.grade)t where t1yr not in(select top 1 yr from @tbl order by yr)and t2yr is null)insert into @tblselect distinct yr,t1grade from @tbl cross apply(select top 100 percent * from cte where t1grade<>grade and t1yr>=yr+1 order by yr)t select * from @tbl order by yr,gradeTry running this script it has Grade 5 added 2 times in 2007 that should not be happening.In simple words any year that has missing grade of other years will be added to that year.ThanksMuzaffar Ali |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-11 : 06:36:08
|
quote: Originally posted by muzaffar_ali99 In simple words any year that has missing grade of other years will be added to that year.ThanksMuzaffar Ali
Then why Grade 3 should not get added for year 2009 with the underlying sample data??? declare @tbl as table(yr int,grade varchar(30))insert into @tblselect 2007,'Grade 1' union allselect 2007,'Grade 2'union allselect 2008 ,'Grade 1' union allselect 2008 ,'Grade 2' union allselect 2008 ,'Grade 3' union allselect 2009 ,'Grade 1'union allselect 2009 ,'Grade 2'union allselect 2009 ,'Grade 4'select * from @tbl Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2010-08-11 : 07:17:49
|
It should get added in the end result if it does not exist in 2009 if exists then ok.Thanks |
 |
|
PavanKK
Starting Member
32 Posts |
Posted - 2010-08-11 : 07:51:21
|
try this---------DECLARE @Tbl TABLE (Year INT,Grade VARCHAR(10))INSERT INTO @Tblselect 2007,'Grade 1' union allselect 2007,'Grade 2'union allselect 2007 ,'Grade 5' union allselect 2008 ,'Grade 1' union allselect 2008 ,'Grade 2' union allselect 2008 ,'Grade 3' union allselect 2009 ,'Grade 1'union allselect 2009 ,'Grade 2'union allselect 2009 ,'Grade 4' union allselect 2009 ,'Grade 5'DECLARE @Min INT,@Max INTSELECT @Min = MIN(Year),@Max = MAX(Year) FROM @Tbl;WITH cte AS( SELECT DISTINCT @Min Year,Grade FROM @Tbl UNION ALL SELECT Year+1,Grade FROM cte WHERE Year+1<=@Max)SELECT C.Year,C.GradeFROM cte CLEFT JOIN @Tbl T ON C.Year = T.Year AND C.Grade = T.GradeWHERE T.Year IS NULL----------------KK |
 |
|
muzaffar_ali99
Starting Member
33 Posts |
Posted - 2010-08-11 : 08:58:21
|
Thank you so much that really helped solve my problem |
 |
|
|
|
|
|
|