query non-unique field

Hello, I'm new to pl sql and I thank you for your time.

I have 3 columns; username (varchar2 (30)), logon_time (date), logoff_time (date)

for example
User1...
User2...
User1...
User2...
util_3...
util_3...

I'm working on a script to calculate the average user session for all users time.

I intend to make use of logon_time and logoff_time to calculate the time of average session for all users.

That's what I have in mind,

1 calculate the elapsed time for each session
2. Summary of all time elapsed / number of sessions

But I can't get my head on the selection of logon_time and logoff_time each line through the names of users that I also need to retrieve variables according to the name of each unique user. I would really appreciate if someone could tell me how I can do this.

Published by: 892209 on October 19, 2011 03:27

Published by: 892209 on October 19, 2011 03:32

Hello

jdude wrote:
through the bad answer function return average

good method is to (sum (logoff_time - logon_time)) / number of sessions

Select user_logon.username,
2 count (*) as numlogins
3 to user_logon
4 user_logon.username group;

It lists the number of connections per user... How can I put this together.

Edited by: jdude on October 19, 2011 20:33

So the formula to use is
(sum (logoff_time - logon_time)) / number of sessions and the number of sessions is the same as count (*) , then
(sum (logoff_time - logon_time)) / count (*) is identical to the formula you want to use.

Note that this account sessions who lack time or logon_time logoff as being 0 hours long. Consider these data:

CREATE TABLE     user_logon
(     username     VARCHAR2 (10)
,     logon_time     DATE
,     logoff_time     DATE
);

INSERT INTO user_logon (username, logon_time, logoff_time)
VALUES      ( 'FOO'
     , TO_DATE ('20-Oct-2011 03:00', 'DD-Mon-YYYY HH24:MI')
     , TO_DATE ('20-Oct-2011 04:00', 'DD-Mon-YYYY HH24:MI')
     );
INSERT INTO user_logon (username, logon_time, logoff_time)
VALUES      ( 'FOO'
     , TO_DATE ('20-Oct-2011 05:00', 'DD-Mon-YYYY HH24:MI')
     , TO_DATE ('20-Oct-2011 06:00', 'DD-Mon-YYYY HH24:MI')
     );

INSERT INTO user_logon (username, logon_time, logoff_time)
VALUES      ( 'BAR'
     , TO_DATE ('20-Oct-2011 03:00', 'DD-Mon-YYYY HH24:MI')
     , TO_DATE ('20-Oct-2011 04:00', 'DD-Mon-YYYY HH24:MI')
     );
INSERT INTO user_logon (username, logon_time, logoff_time)
VALUES      ( 'BAR'
     , TO_DATE ('20-Oct-2011 05:00', 'DD-Mon-YYYY HH24:MI')
     , NULL
     );
COMMIT;

User BAR had two sessions: the first is 1 hour long, and the 2nd is unknown. The 2nd session could not have finished yet, or he might have taken late due to error that caused the logout_time does not have to be registered. It means of BAR, 1 hour, or should we. 5 hours?

SELECT       username
,       24 * AVG (logoff_time - logon_time)     AS avg
,       24 * ( SUM (logoff_time - logon_time)
            / COUNT (*)
            )                    AS sum_cnt
,       24 * AVG ( NVL ( (logoff_time - logon_time)
                   , 0
             )     )               AS avg_nvl
FROM       user_logon
GROUP BY  username
ORDER BY  username
;

Output:

USERNAME          AVG    SUM_CNT    AVG_NVL
---------- ---------- ---------- ----------
BAR                 1         .5         .5
FOO                 1          1          1

Tags: Database

Similar Questions

  • SQL query to get non-unique values

    Hello

    I'm using oracle 11g.

    I have a table (T1) with data below and you want to insert the value in another table (T2).

    CITY            STATE
    --------------- --
    San Francisco   CA
    Palo Alto       CA
    Sarasota        FL
    New York        NY
    Boston          NY
    Boston          CO


    A unique constraint on the city and the State of the columns are given in table 2.


    I want to filter non-unique records before inserting data in table 2 to avoid the UNIQUE constraint exception.


    Thus, in the example above, the query should display subfolders


    Boston          NY
    Boston          CO


    any ideas?


    Thank you

    Hello

    EmAar says:

    Sorry Chris/Frank, I should have been more specific.

    Here is an actual table data.

    description of the category

    Costumes of a1

    Costumes of a1

    Costumes of a1

    A9 crew sweats

    A9 crew sweats

    CK recreation Rep

    CK Rep Tees

    K9 Casual

    Formal K9

    K9 TRAIL RUNNING

    As you can see, the category A1 and A9 are duplicated with the same description. On the other hand, CK & K9 have different descriptions.

    I need a query to display CK & K9 because they have different descriptions against the same category.

    hope that make sense?

    It makes sense, but it's still vague; you really need to post CREATE TABLE and INSERT statements for entry and the exact result wish you get.from of these data.

    My best guess is that you want:

    SELECTED category

    OF THE real

    GROUP BY category

    After HAVING COUNT (DISTINCT description) > 1

    ;

  • Sorting the query according to the unique field data type.

    Hello

    I have a varchar data from the field in a table which cointains 'NumAriques' and 'Alphanumaric '. I need to sort the query using this field.
    While data are numAriques should sort as numAriques else data out as varchar.

    Is it posible in oracle. If so, please help me to get it.

    Hello

    I would do something like this:

    [11.2] Scott @ My11g > !cat t.sql
    with t(n) as (
         select ' 123' from dual
         union all select '123CAD' from dual
         union all select '123TAD' from dual
         union all select '123' from dual
         union all select '1234         ' from dual
         union all select '11111' from dual
         union all select 'zzrytarz' from dual
    )
    ------ end of sample data ------
    select
         n
         -- uncomment 2 following line to ease understanding :
         --,case when regexp_like(trim(n),'^\d+$') then 1 else 2 end
         --,case when regexp_like(trim(n),'^\d+$') then to_char(to_number(n),'fm00000000000000000000') else n end
    from t
    order by
         case when regexp_like(trim(n),'^\d+$') then 1 else 2 end
         ,case when regexp_like(trim(n),'^\d+$') then to_char(to_number(n),'fm00000000000000000000') else n end
    /
    
    [11.2] Scott @ My11g > @t
    
    N
    -------------
     123
    123
    1234
    11111
    123CAD
    123TAD
    zzrytarz
    
    7 rows selected.
    
  • Unique and non-unique indexes


    Hello

    I have a unique index associated with no doubt. Unique and not unique indexes are used B tree architecture. But I want to know if I create a non-unique index on a column that contains unique data.

    When I query this table using this column, it scans each sheet or times found that value it scan stops and give us the result?

    00125 wrote:

    In a non-unique index, scans all the leaves... so what's the difference between full table scan and index ull. I went through a few articles they mentioned that a non-unique index to check with rowid. How she treated? I have clear knowledge in it.

    Please help me with this.

    Thanks in advance

    If you go through architecture index B-tree you could easily understand how indexing works. The picture here shows how a look of column datatype NUMBER indexed as / stored internally. You can imagine that your indexed column TST_COLA this will look like in the internal process. In the B-tree indexes, you have 3 main structures 1. 2 root. Branch 3. Leaves and the database retrieves lines by browsing through the root of index-> branch-> leaves. If the photo if we wanted to retrieve the row whose value indexed column = 25. First data goes to the root and finds that plugs is set to 25, then he Stoops to this particular branch of find what block sheet retains the value 25. More far away after finding the leaves block then goes to this block of sheets and research the special value of 25.

    If the index is UNIQUE, the database knows that there must be only one value, where it performs INDEX UNIQUE SCAN. If the index is NOT UNIQUE it should check all values in this block of leaves to find who all are 25 - in this case INDEX RANGE SCAN is done - as you must check not only value, but all values in this block of sheets - given that the values are not unique. As you can see it that the sheet block contains the long side of the value of the column ROWID, using this database rowid, then goes to the table to retrieve that particular line.

    Full table scan is a method of access where the database just to access the table directly (bypassing index structure) and analyze the ENTIRE table to satisfy the request.

  • Index on non unique values in order to avoid the full table scan

    I have a table with > 100 k records. The table is updated only during the race every night. All columns except one have non - unique values and I am querying the table with this request.

    COL3 - non - unique values - only 40 distinct values
    unique values - no - COL4 - 1000 distinct values
    last_column - 100 k unique values

    Select last_column in the table_name where in (...) col3 or col4 (...)

    I tried to create a Bitmap index individually on col3 and col4 and also combined. However, in both cases, it performs a full table scan.

    Please, help me optimize this query as it is used in the term altogether the system and the cost of the query is very high around 650.

    I don't have much experience with popular indexes then all tracks.

    Thank you
    Sensey

    Published by: user13312817 on November 7, 2011 11:32

    An alternative might be to use a union instead and the 2 index:

    create index my_index1 on my_table (col3, last_column) compress 1;
    create index my_index2 on my_table (col4, last_column) compress 1;

    Select last_column from my_table
    where col3 in (...)
    Union
    Select last_column from my_table
    where col4 (...)

    In other words, if the UNION would apply here whereas in double values for last_column will be deleted.

  • Non editable field change to highlight the problem

    Hi all

    I have an edit non editable field in my screen. In order to make the difference between editable and non editable field, I put the bottom of the field cannot be changed to light gray as in the code below:

       Border nonEditableBorder=BorderFactory.createRoundedBorder(new XYEdges(5, 5, 5, 5),Color.LIGHTGRAY,Border.STYLE_FILLED|Border.STYLE_TRANSPARENT);
    EditField textField=new EditField(EditField.NO_NEWLINE|EditField.FOCUSABLE|DrawStyle.RIGHT);
    textField.setBorder(nonEditableBorder);
    

    Now the field cannot be changed when I scroll the wheel to the right or left the text gets highlighted in white as shown in the image below:

    I don't want the text to highlight. Can anyone help me please with this. Thanks in advance.

    Kind regards

    S.A.Norton Stanley

    Hello

    Thank you. Overridding method drawFocus() and nothing doing, emphasis has been disabled on the fields and the white highlight was not. But once I reached the first or the last field of the white selection screen gets drawn again.  But the border I also assign the background of the field edit as shown below, and this solved my problem.

    Background bgNonEditable = BackgroundFactory.createSolidBackground(Color.LIGHTGRAY);
    textField.setBackground(bgNonEditable);
    
  • When I change a column is a primary key the associated non-unique index to become unique?

    So basically I already tried this and it shows me that the associated index is not unique.

    create table employees2 in select * from employees;

    create index emp_idx on employees2 (employee_id);

    ALTER employees2 table add primary key (employe_id) using index emp_idx;

    Select * from user_indexes where index-name = "EMP_IDX";

    I was wondering if I right assuming that when you change a column to a primary key or unique while using a given index that does not have the respective index become unique.

    The textbooks I use are sometimes a little hard to understand because of the wording, also, I want to just ask someone with a little more experience than me.

    Thank you.

    your test did give the correct answer: the index is not unique if it serves to bear a unique or primary key constraint. Indeed, it is one of the benefits of the use of no unique indexes in support of UK/PK constraints (since it allows to set the unusable index before to make bulk loads; and, of course, they have also some disadvantages - for example, they need an additional logical reading to reach a line). Richard Foote explains the details in https://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ (and other items).

  • sizes of compressed index (unique vs. non-unique)

    Hello

    I wonder if anyone can shed any light on what is happening internally in an index which explains the behavior I see when you do the compression tests. It's all about 11.2.0.3.8 on SLES11.

    I have a simple table with about 12 columns, the initial size is 48 GB, I have that compress with compression OLTP and it comes down to 18 GB - that everything is perfect

    The table has 4 clues in this respect everything I can rebuild with the option 'compress' (i.e. just old style compression - there is no advanced for the index compression) - and I see a strange behavior. I took just a hint an example here:

    It's the creation command to original index - which creates an index of 14 GB

    CREATE INDEXES 'RESET_INDEX' ON 'RESET' ('INS_NUM', 'PARAM_SEQ_NUM', 'PARAM_RESET_HEADER_SEQ_NUM', 'RESET_SEQ_NUM');

    If I run the present

    CREATE INDEXES 'RESET_INDEX' ON THE COMPRESS OF 'RESET' ('PARAM_SEQ_NUM', 'PARAM_RESET_HEADER_SEQ_NUM', 'INS_NUM', 'RESET_SEQ_NUM');

    In fact, the index grows - it becomes 18 GB (because of the many column values that are only 1 or 0 according to me that end by expands when compression is enabled)

    However - and this is the bit that confuses me

    CREATE AN INDEX UNIQUE 'RESET_INDEX' ON THE COMPRESS OF 'RESET' ('PARAM_SEQ_NUM', 'PARAM_RESET_HEADER_SEQ_NUM', 'INS_NUM', 'RESET_SEQ_NUM');

    By adding the 'unique' keyword, index is massively more small-, now only 8 GB!

    What is happening here - when an index is unique fact that somehow ID stored inside it must also be compressed where, in a non-unique index, this is not possible?

    Which seems very strange - but the question - if I add a surrogate key column to a table and add it to all my index finger and make it unique as all my index finger will then get more small when compressed...?

    See you soon,.

    Rich

    The response of a Word is not.

    The explanation lies in what happens in the article published in the 2 'compress' on the unique index producing an error.

    For an index not unique 'compress' with no count of column means 'compress n', where n is the number of columns in the index. for an index unique 'compress' means "compress n - 1".

    Your "change to single" index gets smaller on the compression, because it shows duplicates on the first 3 columns (of 4).

    Concerning

    Jonathan Lewis

  • Accompanied by primary key to a non-unique index?

    Met a weird situation today. A utility, we set up which allows analysts to restore the data in their paintings, started failed when it attempted to drop an index. The index supported a primary key. Makes sense. But our script was supposed to be only an attempt to drop/recreate the nonunique indexes. It turns out that supported on the primary key index was not unique and to the best of my knowledge has emerged as follows:
    SQL> create table junk (f number(1));
    
    Table created.
    
    SQL> create index junk_ix on junk(f);
    
    Index created.
    
    SQL> select UNIQUENESS from DBA_INDEXES where index_name = 'JUNK_IX';
    
    UNIQUENES
    ---------
    NONUNIQUE
    
    SQL> alter table forbesc.junk add constraint junk_pk primary key (f) using index junk_ix;
    
    Table altered.
    
    SQL> select UNIQUENESS from DBA_INDEXES where index_name = 'JUNK_IX';
    
    UNIQUENES
    ---------
    NONUNIQUE
    
    SQL> insert into junk values (1);
    
    1 row created.
    
    SQL> insert into junk values (1);
    insert into junk values (1)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (FORBESC.JUNK_PK) violated
    
    
    SQL> select index_name from dba_constraints where constraint_name = 'JUNK_PK';
    
    INDEX_NAME
    ------------------------------
    JUNK_IX
    What I can't understand, is how an index is not unique is to apply oneness. I thought it was the key to this process. I thought that perhaps an index with the 'SYS_123456' has be created, perhaps, but I couldn't find a:
    SQL> select object_name, object_type from dba_objects order by created desc;
    
    
    OBJECT_NAME   OBJECT_TYPE
    -------------------------------------------     
    JUNK_IX     INDEX
    JUNK     TABLE
    ...
    How the uniqueness is get applied in this case? It comes in Oracle 11.1.0.7

    Thank you
    -= Chuck

    It has always been like that. Oracle can and will use a non-unique index to apply a constraint to PK, the existing index just needs to have the columns PK as the leader or the columns in the index:

    SQL> create table t (id number, id1 number, descr varchar2(10));
    
    Table created.
    
    SQL> create index t_ids on t(id, id1);
    
    Index created.
    
    SQL> select index_name from user_indexes
      2  where table_name = 'T';
    
    INDEX_NAME
    ------------------------------
    T_IDS
    
    SQL> alter table t add constraint t_pk
      2  primary key (id);
    
    Table altered.
    
    SQL> select index_name from user_indexes
      2  where table_name = 'T';
    
    INDEX_NAME
    ------------------------------
    T_IDS
    
    SQL> insert into t values (1, 1, 'One');
    
    1 row created.
    
    SQL> insert into t values (1, 2, 'Two');
    insert into t values (1, 2, 'Two')
    *
    ERROR at line 1:
    ORA-00001: unique constraint (OPS$ORACLE.T_PK) violated
    

    John

  • How to make Non editable fields in Web ADI

    Hi all

    Can you please let me know how we can make the Non editable fields in Web ADI?

    Thank you
    Anil

    Dear Anil

    You define fields don't read that in the page layout.

    Concerning
    Giuseppe

  • When we change the non-mandatory field in the form, it is not updated

    Hello... I have a question here...

    If I required fields in a custom form, and when I ask the form and change the value in the non-mandatory field and click on save, then it does not save. It is said ' no changes to save "." But it is not the same for the non-compulsory fields.

    Can you please suggest me?

    -vrdida

    If you have an element with an assigned LoV it much difference if you change the value of points (assuming that you have either the property 'Validate list', the value yes or you have a WHEN-VALIDATE-POINT-trigger who performs the search ID fk of the given text) or if you visit the 'empty' field, or NULL. In this case, you must create a WHEN-VALIDATE-trigger POINT on the element, which "cleans" the Fk - id as

    IF :BLOCK.LOKUP_ITEM IS NULL THEN
      :BLOCK.FK_ID:=NULL;
    eNd IF;
    
  • non-unique index

    Hello

    Why Oracle adds the rowid of a non-unique index? why each entry in the non-unique index must be unique?

    Thank you

    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:2047869500346039771
    Why a rowid is added to the key for a non-unique index?

    Please read above links. Hope may be useful.

    Concerning
    Girish Sharma

  • validation working is not to count the non-null fields

    I have six fields on the screen, I want to check that at least 3 of them are filled (3)...
    I created a validation as follows to count the non-null fields... but it's not working...
    Please help solve this problem or recommend a better way to do this...


    declare
    number of question_count;
    Start
    question_count: = 0;
    If: p1_t_question_1 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_2 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_3 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_4 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_6 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_6 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;

    If: question_count > 2 then return false;
    otherwise returns true;
    end if;
    end;

    I'll take a look a bit.
    In the meantime, check if the code as

    If the trim (both '%' from: p1_question_with_nul') = 'null '.

    help

    Ok. Take a look now. I used the function TRIM as shown above and also changed the comparison for questions 3 text based on ' if: p1_question_with_text is null'

    Seems to work now

    CITY

    Published by: city has 23 October 2009 05:26

  • UNIQUE compared to a NON-UNIQUE INDEX

    Hello SQL gurus.

    Can you tell me the difference between UNIQUE and NON-UNIQUE index?

    Thank you
    Dave

    The fact that a UNIQUE index will not allow duplicate values in the columns in Index key so that NO SINGLE index will allow duplicates.

    If I CREATE an INDEX UNIQUE MY_TABLE_UK ON MY_TABLE (NAME_COLUMN);

    I can't insert the value "HEMANT" twice in the NAME_COLUMN.

    If I CREATE INDEX MY_TABLE_IDX ON MY_TABLE (NAME_COLUMN);

    I can "HEMANT" insert multiple lines.

    NOTE: If I insert NULL values, a UNIQUE INDEX will allow several NULL values as, by definition "NULL! = NULL ". However, a definition of primary key does not allow nulls.

  • Click on the link inside the non-Focus field

    Hi all

    I use an ActiveRichTextField non-focusable because the field is part of a larger container making screenshots click on itself - not the children. This was done to make it easier for the user to scroll up and down on a list of text fields (do not have to move the focus to a line-of-text database).

    Question:

    My customer wants to activate by clicking on hyperlink. This is done natively by the ActiveRichTextField, but it cannot be activated because the emphasis is disabled.

    Question:

    Someone has any idea how to reconcile the conflicting requirements?

    (a) the ActiveRichTextField should not be focusable on a baseline of text (if it is wrapped, of course)

    (b) by clicking on the hyperlink should open the browser, etc.

    Thank you.

    You can get the x / y position of the textfield, but translate the active regions in x / there would be rather difficult (if possible at all).
    I just looked at the API, but I don't see how you can extract the links (or information about them) as the analysis is done in the background.

    You can analyze the text yourself with regular expressions to detect hyperlinks and offer them in a context menu.
    (e.g. http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/ )

    I usually change my paradigma UI if it turns out that a usecase I need is not taken in charge, but it is sometimes easier to patch something on.

    Basically, your decision: change the user interface in order to support this usecase, or something (like the context menu) on this patch.

    I would say to list the options see, then speak to the customer what he likes.

Maybe you are looking for