'DECLARE @t TABLE (SlNo INT IDENTITY (1, 1) NOT NULL, Date DATETIME, Session VARCHAR(30), TotalMembers INT, TotalLitres DECIMAL(8, 2), Amount DECIMAL(8, 2), GTFat DECIMAL(8, 2), GTPrice DECIMAL(8, 2), GTAmount DECIMAL(8, 2), PnL DECIMAL(8, 2)); INSERT INTO @t (Date, Session, TotalMembers, TotalLitres, Amount) SELECT Cast(Date as date), Session, Count(MemberCode), Sum(Litres), Sum(Amount) FROM myTable Where Col1 = 'P' and Date between '2013-07-01' and '2013-07-09' GROUP BY Cast(Date as Date), Session Order By Cast(Date as date)declare @maxcount int,@loop intdeclare @cr money,@dr moneyset @loop =1select @maxcount= MAX(SlNo )from @twhile(@loop <= @maxcount) begin Update @t Set GTFat = (Select Fat From mTran Where Date=Date and Session=Session and Col1='GT'), GTPrice=(Select Price From mTran Where Date=Date and Session=Session and Col1='GT') set @loop = @loop + 1endSELECT *FROM @torder by Cast(Date as date)'