Output count of SELECT statement is diff from count(*) of the same query

Has this ever happened to you?
I was wondering why some of the items were not appearing in my output. I have this select query:
    SELECT
        bukrs
        gjahr
        hkont
        belnr
        wrbtr
        dmbtr
        matnr
        bschl
        kunnr
        vbeln
        shkzg
    FROM bseg
    INTO CORRESPONDING FIELDS OF TABLE i_output
    FOR ALL ENTRIES IN i_bkpf
    WHERE
        bukrs EQ i_bkpf-bukrs
        AND belnr EQ i_bkpf-belnr
        AND gjahr EQ i_bkpf-gjahr
        AND hkont IN hkont
        AND hkont IN
            ('0004000000',
            '0004000010',
            '0004000020',
            '0004000030',
            '0004000040')
When I tried to put sample filters (bukrs: 1000; belnr 1800000016; gjahr: 2005; hkont: 4000000), I am getting 3 entries from bseg. But when I tried using SE16 or SE16N, I am getting 6 (which is the expected output). When I modify the same SELECT query to return just the output count, I AM getting 6.
    SELECT count(*)
    FROM bseg
    INTO count
    FOR ALL ENTRIES IN i_bkpf
    WHERE
        bukrs EQ i_bkpf-bukrs
        AND belnr EQ i_bkpf-belnr
        AND gjahr EQ i_bkpf-gjahr
        AND hkont IN hkont
        AND hkont IN
            ('0004000000',
            '0004000010',
            '0004000020',
            '0004000030',
            '0004000040')
Do you have any idea why this happens? My internal table is a standard table with no header line and no OCCURS statement.
Kyle

Hello Kyle,
Thats because when using FOR ALL ENTRIES, it will delete the duplicates if the records selected dont have all the primary keys specified in the select query. In your first select query, the field BUZEI is missing which makes the records unique. Change your first select query as this and try
SELECT
        bukrs
        gjahr
        hkont
        belnr
        buzei   -------> Add this and check
        wrbtr
        dmbtr
        matnr
        bschl
        kunnr
        vbeln
        shkzg
    FROM bseg
    INTO CORRESPONDING FIELDS OF TABLE i_output
    FOR ALL ENTRIES IN i_bkpf
    WHERE
        bukrs EQ i_bkpf-bukrs
        AND belnr EQ i_bkpf-belnr
        AND gjahr EQ i_bkpf-gjahr
        AND hkont IN hkont
        AND hkont IN
            ('0004000000',
            '0004000010',
            '0004000020',
            '0004000030',
            '0004000040')
Vikranth

Similar Messages

  • Write a SELECT statement in different ways is not the same thing!!!

    Let's take a look
    use AdventureWorks2012
    go
    dbcc freeproccache
    go
    and now select something
    select * from [HumanResources].[vEmployeeDepartmentHistory]
    go
    so, analyzing 'Compiled Plan'
    SELECT usecounts, cacheobjtype, objtype, text
    FROM Sys.dm_exec_cached_plans
    CROSS APPLY sys.dm_exec_sql_text(plan_handle)
    where cacheobjtype = 'Compiled Plan'
    ORDER BY usecounts DESC;
    GO
    and now let's write some SELECT in "fuzzy case"
    dbcc freeproccache
    go
    select * from [HUMANResources].[vEmployeeDepartmentHistory]
    go
    select * from [HumanRESOURCES].[vEmployeeDepartmentHistory]
    go
    select * from [HumanResources].[VEmployeeDepartmentHistory]
    go
    select * from [HumanResources].[vEmployeeDEPARTMENTHistory]
    go
    select * from [HumanResources].[vEmployeeDepartmentHISTORY]
    go
    SELECT * from [HumanResources].[vEmployeeDepartmentHistory]
    go
    select * FROM [HumanResources].[vEmployeeDepartmentHistory]
    go
    and now let's see compiled plans again
    SELECT usecounts, cacheobjtype, objtype, text
    FROM Sys.dm_exec_cached_plans
    CROSS APPLY sys.dm_exec_sql_text(plan_handle)
    where cacheobjtype = 'Compiled Plan'
    ORDER BY usecounts DESC;
    GO
    So, write a SELECT statement in different ways is not the same thing!!!
    Hope it'll be useful
    P.Ceglie

    Yes.  There is little or no query normalization that happens in front of the query plan cache.  It would be a performance tradeoff to parse and normalize the query before matching it to the cached plans.  The benefit of reducing the number
    of plans in the cache probably wouldn't be worth it. 
    David
    David http://blogs.msdn.com/b/dbrowne/

  • How can I bring the recordset count inside the same recordset?

    I have a query inside a procedure where I'm paging the results.
    I dont know how to bring the total results inside the recordset.
    How can I bring the recordset count inside the same recordset?
    SELECT *
    FROM ( select a.*, rownum rnum
         FROM (SELECT
              FROM table
              ORDER BY id DESC) a
         WHERE rownum <= pg*50 )
    WHERE rnum > (pg-1)*50

    My query is much more comples, so that is why I'm using the other script.
    When I included the "COUNT(*) OVER() tot_cnt AS total" it didn't worked:
    ORA-00904: "TOT_CNT": invalid identifier
    What is the tot_cnt?
    SELECT *
    FROM (SELECT a.*, rownum rnum
    FROM (SELECT
    ed_fotos.id AS COD_FOTO,
    ed_fotos.arquivo AS NOM_ARQUIVO,
    ed_areas.nome AS NOM_REDACAO,
    COUNT(*) OVER() tot_cnt AS total
    FROM edt.ed_fotos ed_fotos
    INNER JOIN edt.ed_volumes ed_volumes ON ed_volumes.id = ed_fotos.volume_id
    INNER JOIN edt.ed_caminhos path_alta ON path_alta.id = ed_volumes.caminho1
    INNER JOIN edt.ed_caminhos path_baixa ON path_baixa.id = ed_volumes.caminho2
    INNER JOIN edt.ed_areas ed_areas ON ed_areas.id = ed_fotos.redacao_id
    ORDER BY id DESC) a
    WHERE rownum <= 200 )
    WHERE rnum >= 100

  • Is trim(both from string) is the same as trim(string)...

    trim(both from string) is the same as trim(string)...
    like in
    SELECT trim(both from STRING_COLUMN) "A" , trim(STRING_COLUMN) "B" from TEXT_TABLE
    will "A" and "B" will be same
    I know this was asked in previous threads , but wanted clarify ..Thanks in advance

    Yes, they are the same
    (See the syntax diagram here http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/functions153a.htm#SQLRF06149)

  • Hi, since installing mountain lion on my macbook, it wont download pdf files from internet sites, the same problem does not exist on my mac mini, i am using firefox on both macs, please help!

    Hi, since installing mountain lion on my macbook, it wont download pdf files from internet sites, the same problem does not exist on my mac mini, i am using firefox on both macs, please help!

    Back up all data.
    Quit Safari. In the Finder, select Go ▹ Go to Folder... from the menu bar, or press the key combination shift-command-G. Copy the line of text below into the box that opens, and press return:
    /Library/Internet Plug-ins
    From the folder that opens, remove any items that have the letters “PDF” in the name. You may be prompted for your login password. Then launch Safari and test.
    If you still have the issue, repeat with this line:
    ~/Library/Internet Plug-ins
    If you don’t like the results of this procedure, restore the items from the backup you made before you started. Relaunch Safari again.

  • Unable to delete Role from User ID in SAP SOLMAN production system but able to from DEV with the same authorization, pls suggest

    unable to delete Role from User ID in SAP SOLMAN production system but able to from DEV with the same authorization, pls suggest

    Hi,
    For SU01 role removal, you do not need S_USER_AGR with 02, and as you mentioned both authorizations available in production, if so trace should not show you the S_USER_AGR with 02 with RC=04.
    I would recommend to do role comparison for the user performing the activity. and then check if you have the S_USER_AGR with 02 in user buffer SU56.
    But ideally it should not ask you S_USER_AGR for 02 through SU01, so please take help of abaper to debug it.
    Also put trace in non-prd to see if S_USER_AGR is getting checked with 02 for removal through SU01.
    BR,
    Mangesh

  • Prevent multiple users from editing/approving the same form SPD 2013,SP 2013

    Hello all, I have a workflow with a to do task, the task is assigned to a group so any of the users in that group can go in and do a quality check on form data and approve it.  How do I prevent multiple users from working on the
    same form? do I just require check out? or is there a way to notify the rest of the group that a user has already started the quality check.

    The "Require Checkout" option is your best bet.  You can also enable the auto checkout on edit option to allow minimal effort on the side of the user.  Other users will then get the error message stating the item is checked out, if they try to
    edit it.
    If you'd like, you could add a workflow to the task list that triggers when something is changed.  That workflow can check if the item is checked out and if so, email the other users assigned to the task.
    I trust that answers your question...
    Thanks
    C
    |
    RSS |
    http://crayveon.com/blog |
    SharePoint Scripts | Twitter |
    Google+ | LinkedIn |
    Facebook | Quix Utilities for SharePoint

  • I updated my Iphone4S this week to the newest IOS 7.1 and now everyone who calls me says the calls are not clear, muffled.  Is there something I can do to clear this?  4 people in 4 different states that called me all said the same thing.

    I updated my Iphone4S this week to the newest IOS 7.1 and now everyone who calls me says the calls are not clear, muffled.  Is there something I can do to clear this?  4 people in 4 different states that called me all said the same thing.

    Settings > General > Reset > Reset Network Settings
    Contact the carrier to troubleshoot.

  • How can I order two different calendars from iPhoto in the same order?

    How can I order two different calendars from iPhoto in the same order?

    You cannot - the "Buy Calendar" button is only available, while viewing one calendar. You can order more than one copy of the same calendar on one order, however.

  • I currently have aT-Mobile sim, and therefore, a UK telephone number. Can I get a sim from EE with the same number to use in my iphone 5?

    I currently have aT-Mobile sim, and therefore, a UK telephone number. Can I get a sim from EE with the same number to use in my iphone 5?

    The easiest way to find out what carrier it's locked to would be to call the store where you purchased the phone.
    The phone can be unlocked from any location.  However, most carrier have restrictions about unlocking and you have to contact them for details.

  • Can I have diff transacton Variant for the same tcode?

    Hi Experts,
    Can I have diff transacton Variant for the same tcode? If so i will i assign it to diffrent users.?
    Requirement Details:
    I have three diiffrent roles accessing XK02.
    I need to provide diffrent transactions variants for these 3 roles, through Variant groups. How will i do this?
    When i tried to do this, i found that, we need to activate a standard variant for the transaction, which inturn will be default for all these roles. S how will i specify different Transaction variant for diff  roles...
    Please help..
    In the below link i have seen a solution where i need to login into each users profile and set it.
    link: [Variant Groups;
    Thanks,
    Jithin

    >
    jithin james wrote:
    > Say i have two groups for the same transaction with two diff variants,
    > then for group 1 i will assign  variant1 and will activate this as the standard variant.
    > NOw for group two, i will assign my second variant (without activating it as standard variant).
    The way you explain is strange, I don't understand. You can't assign manually a variant group to a transaction variant, this is done automatically based on the names, maybe you make confusion with assignment between variant group and user?
    A little example:
    Say we have : ZG1 for group 1, and ZG2 for group 2. Transaction is ZT.
    Say user is assigned to group ZG1 by default, and also ZG2 (he may switch to ZG2 using the transaction I mentioned above)
    When user runs ZT transaction, SAP uses automatically ZG1ZT transaction variant (the standard variant is ignored when a transaction variant is found based on variant group). If the user switches to ZG2 variant group, SAP uses automatically ZG2ZT transaction variant.
    If you activate a standard variant, this will taken into account for users which don't have a variant group assigned, or those which transaction variant doesn't exist for variant group + transaction code.
    If it doesn't work as I said, maybe there is a bug, check SAP notes...

  • HT203167 I downloaded two movies from itunes at the same time (both free digital copies from a dvd) and when they finished downloading... they disappeared. I've not been able to re-download them or find them.. anywhere. Any suggestions for tracking them d

    I downloaded two movies from itunes at the same time (both free digital copies from a dvd) and when they finished downloading... they disappeared. I've not been able to re-download them or find them.. anywhere. Any suggestions for tracking them down?

    If you downloaded them on your computer's iTunes then they should have gone into the Movies part of your iTunes library, if on a device (iPad, iPhone or iPod Touch) then into the Videos app - they haven't appeared there ?
    If you downloaded on your PC and they don't show in the Movies section then you could try searching for them by name (or part of their name) via windows explorer and see if that finds them. Or if you downloaded them on a device and they aren't in the Videos app then have you got a film age rating set in Settings > General > Restrictions that is hiding them, and if not can you find them via the device's spotlight search screen (swipe your first homescreen to the right) ?

  • How do I attach a 2nd PDF from iBook to the same email

    After I initiate an email from iBooks and attach a PDF document how do I attach a 2nd PDF from iBook to the same email

    You can only email one pdf at a time.

  • Function output in a select statement

    I have written a function(get_cols) which returns the following string (this string is created dynamically from the fuctions depending on the rows of the tables)
    the output of the function is;
    MAX (CASE WHEN field_code = 'test_pho' THEN VALUE END ) AS test_pho
    ,MAX (CASE WHEN field_code = 'ESN' THEN VALUE END ) AS ESN
    ,MAX (CASE WHEN field_code = 'IMSI' THEN VALUE END ) AS IMSI
    ,MAX (CASE WHEN field_code = 'PHONE_NO' THEN VALUE END ) AS PHONE_NO
    What I need to do is to use this as it is in a another select statement like;
    (1)
    select
    empno,
    MAX (CASE WHEN field_code = 'test_pho' THEN VALUE END ) AS test_pho
    ,MAX (CASE WHEN field_code = 'ESN' THEN VALUE END ) AS ESN
    ,MAX (CASE WHEN field_code = 'IMSI' THEN VALUE END ) AS IMSI
    ,MAX (CASE WHEN field_code = 'PHONE_NO' THEN VALUE END ) AS PHONE_NO
    from my_employee e, my_columns c
    where e.emp_no = c.emp_no
    and c.emp_no = '100003'
    group by empno
    function returns the correct output, but when i call the function in the select like below it get it as a whole string and doesn't give the correct output
    (2)
    select empno, get_cols('100003')
    from my_employee e, my_columns c
    where e.emp_no = c.emp_no
    and c.emp_no = '100003'
    how can i get the output of the function to the select as separate line as shown is above(1)
    When I get the above output separately and give in the select as above (1) it gives the correct output I want ??
    any help please

    josleen wrote:
    Hi BluShadow,
    Your solution seem interesting. Can you explain how can this be used to produce the required output ? Do we need to maintain a separate my_columns table ?Not quite sure what you are asking?
    As with any query, the output columns have to be defined at design time, you cannot have the number of columns dynamically generated based on the data. So, if you said you wanted to pivot data from rows to columns and the number of possible values in those rows could change, you cannot pivot those to columns unless you are expecting those values or have allowed for maximum number of values.
    Basic example... Let say we have the following data:
    SQL> select * from dept;
        DEPTNO DNAME          LOC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTONAnd we want to pivot that data to put the departments as columns rather than rows. We can simply do something like:
    SQL> ed
    Wrote file afiedt.buf
      1  select max(decode(deptno,10,dname)) as dname_10
      2        ,max(decode(deptno,10,loc)) as   loc_10
      3        ,max(decode(deptno,20,dname)) as dname_20
      4        ,max(decode(deptno,20,loc)) as   loc_20
      5        ,max(decode(deptno,30,dname)) as dname_30
      6        ,max(decode(deptno,30,loc)) as   loc_30
      7        ,max(decode(deptno,40,dname)) as dname_40
      8        ,max(decode(deptno,40,loc)) as   loc_40
      9* from dept
    SQL> /
    DNAME_10       LOC_10        DNAME_20       LOC_20        DNAME_30       LOC_30        DNAME_40       LOC_40
    ACCOUNTING     NEW YORK      RESEARCH       DALLAS        SALES          CHICAGO       OPERATIONS     BOSTON
    SQL>However if a further department is added:
    SQL> insert into dept values (50, 'IT SUPPORT', 'LONDON');
    1 row created.
    SQL> select max(decode(deptno,10,dname)) as dname_10
      2        ,max(decode(deptno,10,loc)) as   loc_10
      3        ,max(decode(deptno,20,dname)) as dname_20
      4        ,max(decode(deptno,20,loc)) as   loc_20
      5        ,max(decode(deptno,30,dname)) as dname_30
      6        ,max(decode(deptno,30,loc)) as   loc_30
      7        ,max(decode(deptno,40,dname)) as dname_40
      8        ,max(decode(deptno,40,loc)) as   loc_40
      9  from dept
    10  /
    DNAME_10       LOC_10        DNAME_20       LOC_20        DNAME_30       LOC_30        DNAME_40       LOC_40
    ACCOUNTING     NEW YORK      RESEARCH       DALLAS        SALES          CHICAGO       OPERATIONS     BOSTON
    SQL>we obviously don't get to see the new data, unless we change our query to add this expected additional column(s) in...
    SQL> ed
    Wrote file afiedt.buf
      1  select max(decode(deptno,10,dname)) as dname_10
      2        ,max(decode(deptno,10,loc)) as   loc_10
      3        ,max(decode(deptno,20,dname)) as dname_20
      4        ,max(decode(deptno,20,loc)) as   loc_20
      5        ,max(decode(deptno,30,dname)) as dname_30
      6        ,max(decode(deptno,30,loc)) as   loc_30
      7        ,max(decode(deptno,40,dname)) as dname_40
      8        ,max(decode(deptno,40,loc)) as   loc_40
      9        ,max(decode(deptno,50,dname)) as dname_50
    10        ,max(decode(deptno,50,loc)) as   loc_50
    11* from dept
    SQL> /
    DNAME_10       LOC_10        DNAME_20       LOC_20        DNAME_30       LOC_30        DNAME_40       LOC_40        DNAME_50       LOC_50
    ACCOUNTING     NEW YORK      RESEARCH       DALLAS        SALES          CHICAGO       OPERATIONS     BOSTON        IT SUPPORT     LONDON
    SQL>Now, rather than having a messy SQL statment with lots of max(decode... statements we can provide a pipelined function to return the same thing...
    SQL> ed
    Wrote file afiedt.buf
      1  CREATE OR REPLACE TYPE mydepts AS OBJECT
      2  ( dname_10   VARCHAR2(14),
      3    loc_10     VARCHAR2(13),
      4    dname_20   VARCHAR2(14),
      5    loc_20     VARCHAR2(13),
      6    dname_30   VARCHAR2(14),
      7    loc_30     VARCHAR2(13),
      8    dname_40   VARCHAR2(14),
      9    loc_40     VARCHAR2(13),
    10    dname_50   VARCHAR2(14),
    11    loc_50     VARCHAR2(13)
    12* )
    13  /
    Type created.
    SQL> ed
    Wrote file afiedt.buf
      1* CREATE OR REPLACE TYPE mydepttable AS TABLE OF mydepts
    SQL> /
    Type created.
    SQL> ed
    Wrote file afiedt.buf
      1  CREATE OR REPLACE FUNCTION alldepts RETURN mydepttable PIPELINED IS
      2    v_obj mydepts := mydepts(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
      3    CURSOR cur_depts IS
      4      select deptno, dname, loc from dept;
      5  BEGIN
      6    FOR i IN cur_depts
      7    LOOP
      8      CASE i.deptno
      9       WHEN 10 THEN v_obj.dname_10 := i.dname; v_obj.loc_10 := i.loc;
    10       WHEN 20 THEN v_obj.dname_20 := i.dname; v_obj.loc_20 := i.loc;
    11       WHEN 30 THEN v_obj.dname_30 := i.dname; v_obj.loc_30 := i.loc;
    12       WHEN 40 THEN v_obj.dname_40 := i.dname; v_obj.loc_40 := i.loc;
    13       WHEN 50 THEN v_obj.dname_50 := i.dname; v_obj.loc_50 := i.loc;
    14      ELSE NULL;
    15      END CASE;
    16    END LOOP;
    17    PIPE ROW (v_obj);
    18    RETURN;
    19* END;
    SQL> /
    Function created.
    SQL> select *
      2  from table(alldepts());
    DNAME_10       LOC_10        DNAME_20       LOC_20        DNAME_30       LOC_30        DNAME_40       LOC_40        DNAME_50       LOC_50
    ACCOUNTING     NEW YORK      RESEARCH       DALLAS        SALES          CHICAGO       OPERATIONS     BOSTON        IT SUPPORT     LONDON
    SQL>So, now there is a single function that acts like a table and does the functionality of pivoting the data. What the function actually does to generate the data is entirely up to you whether that is obtaining data from different tables or some PL/SQL code processing to perform some complex algorithmic type thing on the data from a passed in parameter etc. The point of the pipeline function is that it can return multiple columns of data and act as if it is a table that can be queried against, however it still remains that the output columns must be known at design time. This is actually a requirement of the SQL engine, as you cannot make a dynamic function that returns X number of columns based on data.
    There is however a technique that can allow you to dynamically generate a number of columns based on data, but it involves getting a little more under the hood of Oracle and interfacing with the ODCI interface, actually using a pipelined funcion technique in conjunction with, in laymans terms, stepping in at the point the query is executing and telling oracle what columns this pipelined function is going to return, but at the same time as defining the returned columns.
    An example of doing this is given by AScheffer on this thread: How to pipeline a function with a dynamic number of columns?
    ... but you really are getting into a complex world if you try and write your queries this way, just to try and make generic queries with dynamic columns.
    In essence, the actual business need to actually return a dynamic number of columns is very slim as most applications, whether that is a user facing interface or report generator etc. will be expecting certain data columns to be returned to expected columns or fields within them. If you really want to dynamically pivot data with an unknown number of columns it is usually best to let things like reporting tools deal with this area as that is what they are best at, and don't try and do it within SQL.

  • [OCI] - Select statement on Clob doesn't return the full column's content.

    Hi All,
    I'm actually working on a application using OCI on an Oracle 11gr2. The Oracle client as the 11.2.0 version, was fully(admin) installed and configured with the same NLS_LANG variable as the DB.
    In the Database, we are using CLOB to store long text files. The setup our application was writing the initial CLOB correctly, but we cant read it, we just a part of the text.
    I checked it in SQLPlus and SQLDeveloper :
    select sourcecode from script where name like 'callback';
    result :
    SOURCECODE
    // #import "CallbackFctLib"
    * Benutzerdefinierte Funktionen bei der
    if I set the long to 1000 before the Select statement, then the full file is displayed.
    set long 1000;
    select sourcecode from script where name like 'callback';
    So, this works for SQLPlus and SQLDeveloper but how can I set it for the whole System? is ist possible? does any parameters have to be pass to the "sourcecode" column to define any format?
    thanks for reading...

    Hi!
    When you establish connection to database, try explicitly give instructions about what encoding use your DB, e.g.:
    // connection properties
    Properties connInfo = new Properties();
    connInfo.put("user", username);
    connInfo.put("password", password);
    connInfo.put("charSet", "Cp1251"); // enter your codepage!!!
    // getconnection
    Connection db = DriverManager.getConnection(dataurl, connInfo);

Maybe you are looking for