Select count (1) vs count (*) select

I apologize if this has been requested previously. Can someone tell me what is the difference between
 select count(1) from <mytable> 
as opposed to
  select count (*) from <mytable> 
?

Thank you

Select count (1) in the table

is faster than

Select count (*) in the table

but as long as it is to write the statement - reason: you don't have to hit the shift key for writing a 1, while you need the SHIFT key to get a * ;-)

Tags: Database

Similar Questions

  • What is the difference between count (1) selection of the tab and select count (*) tab;

    What is the difference between count (1) selection of the tab and select count (*) tab;

    994122 wrote:

    Hello

    SQL > set timing on

    SQL > select count (*) of the emp

    2 where deptno = 30;

    COUNT (*)

    ---------------

    6

    Elapsed time: 00:00:00.01

    SQL > select count (1) of the emp

    2 * where deptno = 30

    SQL > /.

    COUNT (1)

    -------------

    6

    Elapsed time: 00:00:00.01

    See that both give the same time

    Do you really think the 100th's of a second it takes 6 rows from counting will truly representative of the difference it takes actually?

    Such a small data set cannot possibly show no difference to these larger units of time (in the calculation of terms where we have millions of instructions per second in lieu)

    As the link to the FAQ provided in the response of Anton (first response on this thread) Watch, count (1) and count (*) are essentially the same, except that count (1) actually get re-written by the query re - write the step of the optimization to make it count (*).

    Generally 'count (*)' makes more sense language, because it reads like "count" while "count (1)" reads as "count we ', which does not have as much good sense when you actually count all.

  • How COUNT (*) SELECT all first before returning SELECT *.

    Our interface involves a grid filled by a SELECT query. The records are retrieved via a complex query with many tables involved and spent many filtering parameters. A simplified example:

    SELECT col1, col2,... colx

    OF tab1 tab2, tab 3 t3 t2, t1, etc.

    WHERE

    T1.Key1 = t2.key1

    and t2.key2 = t3.key2,

    and... etc.

    and t1.coldate > p_FilterDateFrom

    and t1.coldate < p_FilterDateTo

    and t2.somefield = p_FilterSomeFilter

    and t3.someotherfield = p_FilterSomeOtherField

    and... etc.

    Generally, the user enters parameters, so a small, manageable resultset is returned.

    But sometimes the user will enter the parameters that returns thousands of lines. As a result, the frontend in IE makes a break from time to time say ' Stop running this script? A script on this page slows down your web browser to run slowly... »

    I would like to run the above example query with a COUNT (*) SELECT all first before running the actual SELECT col1, col2, col3. This way if the rows returned exceeds a certain threshold, say, 500 lines, I could pop a message saying "more than 500 rows will be returned. Do you want to continue? ». If the user clicks Yes, then I go ahead and perform the actual query and fill the grid. If the user clicks on no, then the user gets a chance to adjust its settings.

    Y at - it an easy way to do short of coding (essentially) two versions of the same complex query, one for "SELECT COUNT (*)" and the other for "SELECT?"

    Thank you.

    Hey, Justin (or someone else). I quite agree with you on this... However, just to play the lawyer of the devil for a second... (or maybe not... so why I asked)

    The COUNT (*) Analytics as a separate and additional column has been added to the query would be a significant hit to the performance? I have simply discarded with a little, but set of samples was not big enough - I'll try a larger, later when I get time.

    something like:

    1. SELECT *.
    2. DE)
    3. SELECT col1, col2,... colx, count (*) NTC)
    4. OF tab1 tab2, tab 3 t3 t2, t1, etc.
    5. WHERE
    6. T1.Key1 = t2.key1
    7. and t2.key2 = t3.key2,
    8. and... etc.
    9. and t1.coldate > p_FilterDateFrom
    10. and t1.coldate<>
    11. and t2.somefield = p_FilterSomeFilter
    12. and t3.someotherfield = p_FilterSomeOtherField
    13. and... etc.
    14. order of something)
    15. where rownum<=>

    So, you can then enter the total number of any line (probably the first) and know how much you have?

    I'm sure it's more work than simply without... However, I guess that the real question... How much better it is than to the separate account? (if applicable)

  • Count the items in the drop-down list by repeating the subform if the user selects "S/o".

    I'm counting the number of drop-down lists the user selected "N/A" in a repeating subform. I want the total appear in a numericfield or textfield. Here is my script that unfortunitly does not work and my page layout form. I placed it in the ready event: layout so the total is updating the number of changes of N/A.

    Hierarchy.GIFScript.GIF

    Hello

    I put the code in the text box, something like calculate event;

    var result = 0;

    instanceCount var = _Subform2.count;

    var instance = Form1.resolveNodes ("Subform2 [*]");

    for (var i = 0; i)< instancecount;="">

    {

    If (instances.item (i). Table1.row2.score.RawValue == 'N/a') {}

    result ++;

    }

    }

    this.rawValue = result;

    The reference to the "_Subform2.count" will ensure that the code is executed whenever an instance is added or deleted.

    Concerning

    Bruce

  • Scripting: Count the number of selected pixels (non-quadrangle) or alternative approach

    Is it possible to count the number of pixels in a selection that is not a quadrilateral? For example, if I have cntl - click on the thumbnail of a text layer, I would like to know how many pixels is selected.

    This application, if anyone has another approach to try...

    Untitled-1.jpg

    My goal is the place the text at some point where it covers do not all lines (or, Alternatively, the place where it covers the smallest number of pixels line). My approach is to insert a text in red to the desired location, apply a BlendMode.Multiply to the text. The number of Red pixels always on the screen will show how many pixels line I'm concealing. Then I move the text into a region and check the histogram in 76 index (which is 255,0,0), find the point with the highest number of red (meaning concealing pixels less line) and finally move the text at that location. See the code snippet below:

    var bestCX = cX;

    var bestCY = cY;

    var bestPixelDensity = - 1;

    var pixelDensity = 0;

    var searchRegionWidth = 3000;

    var searchRegionHeight = 2000;

    loop through an area 3000 x 2000 around where you want

    for (var x = cX - searchRegionWidth/2; x < cX + searchRegionWidth/2; x ++)

    {

    for (var y = cY - searchRegionHeight/2; y < cY + (searchRegionHeight/2); y ++)

    {

    textItemRef.position = new Array (x, y);

    pixelDensity = myDocument.histogram [76]; index of 255,0,0 (red)

    If this point is greater than the previous highest density, use it instead.

    if(pixelDensity > bestPixelDensity)

    {

    bestCX = x;

    bestCY = y;

    bestPixelDensity = pixelDensity;

    }

    }

    }

    move text in the best location found

    textItemRef.position = new Array (bestCX, bestCY);

    The problem is that the images I use are very large ((7000x7000px) for each histogram control takes a few seconds and I really need to move in a fairly large region (3000 x 2000) to find the best place.) I think that it is perhaps faster if I'm not using the histogram at all, and instead each time that I move the text, I select the text and see how many points is in the selection. Any help or alternative approaches would be great!

    I don't know the answer, the only thing I can think is the whole of the histogram to the selection and treatment of the histogram of the selected area.

  • SELECT and COUNT() combinations

    I have an array of PROJECTS and the two fields A and B values. Common has; "xxxxxxx0101", "xxxxxxx0102", "xxxxxxx0103". There are almost always 37 different combinations, where the 8th and 9th character is '0' and '1' - but not always. How can I count the number of combinations in my query?

    I use suite SQL, but it takes the number of combinations to be 37.
    SELECT
         SUBSTR(A, 8, 2),
         SUBSTR(A, 10, 2),
         37
    FROM
         PROJECTS
    WHERE
         B = '3213213'
    GROUP BY
         A
    ORDER BY
         A ASC
    Published by: 964656 on 2012-10-11 00:46

    Try this

    not tested

    select a,count(*) over(partition by substr(a,1,9))
    from table_name
    
  • Select and count

    Hi friends...

    I have 2 tables A and B


    Table A
    ----------------------------------
    ID | NAME
    -----------------------------------
    1. M

    2. N

    3. O

    4. P



    Table B
    ----------------------------------------------------------------------------------------------------
    COVER | NAME | PARENT
    -----------------------------------------------------------------------------------------------------
    10. N | M

    11. N | O

    12. P | N

    13. O | M


    My requirement is to count the number of entries for which Parent is M directly or indirectly

    So, the result should be as

    PARENT COUNTY
    3 M
    O 2
    P 0

    I tried to use connect by but I can't do it.

    Can you please help me with it?

    Thank you

    Hello

    user10819722 wrote:
    ... So, the result should be as

    PARENT COUNTY
    3 M
    O 2
    P 0

    Shoudn't the results also include this line?

    parent     count
    ------     -----
    N     1
    

    I tried to use connect by but I can't do it.

    CONNECT is the right tool for the job. Here's a way to do it:

    WITH     got_ancestors     AS
    (
         SELECT       CONNECT_BY_ROOT parent     AS ancestor
         ,       name                    AS descnedant
         FROM       b
         CONNECT BY     parent     = PRIOR name
    )
    SELECT       a.name
    ,       COUNT (DISTINCT g.descendant)     AS total_descendants
    FROM           a
    LEFT OUTER JOIN  got_ancestors  g  ON  a.name  = g.ancestor
    GROUP BY  a.name
    ORDER BY  a.name
    ;
    

    If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.

    This assumes that tale contains a one line per name. There may be names that never appear in table b at all; they will have 0 counties, of course.

  • Count the number of values selected from a list

    I'm trying to add a counter to a form that tells the user how many values have been selected in a list box. I tried to add a FormCalc formula for an event to calculate the area of "meter" as follows:

    County (form1. #subform [0].) ListBox1.rawValue)

    But for some reason it add up their. It doesn't give me the value 0 if none are selected, or 1 where any number of selections is made.

    Can someone help me?

    Thanks in advance

    Try to put this in the case of calculation of your counter field:

    var num = 0;

    for (var a = 0;<>

    If (ListBox1.getItemState (a))

    NUM ++;

    }

    this.rawValue = num;

    Leave the list box and will calculate the field.

    Kyle

  • After select stmt. why the count value of variable column, nested table is zero.

    CREATE OR REPLACE TYPE numbers_type
    ARRAY OF INTEGER
    /
    CREATE OR REPLACE PROCEDURE p(i) in FULL IS
    numbers1 numbers_type: = numbers_type (1,2,3,4,5);
    BEGIN
    DBMS_OUTPUT. Put_line ("exposed before SELECTING");
    DBMS_OUTPUT. Put_line('numbers1.) COUNT() = ' | numbers1. COUNT());
    FOR j IN 1.numbers1. COUNT() LOOP
    DBMS_OUTPUT. Put_line ('numbers1 (' | j |)) ') = ' || numbers1 (j));
    END LOOP;
    -Auto selection clause to COLLECT LOOSE:
    SELECT a.COLUMN_VALUE
    LOOSE COLLECTION numbers1
    TABLE (numbers1) one
    WHERE a.COLUMN_VALUE > p.i
    ORDER BY a.COLUMN_VALUE;
    DBMS_OUTPUT. Put_line ("presentation and SELECT '");
    DBMS_OUTPUT. Put_line('numbers1.) COUNT() = ' | numbers1. COUNT());
    END p;
    /
    Call p:
    BEGIN
    p (2);
    END;
    /

    Hello

    Before taking in the collection variable, the values of the variables are fushed. So in this case the numbers1 is empty so it becomes zero and trying to read values from this collection which is already empty.

    That's why you get the number zero

    PRABHU

  • Get the select count (*) including zero '0 '.

    Hi gurus,

    I have the problem here, I have this request:

    SELECT COUNT (t.column1) AS mycount.
    To_char(t.CREATION_DATE,'MONTH') AS themonth
    Table 1 t
    WHERE the t.creation_date BETWEEN TO_DATE('01/01/2011') AND TO_DATE('09/07/2011') AND t.column1 > 0
    GROUP OF TO_CHAR (t.creation_date, 'MONTH')
    ORDER BY TO_DATE (TO_CHAR (t.creation_date, 'MONTH'), 'MONTH')

    then I get this query result


    MYCOUNT. THEMONTH
    -------------- ----------------
    JANUARY | 3
    MARCH | 6
    APRIL | 5
    CAN | 9
    JULY | 2


    and I need like this
    MYCOUNT. THEMONTH
    -------------- ----------------
    JANUARY | 3
    FEBRUARY | 0
    MARCH | 6
    APRIL | 5
    CAN | 9
    JUNE | 0
    JULY | 2

    what I am doing wrong? or what I can add to the code? Please, I hope you can help with this one.


    Best regards
    Mentor

    Mentor wrote:

    Hey and when I don't have a date range and did well id how can I get the result?

    SELECT  NVL(COUNT(column1),0) mycount,
            TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth
      FROM  (
              SELECT  column1,
                      creation_date
                FROM  table1
                WHERE column_id IN (2,3,12,67,49,182)
                  AND column1 > 0
             UNION ALL
              SELECT  NULL column1,
                      ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date
                FROM  (
                       SELECT  TRUNC(MIN(creation_date),'MM') min_creation_date,
                               TRUNC(MAX(creation_date),'MM') max_creation_date
                         FROM  table1
                         WHERE column_id IN (2,3,12,67,49,182)
                           AND column1 > 0
                      )
                CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date)
            )
      GROUP BY TRUNC(creation_date,'MM')
      ORDER BY TRUNC(creation_date,'MM')
    / 
    

    >

    and another question, how can I get the two counts of the same column where the condition its diferent?

    SELECT  NVL(COUNT(CASE column2 WHEN 'JOHN' THEN 1 END),0) count_a,
            NVL(COUNT(CASE column2 WHEN 'MICHAEL' THEN 1 END),0) count_b,
            TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth
      FROM  (
              SELECT  column2,
                      creation_date
                FROM  table1
                WHERE column2 IN ('JOHN','MICHAEL')
             UNION ALL
              SELECT  NULL column2,
                      ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date
                FROM  (
                       SELECT  TRUNC(MIN(creation_date),'MM') min_creation_date,
                               TRUNC(MAX(creation_date),'MM') max_creation_date
                         FROM  table1
                         WHERE column2 IN ('JOHN','MICHAEL')
                      )
                CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date)
            )
      GROUP BY TRUNC(creation_date,'MM')
      ORDER BY TRUNC(creation_date,'MM')
    / 
    

    SY.

  • By selecting a row if count is greater than 4

    Hello

    I use Oracle 10.2.0.4.0 version
    CREATE TABLE DMM.QUES
    (
      CASENAME        VARCHAR2(100 BYTE),
      CASENUMBER      VARCHAR2(20 BYTE),
      COVERAGE        VARCHAR2(10 BYTE),
      DIVISIONNUMBER  VARCHAR2(6 BYTE),
      CLASSNUMBER     number,
      TIER            VARCHAR2(2 BYTE),
      MONTHLYRATE     NUMBER
      )
    INSERT orders
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','health', '456J','123',null,.25)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','dental', '456J','123','ee',.45)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','dental', '456J','123','es',.45)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','dental', '456J','123','ec',.46)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','dental', '456J','123','fa',.49)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','life', '456J','123',null,.49)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','life', '456J','123',null,.49)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','life', '456J','123','cc',.34)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','life', '456J','123','dd',.49)
    insert into QUES (CASENAME,CASENUMBER,  COVERAGE,  DIVISIONNUMBER,  CLASSNUMBER,  TIER,MONTHLYRATE)
    values ('ABC','123J','life', '456J','123',null,.55)
    Select * from c
    ABC     123J     health     456J     123          0.25
    ABC     123J     dental     456J     123     ee     0.45
    ABC     123J     dental     456J     123     es     0.45
    ABC     123J     dental     456J     123     ec     0.46
    ABC     123J     dental     456J     123     fa     0.49
    ABC     123J     life     456J     123          0.49
    ABC     123J     life     456J     123          0.49
    ABC     123J     life     456J     123     cc     0.34
    ABC     123J     life     456J     123     dd     0.49
    ABC     123J     life     456J     123          0.55
    Power required
    ABC     123J     health     456J     123          0.25
    ABC     123J     dental     456J     123     ee     0.45
    ABC     123J     dental     456J     123     es     0.45
    ABC     123J     dental     456J     123     ec     0.46
    ABC     123J     dental     456J     123     fa     0.49
    ABC     123J     life     456J     123          0.49
    for a combination of classnumber and coverage, if count is greater than 4 then I must return only one record.
    in the example above 123/Life, we have a number of 5. so I need to show that a single folder and can be any folder.

    Please let me know where I can read on other similar examples.

    Thanks in advance.

    This:?

    SQL> select casename, casenumber, coverage, divisionnumber, classnumber, tier, monthlyrate
      from (select q.*,
                   count (*) over (partition by classnumber, coverage) cnt,
                   row_number () over (partition by classnumber, coverage order by 1) rn
              from ques q)
     where cnt <= 4
        or (cnt > 4
        and rn = 1)
    /
    CASENAME             CASENUMBER                     COVERAGE        DIVISIONNUMBER CLASSNUMBER TIER MONTHLYRATE
    -------------------- ------------------------------ --------------- -------------- ----------- ---- -----------
    ABC                  123J                           dental          456J                   123 fa           .49
    ABC                  123J                           dental          456J                   123 ec           .46
    ABC                  123J                           dental          456J                   123 es           .45
    ABC                  123J                           dental          456J                   123 ee           .45
    ABC                  123J                           health          456J                   123              .25
    ABC                  123J                           life            456J                   123              .55
    
    6 rows selected.
    
  • Select Count

    My query:

    SELECT distinct ap.project_files_location, ddh.file_name, dur.*
    Of admin_project ap
    INNER JOIN hard dmand_user_report on dur.admin_project_oid = ap.admin_project_oid
    INNER JOIN dmand_docsys_hier on ddh.admin_report_oid = dur.admin_report_oid ddh
    WHERE ddh.file_name = '_TESTFILE.pdf' and ap.project_files_location = '_Monthly_Reports\\' and dur.admin_user_oid = 983


    Product 5 lines. I want to choose the number of rows produced by this query in a variable. How do you do that?
    Thank you

    Well, there are many possible solutions...

    The simplest one:
    If you already know that the query returns 5 lines then you can:

      myvar := 5;
    

    You can choose the number in a variable:

      select count(*)
      into myvar
      from
    (
    SELECT distinct ap.project_files_location, ddh.file_name, dur.*
    FROM admin_project ap
    INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
    INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
    WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
    " and dur.admin_user_oid=983
    )
    

    You can have a variable from PL/SQL table and bulk get all inside recods. Then you can check the number of records in the table using PL/SQL. COUNTING method:

    SELECT distinct ap.project_files_location, ddh.file_name, dur.*
    BULK COLLECT INTO mytabofrecords
    FROM admin_project ap
    INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
    INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
    WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
    " and dur.admin_user_oid=983;
    
    myvar := mytabofrecords.COUNT;
    

    You can also use "implicit cursor attribute" SQL ROWCOUNT % as in the example below:

    INSERT INTO mynewtable
    SELECT distinct ap.project_files_location, ddh.file_name, dur.*
    FROM admin_project ap
    INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
    INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
    WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
    " and dur.admin_user_oid=983;
    
    myvar := SQL%ROWCOUNT;
    

    Need more options?

  • selection of special days and counting

    Hi all
    I need to select certain dates and the following days with the same criteria.
    An example would be:

    CREATE TABLE DEMO
    (
    EMP_ID NUMBER NOT NULL,
    WORKING_DAY DATE NOT NULL,
    ACTIVE_CODE NULL VARCHAR2 (1).
    CONSTRAINT DEMO_PK
    KEY ELEMENTARY SCHOOL (EMP_ID, WORKING_DAY)
    USING INDEX
    )


    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('01.11.2010 00:00:00 '), '_');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('02.11.2010 00:00:00 '), '_');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('03.11.2010 00:00:00 '), 'K');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('04.11.2010 00:00:00 '), 'K');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('05.11.2010 00:00:00 '), 'K');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('06.11.2010 00:00:00 '), NULL);
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('07.11.2010 00:00:00 '), NULL);
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('08.11.2010 00:00:00 '), 'K');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('09.11.2010 00:00:00 '), '_');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('10.11.2010 00:00:00 '), 'K');
    INSERT INTO DEMO (EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES (123, TO_DATE ('11.11.2010 00:00:00 '), '_');

    Now, I'm looking for the first day with Active_Code = 'K' and count the days that have 'K', too.
    Expected result: November 3-> 4 Ks (because the 8 is after the weekend); 10 November-> 1 K

    I hope that the example/explanation is not too bad...

    I built a procedure which was to count the occurrences, if I give the first occurrence (here the 3rd and 8th).
    But findign the first 'K' is my problem.
    A solution would be:
    AND
    (SELECT T2. T2 DEMO ACTIVE_CODE
    WHERE T2. EMP_ID = T1. EMP_ID AND T2. WORKING_DAY = T1. WORKING_DAY-1) <>'K '.
    AND
    (SELECT T2. T2 DEMO ACTIVE_CODE
    WHERE T2. EMP_ID = T1. EMP_ID AND T2. WORKING_DAY = T1. WORKING_DAY-2) <>'K '.
    AND
    (SELECT T2. T2 DEMO ACTIVE_CODE
    WHERE T2. EMP_ID = T1. EMP_ID AND T2. WORKING_DAY = T1. WORKING_DAY-3) <>'K '.

    But it is pretty darn imperformant if I'm dealing with millions of records :-(

    Could someone please give me a hint on how to solve this problem?
    Thank you.

    Best regards
    Christian

    Hi, Christian,.

    Assumiung you have exactly one line for each working_day (where active_code is not NULL):

    WITH     got_grp_num     AS
    (
         SELECT     emp_id
         ,     working_day
         ,     active_code
         ,       ROW_NUMBER ()
                      OVER ( PARTITION BY  emp_id
                          ORDER BY          working_day
                        )
               - COUNT ( CASE
                       WHEN  active_code = 'K'
                       THEN  1
                   END
                    )     OVER ( PARTITION BY  emp_id
                          ORDER BY          working_day
                        )      AS grp_num
         FROM     demo
         WHERE     active_code     IS NOT NULL
    --     AND     ...     -- Any filtering goes here
    )
    SELECT       emp_id
    ,       MIN (working_day)     AS first_day
    ,       COUNT (*)          AS cnt
    FROM       got_grp_num
    WHERE       active_code     = 'K'
    GROUP BY  emp_id
    ,       grp_num
    ORDER BY  emp_id
    ,         first_day
    ;
    

    I'm guessing on the emp_id role in this problem.

    The trickiest part of this problem is to obtain an appropriate value for the GROUP BY.
    When you have several consecutive lines of 'K', the difference between the ROW_NUMBER (in cash lines no 'K', but not weekends) and the number of lines of 'K' previous will be constant. This is what grp_num is. (A non - adjacent lines 'K' also has the same grp_num, but that's OK, it will be rejected by the WHERE clause in the main query.)

    Published by: Frank Kulash, November 4, 2010 05:43

  • Set a variable to Select count (*)

    Hello

    I have the following statement that produce a result of '8' in Oracle SQL Developer.

    SELECT count (*)
    LIB_ISBN_T p,
    Table (XMLSequence)
    extract (p.ISBN_PAYLOAD,
    ((("/ ISBNdb/BookList/BookData/subjects/subject")));

    How can I get this value in a numeric variable so that I can make some statements about the variable If?

    I am using Oracle 10 g and APEX 4

    Thanks a lot for your help.

    Published by: PhilMan2 on November 29, 2010 16:34

    I learned that I had to use the INTO clause.

  • Pl/sql script and select count()

    Hello!
    Is it possible somehow to put the result of (Select Select) count (h.id) in the "utl_file.put_line" (?)
    in this script, plsql?


    declare
    cursor kursori is
    SELECT count (h.id), n.state
    OF popperson h, popnationality n
    WHERE n.state > 3 and (n.state < 246) or n.state > 246
    and h.registerstate = 1 and h.id = n.state group n.poppersonid;
    Tietue kursori % ROWTYPE;
    tiedosto utl_file.file_type;
    Start
    tiedosto: = utl_file.fopen (' TEST_OUTPUTDIR ',' test.txt ', 'W');
    Open kursori;
    loop
    extract the kursori in tietue;
    When the output kursori % NOTFOUND;
    () UTL_FILE.put_line
    tiedosto, 'Code '. Tietue. State | ' ' || Tietue. ?) ;
    end loop;
    close kursori;
    UTL_FILE.fclose (tiedosto);
    end;
    /

    Thank you! OV
    declare
    cursor kursori is
    SELECT count(h.id) cnt, n.state
    FROM popperson h, popnationality n
    WHERE n.state > 3 and (n.state < 246 or n.state > 246)
    and h.registerstate=1 and h.id=n.poppersonid Group by n.state;
    tietue kursori%ROWTYPE;
    tiedosto utl_file.file_type;
    begin
    tiedosto := utl_file.fopen ('TEST_OUTPUTDIR','test.txt', 'W');
    open kursori;
    loop
    fetch kursori into tietue;
    exit when kursori%NOTFOUND;
    utl_file.put_line (
    tiedosto, 'Code ' || tietue.state || ' ' || tietue.cnt);
    end loop;
    close kursori;
    utl_file.fclose(tiedosto);
    end;
    /
    

    p.s: not tested.

    Concerning

    REDA

  • Create table from a credit count Select

    Hello Folks, all the ideas here, I need to create a table using the below "Select" as a criterion.

    I have the following table and its duplicate records, identified by double Unique_seq has. I want to create a table of these somewhere else where, then delete them from schema.tablename.

    Schmea.TableName
    -------------------------
    Field1
    Field2
    field3
    Field4
    Unique_Seq


    SELECT Unique_Seq, COUNT (Unique_Seq)
    Starting FROM Schema.Tablename
    Unique_Seq GROUP
    SEEN (COUNT (Unique_Seq) > 1

    I want to create the table using all fields from schema.tablename

    Thank you

    What is then?

    New table to contain the duplicate data, pushing ad_id null if exist for the same unique_seq

    CREATE Table tester_c_dup as SELECT ad_id,cd_id,unique_seq,z_master_key
     FROM (select a.*,row_number() over (partition by unique_seq order by unique_seq,ad_id) rn from tester_c A) temp
    where temp.rn != 1
    /
    
    select * from tester_c_dup;
    
    AD_ID                                    CD_ID                               UNIQUE_SEQ Z_MASTER_KEY
    ---------------------------------------- ----------------------------------- ---------- -------------------------
                                             KL1923836                                65478 BG7892534KL1923836
                                             OP0021889/10                            529832 NNCC0049136225-JAN-10
                                             OP0022096/10                            539481 NNCC0060037812-JAN-10
    
    Elapsed: 00:00:00.01
    

    eliminating duplicates in the source table and also focus on null ad_id while eliminating duplicates.

    delete from tester_c A
    where a.rowid in
    (select rowid from (select a.rowid,row_number() over (partition by unique_seq order by unique_seq,ad_id) rn from tester_c A) temp
    where temp.rn != 1)
    /
    
    select * from tester_c;
    
    AD_ID                                    CD_ID                               UNIQUE_SEQ Z_MASTER_KEY
    ---------------------------------------- ----------------------------------- ---------- --------------------------------------
    OP0021889/10                             OP0021889/10                            529832 NNCC00491362OP0021889/1025-JAN-10
    OP0022096/10                             OP0022096/10                            539481 NNCC00600378OP0022096/1012-JAN-10
    LI0192                                   IK735241                                 53925 PP54TH34120988
                                             KL1923836                                65478 BG7892534KL1923836
    PQ8712346                                BN27345236                               64877 7234723642
    
    Elapsed: 00:00:00.00
    

Maybe you are looking for