CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Give SQL Query for this question?

    ID Close
    ---- ------------
    1 27718
    2 27741
    3 27661
    4 27626
    5 27647
    6 27651
    7 27685
    8 27733
    9 27770
    10 27797
    11 27893
    12 27846
    13 27837
    14 27853
    15 27835
    16 27829
    17 27881
    18 27890
    19 27868
    20 27827
    21 27843
    22 27839
    23 27828
    24 27829
    25 27799
    26 27760
    27 27779

    How i can get average of close columns from id number 1 to all like 1 to 5, 2 to 6, 3 to 7, 4 to 8, 5 to 9 etc... The result should be automatically generated for further id's.

    Anyone Please give me a SQL query ..Table Name is " sma ".. ( or ) give me in C# program..
    Please do need full.

  2. #2
    Join Date
    Aug 2007
    Posts
    179

    Re: Give SQL Query for this question?

    create table test (
    id int,
    data float)
    insert into test values (1, 27718)
    insert into test values (2, 27741)
    insert into test values (3, 27661)
    insert into test values (4, 27626)
    insert into test values (5, 27647)
    insert into test values (6, 27651)
    insert into test values (7, 27685)
    insert into test values (8, 27733)
    insert into test values (9, 27770)
    insert into test values (10, 27797)
    insert into test values (11, 27893)
    insert into test values (12, 27846)
    insert into test values (13, 27837)
    insert into test values (14, 27853)
    insert into test values (15, 27835)
    insert into test values (16, 27829)
    insert into test values (17, 27881)
    insert into test values (18, 27890)
    insert into test values (19, 27868)
    insert into test values (20, 27827)
    insert into test values (21, 27843)
    insert into test values (22, 27839)
    insert into test values (23, 27828)
    insert into test values (24, 27829)
    insert into test values (25, 27799)
    insert into test values (26, 27760)
    insert into test values (27, 27779)


    select * from test

    declare @A int;
    declare @B int;

    set @A = 1;
    set @B = 5;

    while @B <= (select max(id) from test) begin
    select @A as id_start, @B as id_end, avg(case when id >= @A and id <= @B then data end) as avg_Data from test;
    set @A = @A +1;
    set @B = @B +1;
    end;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured