An Average of a Count erroneously returns integers and not decimals

I have a table which holds sales information showing the invoice number and the branch (e.g. 'New York', 'Boston', etc..)  that sold the items. Each row represents a product on an invoice. I am trying to find out the average number of lines per invoice
for each branch. So my final result set might tell me that the 'New York' invoices had an average of 2.4 lines per invoice and the 'Boston' invoices had an average of 1.9 lines per invoice. Like this:
New York, 2.4
Boston, 1.9
I've first written a subquery that counts the number of lines for each BRANCH & INVOICE combination for any date after 1/1/2015. Then I've put a query around that subquery which averages this count by Branch. The problem is, the entire query is only
returning integers and not decimals. Why is this?
Note that I tried casting the data type to DECIMAL and FLOAT but still get the same results.
Here is the query:
Select t1.Branch, cast(avg(t1.Count) as numeric(10,2))
as Avg_Lines
from
(Select Branch, invoice, count(*) as Count
 from linprm
 where invdte>20150101
 group by Branch, invoice) t1
 group by t1.Branch

To expand Scott's answer:
Select t1.Branch, avg(cast(t1.Count as numeric(10,2)))
as Avg_Lines
from
(Select Branch, invoice, count(*) as Count
from linprm
where invdte>20150101
group by Branch, invoice) t1
group by t1.Branch
-- or
Select t1.Branch, cast(avg(t1.Count) as numeric(10,2))
as Avg_Lines
from
(Select Branch, invoice, cast(count(*) as numeric(10,2)) as Count
from linprm
where invdte>20150101
group by Branch, invoice) t1
group by t1.Branch
Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com

Similar Messages

  • Average filter but the image is gray and not color RGB

    Hi,
    i use this code for an average filter 3x3, the result is the image whit average but gray and not RGB color.... why??
    thanks in advance
        public void Average()
            int values[]=null;
            int input2d[] = new int [image.getWidth()*image.getHeight()];
            int [][] output2d=new int [image.getWidth()][image.getHeight()];
            image.getRaster().getSamples(0, 0, image.getWidth(), image.getHeight(),0,input2d);
            for(int y=0; y < image.getHeight(); y++)
                for(int x=0; x < image.getWidth(); x++)
                    int count = 0;
                    int average=0;
              values = new int[5*5];
                    for(int s = 0; s < 5; s++)
                        for(int r = 0; r < 5; r++)
                            try
                                values[count] = input2d[(y-2+r)*image.getWidth()+(x-2+s)];
                                count++;
                            catch(ArrayIndexOutOfBoundsException e){ }
                    //trovo il valore medio dell'intorno 5x5
                    for (int temp=0; temp<count; temp++)
                        average+=values[temp];
                    int valpixel=average/count;
                    output2d[x][y]=valpixel;
            image2 = new BufferedImage(image.getWidth(),image.getHeight(), BufferedImage.TYPE_INT_RGB);
            image2.getGraphics().drawImage(image,0,0,null);
            for(int y=0; y < image.getHeight(); y++)
                for(int x=0; x < image.getWidth(); x++)
                    image2.getRaster().setSample(x, y, 0, output2d[x][y] );// componente R
                    image2.getRaster().setSample(x, y, 1, output2d[x][y] );// componente G
                    image2.getRaster().setSample(x, y, 2, output2d[x][y] );// componente B
        }

    Can hel me please!!!

  • Why does the new dvd superdrive keep returning disk and not reading it

    Just got this usb superdrive and everytime put a disk in it it comes back not reading anything 

    Welcome to Apple Support Communities
    Return it and get another one. The Apple USB SuperDrive sometimes doesn't work correctly, and other external disc drives work

  • List Managers Displays Return Value and Not Display Value

    Hi
    I have created a List Manager (based on a Popup LOV). My lov is as follows:
    SELECT pt.paper_code d, pt.paper_icode r
    FROM uo_sturec.paper_table pt
    WHERE pt.start_year <= 2006
    AND (pt.finish_year >= 2006 OR pt.finish_year = 0)
    AND pt.paper_type = 60
    ORDER BY pt.paper_code
    However, when I select something from the popup, it displays the pt.paper_icode in the field and when I click on Add, the pt.paper_icode is added to the selction box.
    What can I do to make the display value, pt.paper_code appear both in the field and the selection box?
    Kind regards
    Jo

    Hi Scott
    Thank you for your reply. However, I want the return value to be pt.paper_icode, because I'll be using this value to populate another item on the same page. Is there some javascript I could write to just change the display value of the List Manager while preserving its return value?
    Thanks.
    Jo

  • Can I count the PWM pulses and note the timer value simulataneously.

    Im using 6024E. I want to be used for 1-phase motor control. I could generate PWM pulses and read the feedback analog current, but I am unable to read the velocity of the motor. Counting PWM pulses(generated by sensor for each degree of rotation) and reading the digital timer value simultaneously to calculate velocity of the motor. If any body can give the solution it will be appreciated
    Attachments:
    motor_control_ver1.0.llb ‏210 KB

    Hello;
    In case you can read a digital pulse train that would give you the speed of the motor, you can use one of the counters of the DAQ board you have to accomplish that.
    You can configure the counter as buffered event counter, hook up the digital pulse train to the counter gate, and since you have the frequence of the internal timebase that is connected to the counter input, you can calculate the speed of the motor by having the count values that are stored at the event of a gate input pulse.
    Hope this helps.
    Filipe A.
    Applications Engineer
    National Instruments

  • Select extract(month from sysdate)+6 = returns 13 and not 1(jan)

    Hi
    I am using the following query to get the month 6 months from now.
    select extract(month from sysdate)+6
    from dualHow can I say that the month should be 6 months from todays month? It worked all these days because this month + 6 was always a valid month. Now it returns 13.

    When I run the following, I get '1'. I am expecting '01'. What am I doing wrong here?
    SELECT substr(lpad(to_number(to_char(ADD_MONTHS(SYSDATE, 6),'MM')),2),1,2) FROM DUAL;
    I need number - so using to_number. I need to extract the month, so using to_char. If someone could help me get 2 digits for the month and simplify the query if possible - would be great.
    The table has month and date stored as number (bad idea I know but thats how it is). Now for January 31, it is stored as 131. For 15 Feb it is 215. What I need is give me records where the month is 6 months from now. Something like:
    to_number(substr(lpad(tbl1.monthdt, 4, '0'), 1, 2)) in
    substr(lpad(to_number(to_char(ADD_MONTHS(SYSDATE, 6),'MM')),2),1,2)Message was edited by:
    bpel

  • Why this function returns 3 and not assign a number?

    Hi all,
    call function 'NUMBER_GET_NEXT'
           exporting
                nr_range_nr             = '01' 
                object                  = 'ZSBMDNUM'
           importing
                number                  = ZMM_TR_STR-dosyano
           exceptions
                interval_not_found      = 1
                number_range_not_intern = 2
                object_not_found        = 3
                quantity_is_0           = 4
                quantity_is_not_1       = 5
                interval_overflow       = 6
                others                  = 7.
    Thanks.

    Hi,
    follow the below procedure for auto number generation.
    From SNRO create a custom no. range object. When creating it specify
    Short, long text,Number length domain (Ex: NUM5) and Warning % (Ex:
    10.0.).
    After creating it maintain the intervals by clicking the button
    Number Ranges from the 1st screen and in the next screen press
    change intervals and then maintain the intervals (From number, To
    number, Current no etc.).
    Now u can use this no. range object with the FMs, those I mentioned
    in my earlier reply.
    You can maitain a number range object from tran. SNRO and can
    fetch
    the next available no. using the FM 'NUMBER_GET_NEXT'.
    Also check the FMs below for further operations on number range.
    'NUMBER_RANGE_OBJECT_READ',
    'NUMBER_RANGE_INTERVAL_LIST'
    'NUMBER_RANGE_OBJECT_LIST'
    'NUMBER_RANGE_OBJECT_GET_INFO'
    Reward if useful,
    Thanks,
    Sreeram.

  • Count(1) returns null in group by

    hi gems..good afternoon..
    I read that the COUNT() function always returns 0 (zero) if there is no matching rows in the table.
    The following code returns the 0 as expected:
    SELECT COUNT(1) FROM book_table
                 WHERE client_id = 10009
                   AND book_id = 5465465
                   AND book_sub_id = 'gfdf'
                   AND amount = 78686But when I used the GROUP BY clause with the query, then it returned nothing:
    SELECT COUNT(1) FROM book_table
                 WHERE client_id = 10009
                   AND book_id = 5465465
                   AND book_sub_id = 'gfdf'
                   AND amount = 78686
    group by client_id,book_id,book_sub_id,amountWhy this is happening..please suggest...

    gogol wrote:
    But Ranit...
    Again I am thinking...the COUNT() is an aggregate function. Now a function should return something(as per my plsql knowledge) and in this case the return datatype is integer. So why isnt it returning zero..Don't think like that sandy.
    The Group By is actually done on an empty result set, so the result is neither 0 nor NULL
    It is an empty result set.
    Check this -- http://stackoverflow.com/questions/2552086/does-count-always-return-a-result
    >
    The "return value of the 'count' function" is ALWAYS a non-null integer, without exception. By mentioning "group by", you're referencing the containing query and changing the subject of "return value" from "count function" to "query's result set". A non-grouped count query produces a result set of a single record containing the return value of count. Alternatively, a grouped count query produces a result set where each record contains a count value. In that case, if there are no groups for count to run on, count is never run and the "query return value" is an empty set.
    >
    Hope this Helps.
    Ranit B.
    Edited by: ranit B on Nov 23, 2012 5:25 PM

  • How to Tune EM Metric Alert "Average Users Waiting Count"?

    We have been getting below EM alert frequently , How can tune/fix this issue?
    Metric=Average Users Waiting Count
    Wait Class=Concurrency
    Severity=Warning
    Message=Metrics "Average Users Waiting Counts" is at 84.66522 for event class "Concurrency"

    Have a look at this to understand the Metrics first.
    Average Users Waiting Count
    <br>
    Oracle Database FAQs
    </br>

  • Count problem...'IN' and 'NOT IN' problem?

    I'm not sure what I missed here. Can anyone help?
    I have an address_main table, and a case_main table. Case_main attach case to address by using address_id, which is a unique number.
    SQL> select count(address_id) from address_main;
    COUNT(*)
    39576
    SQL> select count(address_id) from address_main
    where ADDRESS_ID in (select address_id from case_main);
    COUNT(ADDRESS_ID)
    39080
    I'm expecting the next statement come back a count result at 456 (39576-39080), but it returns '0'.
    SQL> select count(address_id) from address_main
    where ADDRESS_ID NOT IN(select address_id from case_main);
    COUNT(ADDRESS_ID)
    0
    So 'IN' and 'NOT IN' can not cover all the posibility?

    Check for NULL values, they often affect results just the way you describe.
    Consider:
    SQL> create table address_main(x) as
      2  select rownum
      3    from dual
      4  connect by level <= 5;
    Table created.
    SQL> create table case_main(x) as
      2  select rownum
      3    from dual
      4  connect by level <= 5;
    Table created.
    SQL> -- We'll update just one row in CASE_MAIN with NULL value
    SQL> update case_main set x = NULL where x = 5;
    1 row updated.
    SQL> select * from address_main;
             X
             1
             2
             3
             4
             5
    SQL> select * from case_main;
             X
             1
             2
             3
             4
    SQL> select count(x) from address_main;
      COUNT(X)
             5
    SQL> select count(x) from address_main
      2   where x in (select x from case_main);
      COUNT(X)
             4
    SQL> -- But ...
    SQL> select count(x) from address_main
      2   where x not in (select x from case_main);
      COUNT(X)
             0

  • I'm trying to write a formula to return a blank cell if the condition is met, and to show the numerical difference if not.  Here is what I've written:=IF((F35-F29)=0, " ", (F35-F29)). It is returning 0.00, not a blank cell.  Where is my error?

    I'm trying to write a formula to return a blank cell if the condition is met, and to show the numerical difference if not.  Here is what I've written:=IF((F35-F29)=0, " ", (F35-F29)). When the condition is met (i.e., the difference=0) it is returning 0.00, not a blank cell.  Where is my error?

    Without knowing what is in F35 and F29, I can only guess.  Your formula matches what you said but if F35 and/or F29 are the results of formulas and are decimal numbers (not integers), it is very possible that they are not exactly the same number even though you think they should be.  IEEE Floating Point math often has very very small errors so the answer is not exactly what you would get with a pencil and paper. Try one or both of these things:
    Format the IF cell as scientific. is that 0.0 exactly zero? I suspect it will be some very tiny number.
    Type the exact same number into F35 and F29 and see if your IF formula works as you expect.
    You might need to use ROUND on F35, F29 and/or in your IF formula to get rid of the tiny math errors.
    If you search the forum for floating point math or IEEE 754 you should find more than a few posts about Numbers and floating point math. Or you can read about IEEE 754 on Wikipedia.  It trips up a lot of people.
    Here is a common example.  B2 and C2 are from the formulas shown in B1 and C1. They should both be exactly zero, if done with paper and pencil.  Neither is exactly zero (though I have them formatted with 2 decimals so they appear to be 0.00), they are different from each other, and subtracting one from the other is also not exactly zero.

  • Return values and methods repost

    Had to repost for clarity. I'm working on a program which not only could I not compile...and have already been adviced on the forum it's well of the mark. Kindly request a few pointers ( !code ) on where i'm going wrong. I'll put the question as put to me and the code I originally thought was close to archieving th task.
    Here goes,
    The following code fragment is the main method for a simple program. The program reads in the ages of 3 people. Each age is checked for validity. If all ages are valid the program will then calculate the age of the oldest person and the average age(which is to be represented as a double).
    The method names highlighted in bold require implementation(note some method calls are duplicated, but this doesn't mean you have to write the method more than once!). The first takes an integer parameter and returns true or false depending on whetherthe age is valid, i.e. between some sensible limits. The remaining methods both take threeparameters, all intergers. oldest displays the highest age value; average returns the average of the ages.
    public class Tut11_1{
    public static void main(String[] args){
        GUI gui=new GUI();
        int age1=0;
        int age2=0;
        int age3=0;
        double average=0;
        age1=gui.getInt("Enter first age");
        age2=gui.getInt("Enter second age");
        age3=gui.getInt("Enter third age");
        if(!isValidAge(age1))
             gui.putText("Age 1 is not valid");
        else if(!isValidAge(age1))
             gui.putText("Age 2 is not valid");
        else if(!isValidAge(age1))
             gui.putText("Age 3 is not valid");
        else
             oldest(age1, age2, age3);
             average=averageAge(age1, age2, age3);
             gui.putText("Average age is " + average);
        This is where i've got to....
    public class Tut11_1{
    public static void main(String[] args){
        GUI gui=new GUI();
        int age1=0;
        int age2=0;
        int age3=0;
        double average=0;
        age1=gui.getInt("Enter first age");
        age2=gui.getInt("Enter second age");
        age3=gui.getInt("Enter third age");
        if(!isValidAge(age1))
             gui.putText("Age 1 is not valid");
        else if(!isValidAge(age1))
             gui.putText("Age 2 is not valid");
        else if(!isValidAge(age1))
             gui.putText("Age 3 is not valid");
        else
             oldest(age1, age2, age3);
             average=averageAge(age1, age2, age3);
             gui.putText("Average age is " + average);
        //implementing method names
        public static Boolean isValidAge(int oldest, double averageAge, double average)
             if(isValidAge<=0)
                  return false;
             else if(isValidAge<=100)
                  return true;
             else if(isValidAge>100)
                  return false;
             else
                  oldest(age1, age2, age3);
                return oldest;
                average=averageAge(age1, age2, age3);
                return average;
        }

    N_E_W_B_I_E wrote:
    That seem to make sense...will give that a go. On the case of boolean would this be right?
    public static boolean isValidAge(_DONT I HAVE TO DECLARE ANYTHING HERE_ )To be honest with you I am not seeing alot of hope of you completing this assignment without one on one help.
    This is just to give you a push in the right direction. I wouldn't normally do this but it's not all of your assignment and there seems to be a real lack of progress in your two threads thus far....
    public static boolean isValidAge(int age){
      if(age<=0){
        return false;
      return age<100;
    }So that method is now finished. When you call it you can do this...
    int x = 17;//or whatever the user inputs
    boolean validAge = isValidAge(x);
    if(validAge==true){
    }else{
    }The above can be compressed (because an if is a true/false and boolean is a true false) as
    int x = 17;//or whatever the user inputs
    if(isValidAge(x)){
    }else{
    }

  • Incorrect message count in Mail.app inbox (not unread message count)

    In Mail.app my inbox is empty but I have a current count of about 4.3 billion messages (varies some). Does anyone know a fix for this? I have 3 IMAP accounts set up as well as several RSS feeds. This is not a major problem since the new message count is usually correct (except for some minor issues updating the GMail count) but is a nuisance still.

    It is not the answer to "billions", but click on View and see which is offered: Hide Deleted Messages or Show Deleted Messages, and whether shown in gray or black type?
    Perhaps you should use Rebuild on each individual Inbox. If you do, be aware that all messages seem to disappear, before returning, so do not panic and do anything until Rebuild is finished.
    Ernie

  • Ranges declaration for integers "and" characters

    Hi All...
    Is there any way in which we can declare a ranges variable for both integers and characters???
    I want the ranges variable to check in between 10 to 13 and 30 to 33 and A to E.
    I tried as ranges BT 'A' and 'E'. That is, it should only accept values in [A B C D E].
    But the problem is, it is accepting the value [BETER] also.
    How can this be solved.

    That is fine. But the declaration occupies more space than actual code. Readability also counts!
    Any other suggestions?
    Thank u Vivek.

  • Return Item and Free goods check box in purchase order

    Hi Guru:
    I just got the question regarding the return item and free goods check box,my question is that if I check the free good check box,then the price of the matieral will be charged to zero,however,if I want to issue the return purchase order to the vendor,does I need to check the return box first and then issue the return order to the vendor? Then go to MIGO transaction to select the movement type 122 to return the goods,what's the whole precedure for returning the goods to the vendor,can anybody tell me,very appriated!

    Hi,
    Retun to vendor through have two types....
    One is you have Po created, vendor send the item in that few items have rejected so you need to return the item to vendor......
    here in MIGo chose returrn to vendor selce the PO numbere.... system automaticaly take the movent type 122....
    sencond type is Retun PO..... In Po while creting the PO tick the return indication..
    If you do the GR system will pick 161mopvent type.. ( GR returns)...
    REgards
    anand

Maybe you are looking for