Average the value based on day in SQL

I have a table with two fields (SampleTime, value) the table likes:
SampleTime field2
1999-01-01 01:00:00 0.01
1999-01-01 02:00:00 0.01
1999-01-01 03:00:00 0.03
The sample time has one hour interval. How can get average value of field2 based on day? like:
1999-01-01 average of all values within 1/1/99
1999-01-02 average of all values within 1/2/99
1999-01-03  average of all values within 1/3/99
Is there a build-in function in SQL to implement this?  It should be very sample. But I am not sure how to write this SQL query statement. Thanks. 

Can explain the parameters : varchar(8), sampletime, 10?
What do these parameters mean?
Thanks.
you wanted the date in mm/dd/yy format... convert(varchar(8),sampletime,10) gets you the output in that format..
you did not mention your sql version, if sql 2008 and above, you can use date funtion(it the format does not matter).
declare @table table (SampleTime datetime,field2 decimal(3,2))
insert into @table
values
('1999-01-01 01:00:00','0.01'),
('1999-01-01 02:00:00','0.01'),
('1999-01-01 03:00:00','0.03')
select avg(Field2),Convert(varchar(8),sampletime,10) from @table group by Convert(varchar(8),sampletime,10)
--SQL 2008 and above
select avg(Field2),Cast(sampletime as date) from @table group by Cast(sampletime as date)
Hope it Helps!!

Similar Messages

  • Calculate Average value based on Day ??

    Hello,
    I am trying to calculate the Average value based on a day. The data is presented as follows...
    Day          SOCount
    Mon                34
    Mon                 56
    Mon                 67
    Tues               24
    Tues               25
    Tues               23
    Weds              45
    Weds              69
    The issue im having is that the Day column needs to be grouped first and the SOCount sumed together. Then the Average SO Count needs to be calculate based on this.
    Thanks

    Thanks for the reply,
    The solution you have provided only gives me the average of the count of the SO Count, not the actual average of all the values added together then  divided by the count..
    The report i am creating only has charts in it. So i am trying to create a chart showing the
    Average Sale Order Value by day.
    I should have metioned this from the start, sorry.
    Is it possible to do ?
    Edited by: davitali on Nov 4, 2011 6:32 AM

  • Returning a value based on user-supplied sql variables

    I'm using JDeveloper 10.1.3.1, ADF and JSP.
    I want to return a primary key based on values a user enters in unbound fields (like inputText). They'll be entering information for a record including a 10-segment GL code, and when the other bound values are committed, I'd like the associated GL id to be returned for the row (if found). Validation is not necessary at this point. A null return is okay.
    I've defined bind variables in a GL view object - one for each of the 10 segments - plus a jspx form with 10 inputText objects.
    Does it sound like this is going in a logical direction? I'm stuck at this point, and am open to suggestions.

    1. Open the formula workshop.
    2. From the Repository Custom Functions, under Crystal and then Date, RIGHT click on cdlastdayofmonth, click on ADD TO REPORT.
    3. Create a new formula, in the formula workshop, under FUNCTIONS, go down the list till you see "CUSTOM FUNCTIONS", expand that till you see cdlastdayofmonth.
    4. In your formula, type cdlastdayofmonth(currentdate)
    5. Save and close and display the formula in your report, you should see 11/30/2008.
    If you want just the day then modify the formula to:
    totext(day(cdlastdayofmonth(currentdate)),0,'','');
    since you have parameters for month and year, do this:
    totext(day(cdlastdayofmonth(date({?year},{?month},01))),0,'','');
    to give you the last day of the month.

  • Unable to use the values returned by a PL/SQL stored procedure in a XSQL page

    Hi,
    I've been messing around with XML and XSQL in particular. I was trying to write a xsql page to display a report with account totals...I have the following .xsql which calls a PL/SQL stored procedure :
    <?xml version="1.0"?>
    <xsql:query connection="pfcdm" xmlns:xsql="urn:oracle-xsql">
    <xsql:set-session-param name="zasset_total" value="100">
    <xsql:dml connection="pfcdm">
    rraman.sp_vw_id(zasset_total,zinvm_total,zmkt_val);
    </xsql:dml>
    </xsql:set-session-param>
    select 'Asset total is {@zasset_total}' as "ASSET_TOTAL" from dual
    </xsql:query>
    My procedure sp_vw_id returns the values that it should. But, I am not sure how to declare variables within a page, and to output the return values. There is very scanty documentation on the usage of <xsql:dml> or <xsql:ref-cursor-function>.
    Any response would be greatly appreciated.
    Thanks,
    Raja

    Here is the example from the Oracle9i (complete rewrite) of the XSQL Chapter in our Oracle documentation.
    Question
    I using <xsql:dml> to call a stored procedure which has one OUT parameter, but I was not able to see any results. The executed code results in the following statement:
    <xsql-status action="xsql:dml" rows="0"/>
    Answer
    You cannot set parameter values by binding them in the position of OUT variables in this release using <xsql:dml>. Only IN parameters are supported for binding. You can create a wrapper procedure that constructs XML elements using the HTP package and then your XSQL page can invoke the wrapper procedure using <xsql:include-owa> instead.
    For an example, suppose you had the following procedure:
    CREATE OR REPLACE PROCEDURE addmult(arg1 NUMBER,
    arg2 NUMBER,
    sumval OUT NUMBER,
    prodval OUT NUMBER) IS
    BEGIN
    sumval := arg1 + arg2;
    prodval := arg1 * arg2;
    END;You could write the following procedure to "wrap" it, taking all of the IN arguments that the procedure above expects, and then "encoding" the OUT values as a little XML datagram that you print to the OWA page buffer:
    CREATE OR REPLACE PROCEDURE addmultwrapper(arg1 NUMBER, arg2 NUMBER) IS
    sumval NUMBER;
    prodval NUMBER;
    xml VARCHAR2(2000);
    BEGIN
    -- Call the procedure with OUT values
    addmult(arg1,arg2,sumval,prodval);
    -- Then produce XML that encodes the OUT values
    xml := '<addmult>'&#0124; &#0124;
    '<sum>'&#0124; &#0124;sumval&#0124; &#0124;'</sum>'&#0124; &#0124;
    '<product>'&#0124; &#0124;prodval&#0124; &#0124;'</product>'&#0124; &#0124;
    '</addmult>';
    -- Print the XML result to the OWA page buffer for return
    HTP.P(xml);
    END;This way, you can build an XSQL page like this that calls the wrapper procedure:
    <page connection="demo" xmlns:xsql="urn:oracle-xsql">
    <xsql:include-owa bind-params="arg1 arg2">
    BEGIN addmultwrapper(?,?); END;
    </xsql:include-owa>
    </page>This allows a request like:
    http://yourserver.com/addmult.xsql?arg1=30&arg2=45
    to return an XML datagram that reflects the OUT values like this:
    <page> <addmult><sum>75</sum><product>1350</product></addmult>
    </page>

  • How to populate the values based on different selections in Dashboard Promp

    Hi,
    I have a group prompt, which has 2 drop downs. The DropDown1 decides the values of DropDown2.
    I have used a SQL query in the "Show" values which will populate the dropdowns.
    When a value is 'selected" from DropDown1, the selected value decided the values of DropDown2 which is again a SQL query.
    But I see that when a new value is selected from DropDown1, the values in DropDown2 will not change until I click on GO.Even after clicking GO, the previously selcted value is retained.
    As an illustration:
    animals,plants are the choices in the first drop down, which if, animal is selected would populate the dropdown2 with cow,sheep, tiger.
    Assume animal is selected. The second drop down now is populated with cow,sheep and tiger. Assume you chose sheep.This resulted in display of sheep related info on the BI page.
    Now if I go back to the first dropdown and select plants, I would expect the second drop down to be populated with fig, palm, coconut.
    However in my case I see only the proviously popluated animals in the DropDown2.
    If I click on Go, I see that the DropDown2 is populated with Sheep,,fig, palm, coconut. Ideally I should not have seen the prevously selected value in DropDown2.
    Here there are 2 problems stated for a group promt having 2 DropDowns where item chosen from one decides the values of the other dropdown
    1. On selecting an item from DropDown1, is not populating with the corresponding values in DropDown2
    2. When 'Go' is clicked, DropDown2 is populated with the corresponding values for DropDown1 + the previously selected value in DropDown2 is retained.
    Any input to get out of this problem will help
    thanks
    Shubha

    Hi
    I am using group prompt and has 4 drop downs.
    Value of one decides the other. Values for the dropdowns will be populated by SQL results.
    'Constrain' will not be available for sql results..
    How to go about this?
    thanks
    Shubha

  • Restricting the values based on keyfigures.

    Hi All,
    Good Morning..
    I have a query on Bex reporting.
    I want to calculate transit aging.Just i want to find the difference between the two dates.
    I have one more field called quantity.
    I want to calculate the difference of dates only whose quantity is "ZERO". I can restrict the keyfigures based on characteristics. But for my case i want to restrict the keyfigures based on Quantity keyfigures.
    Is it possible in Query designer.
    Thanks in advance.
    Thanks,
    Siva.

    Hi,
    p.o number ______p.odate_______consimentdate______qty__aging
    87000001______01.02.2009______02.02.2009________1___ nil
    87000002______05.02.2009______07.02.2009________0____( current date - consiment date)
    Craete Two formula Variables with replacement path in columns..
    1. One is for PO date and replace with Key.
    2. Other is forconsiment date and replace with Key.
    then just drag and drop in two columns in report and see the output. Then create one formula and do substraction. In another column you craete one more formula and use If condition
    Column A = p.odate (with Replacemet variable value)
    Column B = consimentdate (with Replacemet variable value)
    Column C = B-A
    Column D =  If QTY <> 0 then C
    Thanks
    Reddy

  • How to group the output based on DAYS field

    Hi
    I am modifying the predefined ALV report "RFTMBL01" .I had added the days field to that report output and I have to group the output based on the DAYS field
    Records within 0-10 days in one group
    Records within 10-30 days in one group
    Records above 30 days should be  one group
    No of days may repeat that means with no of days 10 .many records will be there
    There is also a field by name "AMOUNT" in my output.
    I have to calculate SUBTOTALs at the end of every group and at the end of the report i should caluculate GRAND TOTAL.
    Please remember that i should not use any any BLOCKED ALVs and for Totals i should not use the SYMBOLS provided in the application toolbar of the report
    Thanks in Advance

    <b>>>>The file has to be routed based on the Company Code</b>
    check with xpaths.
    /people/shabarish.vijayakumar/blog/2006/06/07/customise-your-xpath-expressions-in-receiver-determination
    /people/shabarish.vijayakumar/blog/2005/08/03/xpath-to-show-the-path-multiple-receivers
    <b>>>>different file should have different file name again based on the company code.</b>
    /people/jayakrishnan.nair/blog/2005/06/20/dynamic-file-name-using-xi-30-sp12-part--i
    /people/jayakrishnan.nair/blog/2005/06/28/dynamic-file-namexslt-mapping-with-java-enhancement-using-xi-30-sp12-part-ii

  • Formula to Copy all the values based on condition

    Hi All,
          I have Text Column called "PASS" in Pivot table holding values like and NA and 0.0133333333333333 .
          So I want to create the calculate column "CALS "and copy all the values which are not holding NA into this column and set value 0 if found NA.Can any one help me how can I solve this.
    Thanks,
    Sid

    Hello Sid,
    For this you can use a calculate column a simple condition with the IF function like
    =IF(MyTable[MixedValueColumn] = "NA", 0, CURRENCY(MyTable[MixedValueColumn]))
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • How to dsplay the values based on the check box?

    forms6i
    Hi to all
    the requirment the we have already master detail form.example emp details based on the dept number.in the form just only displays three coloumns per record wise(emp details).
    actually my requrment is place the check box each record and place a one button in the form.once check the particular record and click on the button open the another form
    it's displays that particular record entire emp details.any one help me...........
    Edited by: 994418 on 3 Apr, 2013 1:50 AM

    994418 wrote:
    forms6i
    Hi to all
    the requirment the we have already master detail form.example emp details based on the dept number.in the form just only displays three coloumns per record wise(emp details).
    actually my requrment is place the check box each record and place a one button in the form.once check the particular record and click on the button open the another form
    it's displays that particular record entire emp details.any one help me...........
    Edited by: 994418 on 3 Apr, 2013 1:50 AMHi,
    As you said, you emp block is tabular, i think there is no need of check box. Just add a button in that block. So there is a button for each record and associate button click display the associate record details (you said to go another form and display)
    check this Calling a form and passing a context
    Hope this helps
    Hamid
    Mark correct/helpful to help others to get right answer(s).*

  • How to sum the values based on the for all entries

    for example :
    select aufnr vornr sum (ISM02) sum ( ISM04) Sum (ism05) from afru into table I_afru for all entries of I_caufv where aufnr = i_caufv-aufnr group by aufnr.
    can any one help based on this query
    without forall entries it is working based for all entries only i want the solution Help me please.
    Regards
    Augustine

    Simple,
    select aufnr vornr ISM02 ISM04 ism05 from afru into table
    I_afru for all entries of I_caufv
    where aufnr = i_caufv-aufnr .
    <b>if sy-subrc eq 0.
    sort I_afru by aufnr.
    loop at l_afru.
    at end of aufnr.
    SUM.
    l_afru_temp = l_afru.
    append l_afru_temp.
    endat.
    endloop.
    endif.</b>

  • Reading data from table and print the value based condition

    hi
    my table like this
    slno  |  value1 |  value2  |
    1            0            5
    2            5           10
    3           10          15
    assume n= 8 ( where  n is user define)
    i want output like
    number 8 between value  5 and  10
    plz help

    Hi,
    Try:
    Select 'Number '+ U_Value3 + ' Between '+ U_Value1'and 'U_Value2
    From [dbo\].[@UDT T0\]
    Where T0.U_Value3=[%0\] and T0.U_Value3 BETWEEN U_Value1 AND U_Value2
    Thanks,
    Gordon

  • Restricting the values based on exit

    Hi,
    I have a report in which it is displaying the user details with logged date information.  But my requriement if the user is not there then that person should not be display.  I am very new to BW how to restrict in the query.
    Thanks
    Naveen

    Hi Naveen
    Please let me know, if i have understood your question correct.
    You are executing a report which shows some user details. You dont want the information for the user, who are no more able to access the system. Which means the user were there before and now they have been removed from the system. Am I right?
    Sriram

  • LOV Data will render dynamically based on the value in adv table

    Hi Friends,
    am wokring on the requirement of iprocuremnt
    1) in iprocurement checkout page we have option called edit lines , when i click edit lines , the lines items will show catalog items and non catalog items.
    2)if item should be non catalog delevere to location lov data should be restrict
    3)if item should be catalog delevere to location lov data should be all locations
    means dynamically how to restrict the lov data in line level advance table
    can you help me on how to achieve this requirement
    Thanks
    krish.

    If you are using messageLovInput you can try this approach.
    1. In your CO catch 'lovPrepare' event [this event is triggered when user clicks on the magnifying icon besids the lov]
    2. Extract the values based on which you want to filter the LOV data
    3. Call VO's initQuery method to modify the Where clause of VO related to this LOV.
    Code...
    String eventActionParameter = pageContext.getParameter(EVENT_PARAM);
    if(eventActionParameter.equals("lovPrepare")) {
    //Extract the values
    //Call a AM method to very the Where clause
    }

  • Doubt in the value return by the function based on given condition

    Hi,
    Below function should return the value based on the value of in_row_number_high variable.
    I have used CASE but it does't return proper value.
    How could I give that condition?
    Please help.
    FUNCTION gen_total_lane_count_pct
        in_lane_id                  edr_rpt_by_ranges_output.lane_id%TYPE,
        in_direction_id             edr_rpt_by_ranges_output.direction_id%TYPE,
        in_interval_start_date_time edr_rpt_by_ranges_output.interval_start_date_time%TYPE,
        in_interval_end_date_time   edr_rpt_by_ranges_output.interval_end_date_time%TYPE,
        in_row_number               edr_rpt_by_ranges_output.range_low%TYPE,
        in_row_number_high          edr_rpt_by_ranges_output.range_high%TYPE,
        in_lane_min                    edr_lane_by_class_report_data.v_lane%TYPE,
        in_lane_max                    edr_lane_by_class_report_data.v_lane%TYPE   
    RETURN NUMBER
    IS
         my_total             NUMBER(18);
         my_count_pct         NUMBER(18);
         my_total_pct         NUMBER(18);
         my_each_count_result NUMBER(18);
         my_count_result            NUMBER(18);
         my_total_count             NUMBER(18);
      lane_start           edr_rpt_tmp_report_lanes.site_lane_id%TYPE;
      lane_end             edr_rpt_tmp_report_lanes.site_lane_id%TYPE;
      row_start            NUMBER(12);
      row_end              NUMBER(12);
    BEGIN
         my_count_pct       := 0 ;
           SELECT MIN(site_lane_id)
             INTO lane_start
          FROM edr_rpt_tmp_report_lanes
         WHERE edr_rpt_tmp_report_lanes.output_lane_id        = in_lane_id
           AND edr_rpt_tmp_report_lanes.output_direction_id   = in_direction_id;
         SELECT MAX(site_lane_id)
              INTO lane_end
           FROM edr_rpt_tmp_report_lanes
          WHERE edr_rpt_tmp_report_lanes.output_lane_id        = in_lane_id
            AND edr_rpt_tmp_report_lanes.output_direction_id   = in_direction_id;  
         SELECT MIN(range_low)
           INTO row_start
           FROM edr_rpt_by_ranges_output;
         SELECT MAX(range_low)
           INTO row_end
           FROM edr_rpt_by_ranges_output;
               my_each_count_result := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        in_row_number,
                                        in_row_number_high,
                                        in_lane_min,
                                        in_lane_max             
        my_count_result := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        row_start,
                                        row_end,
                                        in_lane_min,
                                        in_lane_max              
         my_total_count :=  edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        in_row_number,
                                        in_row_number_high,
                                        lane_start,
                                        lane_end               
             IF (my_each_count_result > 0)   THEN                          
                   my_total := edr_rpt_lane_by_class_package.gen_total_lane_count (
                                        in_lane_id,
                                        in_direction_id,
                                        in_interval_start_date_time,
                                        in_interval_end_date_time,
                                        row_start,
                                        row_end,
                                        lane_start,
                                        lane_end              
                CASE
                WHEN in_row_number_high = row_end THEN                       
                     my_count_pct := ROUND((my_each_count_result / my_count_result ) * 100 );     
                WHEN in_row_number_high < row_end THEN                
                        my_count_pct := ROUND((my_each_count_result / my_total ) * 100 ) ;               
                ELSE
         RAISE_APPLICATION_ERROR(-20101, 'The row number specified is not recognized.');                 
                END CASE;     
             END IF;
             return my_count_pct;
    END gen_total_lane_count_pct;          Edited by: Indhu Ram on Jan 5, 2010 11:43 AM

    Hi,
    I have to find out the % value for each column value( For rx if there is only 2 columns).To calculate the % , each column value should get divided by the total of that column value.
    To calculate the % for the total row , each column total should get divide by the sum of column ( sum of column 1 + sum of column 2)values.
    ROUND((my_each_count_result / my_count_result ) * 100 ) ---- This calculaton gives me the % for the total row.
    ROUND((my_each_count_result / my_total ) * 100 ) ------- This calculation gives the % for the data row.
    The condition that I have tried is, if it reaches end of data row (to achieve that condition I have given this condition in_row_number_high < row_end )
    Now my result returns only values based on this condition -- in_row_number_high < row_end .
    It does't look for the first condition.
    CASE
    WHEN in_row_number_high = row_end THEN
    my_count_pct := ROUND((my_each_count_result / my_count_result ) * 100 );     
    WHEN in_row_number_high < row_end THEN
         my_count_pct := ROUND((my_each_count_result / my_total ) * 100 ) ;               
    ELSE
    RAISE_APPLICATION_ERROR(-20101, 'The row number specified is not recognized.');          
    END CASE;
    Hope , you have some good understanding now. Please help me how could I do this?

  • Is there a way to fill a cell with a value based on the selection from another cell?

    For example: If have a drop down menu created for Cell A1 with options (Dog 1, Cat 2, Bird 3). Is there a way to fill cell A2 automatically when I select from the drop down menu.
    So if I selected 'Cat' in A1, cell A2 would automatically input the 2.

    I suggest an extensible method that allows you to add other animals:
    This method adds a small table called "Animal Lookup" that matches names (same as in the pop-up menu) with a value.
    The table on the left uses this table to retrieve the value based on the animal:
    B2=VLOOKUP(A2, Animal Lookup :: A:B, 2, 0)
    select B2 and fill down

Maybe you are looking for

  • Solved - D Series Hard Drive Tray interchang​e

    All, I think I've solved the mystery of the hard drive tray interchange for at least the D10 (I don't have a D20 or D30 to compare with, but I think they're similar). When I was taking my system apart for spring cleaning to evict all of the dust bunn

  • Sub reports

    Hi All, I have a report having a command object where I have empno and some columns. And I have another command object which also having empno. The 2nd command object should get some empnos from 1st command object and should display countries based o

  • Why insert a row will cause a sort (it can be saw from autotrace)?

    When I simply insert a row into a table, I could also see there is a sort in memory. But why insert need a sort? C:\Documents and Settings\qiwu>sqlplus qihua/qihua SQL*Plus: Release 10.2.0.1.0 - Production on Sun Jul 27 19:51:51 2008 Copyright (c) 19

  • Sequence in Control File

    How to create sequence in control file from command prompt.

  • Adobe X install in Terminal Server environment

    In relation to this link: http://helpx.adobe.com/acrobat/kb/slow-display-performance-terminal-server.html I need to know if these registry keys are different for Adobe X – Also need to know if they should be applied to HKLM instead of HKCU so the pol