while select returns different tables

  • Hello

    My querry returns 4 different tables with each one row.

    But I want one table with 4 rows. How to?

    Thanks rembri

    DECLARE @startdate as datetime

    DECLARE @enddate as datetime

    SET @startdate ='2011-04-04 07:00:00'

    while DATEADD(dd,1,@startdate) < GetDate()

    Begin

    SET @enddate = DATEADD(dd,1,@startdate)

    select @startdate as startdate,@enddate as enddate, count(*) as count, avg(DateDiff(ms, x1.StartDateTime, x1.EndDateTime)) as DurationAv

    from x1 with (nolock)

    where EndDateTime > @startdate

    and EndDateTime < @enddate

    SET @startdate=DATEADD(dd,1,@startdate)

    end

  • Try this. I didnt test the code as I do not have sql installed in my system.

    DECLARE @startdate as datetime

    DECLARE @enddate as datetime

    DECLARE @T TABLE (startdate DATETIME, enddate DATETIME, count AS INT, DurationAv INT)

    SET @startdate ='2011-04-04 07:00:00'

    while DATEADD(dd,1,@startdate) < GetDate()

    Begin

    SET @enddate = DATEADD(dd,1,@startdate)

    INSERT INTO @T(startdate, enddate, count, DurationAv)

    select @startdate as startdate,@enddate as enddate, count(*) as count, avg(DateDiff(ms, x1.StartDateTime, x1.EndDateTime)) as DurationAv

    from x1 with (nolock)

    where EndDateTime > @startdate

    and EndDateTime < @enddate

    SET @startdate=DATEADD(dd,1,@startdate)

    end

    SELECT * FROM @T

    _____________________________________________
    One ounce of practice is more important than tonnes of dreams

  • Great, its easy if you know how.

    Thanks and regards rembri

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply