PDA

View Full Version : Solved: nested loop on sql statement



Gtrain
08-03-2010, 10:25 PM
Ok guys I am trying get some info by dates and having a little trouble with my statement, i basically want to change the year and month i am looking at, but leave the day as the first
ie 2008-01-01, 2008-02-01....2010-07-01, 2010-08-01

i think i am doing something wrong in converting to a date and i am not actually sure my loops will work, first time looking at sql loops forgive me.

here is my code, any help?

declare @yr int
declare @mth int
set @yr = 2007
set @mth = 0
while @yr<=2010
begin
while @mth<=12
begin
SELECTcount(case when expirydate <= @yr + '-' + @mth + '-01' then linecode end)as [ended],
count(case when deliverydate <= @yr + '-' + @mth + '-01' then linecode end)as [current welds],
count(case when deliverydate <= @yr + '-' + @mth + '-01' then linecode end)
-count(case when expirydate <= @yr + '-' + @mth + '-01' then linecode end)as [Total]
FROM sometable
where code ='xxx'
end
end

is this also possible without creating a table?

Gtrain
08-04-2010, 02:32 AM
had to convert to varchar, :banghead:

this can be closed Thanks