Counter in a select

Hi, I need a counter in a select without using temporal tables. Simplified, it's:
select a.code, b.desc
from a, b
where a.code=b.code
(table a has several fields as pk, not only 'code')
and results are:
coda Barcelona
codb New York
codc New York
codd Paris
and should be
coda Barcelona 1
codb New York 1
codc New York 2
codd Paris 1
Using group by and nested selects I get the total of lines by city (eg: Barcelona 1, New York 2,...), but not the number of line.
Thanks!

How about this?
SQL> r
  1  select dname, ename,
  2         row_number() over (partition by dname order by ename) rn
  3  from emp, dept
  4* where emp.deptno=dept.deptno
DNAME          ENAME              RN
ACCOUNTING     CLARK               1
ACCOUNTING     KING                2
ACCOUNTING     MILLER              3
RESEARCH       ADAMS               1
RESEARCH       FORD                2
RESEARCH       JONES               3
RESEARCH       SCOTT               4
RESEARCH       SMITH               5
RESEARCH       WARD                6
SALES          ALLEN               1
SALES          BLAKE               2
SALES          JAMES               3
SALES          MARTIN              4
SALES          TURNER              5
SALES          WARD                6
15 rows selected.
SQL>

Similar Messages

  • Adding count from two select statements

    Hi,
    How to add counts from two select statements in a single SQL statement.
    For ex: I have
    SELECT COUNT(DISTINCT COL1) FROM table1
    and
    SELECT COUNT(DISTINCT COL2) FROM table2.
    I want to add the counts from these two sqls in a single select query. How to do this.
    Thanks & Regards,
    Sunil.

    select ((SELECT COUNT(DISTINCT COL1) FROM table1) + (SELECT COUNT(DISTINCT COL2) FROM table2)) as "total" from dual;
    regards,

  • Take count on consecutive selections of SELECT-OPTIONS.

    Hi Experts,
    i am new to ABAP    Development. My client has given a requirement in which i have to calculate count of Documents under
    selected Document types. The Document types is to be selected in Select-options. i have to to do multiple selections in this.
    But i have to take count of consecutive selections using select-options. Means for first time i will click select-options,give multiple selections and take count of documents corresponding to given Document types and save in count1.
    2nd time : Now again i have to select same select-options to take count of documents of different Document Types and save in count2.
    Is any event has to be triggered in this ...if yes then how.
    SELECT-OPTIONS Doc_type for bkpf-blart.
    select count(*) from bkpf into it_loc-wa where bkpf~blart in Doc-type.
    And then i have to create a report using first count1 and second count2.
    Please help me. 
    Regards:
    Mohammed Shahab Ansari

    Hi
    Can you be more clear on the requirement please. Why do you want to get the number of documents for the document selected on the selection screen? Is your requirement to get the documents belonging to that document type  and again trigger another selection screen based on that selection ?
    If you just want to get the count from your selection screen and then may be try the below code.
    tables: bkpf.
    data: ld_count type i.
    data: begin of lt_count occurs 0,
           dtype type bkpf-blart,
           count type i,
          end   of lt_count.
    SELECT-OPTIONS: s_Dtype for bkpf-blart.
      loop at s_dtype.
    select count(*) from bkpf into ld_count where bkpf~blart = s_Dtype-low.
      lt_count-dtype = s_dtype-low.
      lt_count-count = ld_count.
      append lt_count.
      clear lt_count.
    endloop.
    loop at lt_count.
      write: /5 lt_count-dtype, lt_count-count.
      endloop.
    Regards,
    Vijay V

  • Page Version Count within the selected dates

    Hi,
    We have a folder of name General in our site.
    i have two SharePoint date time controls on the user control.
    Now i need to get the number of page versions within the selected dates.
    I am able to get all the pages under the 'General' folder as below
    SPFolder oGeneralFolder = list.RootFolder.SubFolders["General"];
     DataTable dt = new DataTable();
    dt.Columns.Add("Page Versions Count");
    foreach (SPFile file in oGeneralFolder.Files)
    dr["Page Versions Count"] = ?
    Now i need to get the number of page versions within the selected dates.
    Because i don't see any supporting property in SPFile object to filter the page version according to the date.
    Please share your ideas or thoughts on the same.
    Regards,
    Sudheer
    Thanks & Regards, Sudheer

    Hi Naga,
    Get item of File and do the following
      foreach (SPListItemVersion v in item.Versions)
    if(v.Created > daterange &&v.Created < daterange )
    count++;
    Within the foreach loop, check for specified date range and increase the count if it fine.
    Thanks,
    Vivek
    Please vote or mark your question answered, if my reply helps you

  • How to reduce logical count and scan count for a select query

    hi,
    I have two tables one is master and other is history. i need to combine this two tables into one temporary table.
    I am using the below query to create temp table.
    Select * into temporders
    from
    (select * from orders
    union
    select * from ordershistory) b
    where updateon= (select max(updateon)from (select updateon,name,units,subunits from orders
    union
    select updateon,name,units,subunits from ordershistory) a
    where updateon <='11/08/2008 11:18 AM' and a.name=b.name and a.units=b.units and a.subunits=b.subunits group by name,units,subunits)
    order by report,subunitsorder
    the statistics for this query:
    SQL Server parse and compile time:
    CPU time = 47 ms, elapsed time = 62 ms.
    Table 'Worktable'. Scan count 556, logical reads 1569, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'ORDERSHISTORY'. Scan count 116, logical reads 339, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'ORDERS'. Scan count 116, logical reads 285, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    SQL Server Execution Times:
    CPU time = 32 ms, elapsed time = 63 ms.
    (115 row(s) affected)
    you see logical reads and scan count for worktable(temporary) is quite high.
    So anyone can give a solution for reduce the scan count and logical reads.
    NOTE: name,units, subunits,updateon columns have primarykey

    SQL ServerAm i reading it properly? :(
    This is Oracle Forum And not the SQL Server.
    Regards.
    Satyaki De.

  • Count(*) function in select statement having group by condition

    Hi
    I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
    the sql stement is grouped by state and it is joined with 5 more tables.
    Thanks
    ps: ignore the previous post

    Assuming there is one record per city per state, then
    SELECT state,count(*)
    FROM state_tbl
    GROUP BY stateWill get one record per state with the number of cities in each state. If you want to join that result set to other tables you need to either create a view with that statement or use an in-line view viz.
    SELECT c.cust_name,c.state,s.num_cities
    FROM customers c,
         (SELECT state,count(*) num_cities
          FROM state_tbl
          GROUP BY state) s
    WHERE c.state = s.stateTTFN
    John

  • Count() function with selective criteria?

    I'm struggling with what I would expect to be a fundamental reporting concept in CR.
    Suppose I have the following EMPLOYEE table:
    u2022 EMPLOYEE.ID
    u2022 EMPLOYEE.GENDER_CODE
    u2022 EMPLOYEE.MANAGER_FLAG
    I need to generate a statistical summary report containing the following:
    u2022 Total number of Employees
    u2022 Number of Male Employees
    u2022 Number of Management Employees
    This would be easy if I could just use a Count() function in the Function Workshop which
    contained selective criteria.  For example:  Count ({EMPLOYEE.GENDER_CODE} = 'M')
    But I can't figure out how to do this without getting a CR error message.
    Record Selection doesn't work, because I need the whole data set.
    Group Selection with Summaries doesn't work, because the gender and management
    attributes are not mutually exclusive.
    One solution that seems awkward to me is to create additional SQL commands in the
    Database Expert using COUNT(*) and WHERE criteria to get the number of Males
    and number of Managers.  But I have to believe that there is a better way.  Plus this
    approach causes problems elsewhere in my report.
    Am I missing something?
    Thanks,
    Bill

    Thanks Raghavendra!
    The good news is that I was able to create a "1 or 0" formula and then sum the resulting values.
    The bad news is that I'm only able to get this to work for part of my report.
    I am joining two tables.  As an example COMPANY and EMPLOYEE.
    I can use "1 or 0" formulas on all of the employee statistics.
    But I cannot do the same for company statistics, because the number of company records being assigned a 1 is being inflated by the join between the two tables.
    I have achieved a successful result by using Running Total Fields at the end of the report.
    But I want to place these summary statistics at the begining of my report.  (Such as Total Companies in the Western Region.)
    I tried to use "COMPANY.NAME = previous(COMPANY.NAME)" logic in my function, but then I was not allowed to summarize it.
    Any ideas?
    Thanks,
    Bill

  • Cycle count Storage location selection

    Hi,
    Could someone explain to how the Storage locations are selected when I run transaction MICN.
    Our settings in config are like this
    I have a material in one storage location, it has never been counted. I run the transaction MICN for that material in my plant with today's date nothing is returned. I change the date to one year in the future and the system returns many locations including the one the material is in. I can comprehend that because the count date is empty in the mard table, I have to enter a future date but why do I get many location where the material is not even stored in?
    By the way as we only want to count all our items once per year, they have all been set with the letter D in the material master.
    Kind Regards
    Thierry

    Where do you see the advantage of cycle counting if all your materials are classified with D?
    In that case you can just use MI31 and do annual inventory.
    If you have no last count date, then SAP uses the creation date if the accounting view.
    Which is for all materials equal if you loaded them before go-live.
    And the storage location is coming from MARD table, all locations for which your material was created. And because you did not activate the box to exclude the materials with zero stock.

  • Swap Counter in simple Selection Sort method

        void sortSelection(List object)
             int swapCount=0;
               for(int i = list.length; i >= 2; i--)
                 int maxIndex = 0;
                        for(int j = 1; j < i; j++)
                             if(list[j] > list[maxIndex])
                                  maxIndex = j;
                   int temp = list[maxIndex];        //
                   list[maxIndex] = list[i-1];         //         Is the problem here?        
                   list[i-1] = temp;                     //
                   swapCount++;                     //
              System.out.println("\n" +swapCount+ " swaps were needed. ");
        }This is a pretty simple selection sort method. But I am fairly sure that I am incorrectly counting swaps with the code. I have defined a "swap" as when two array indices have their values switched. I am convinced that there is something wrong because the array of numbers is randomly generated, and yet the swapCount counter is always equal
    to (list.length-1) when it prints out. I would think it should be different every time. I indicated the line where I believe the problem lies, but I am not for sure. Thanks everybody.

    List of Random Integers, then sorted:
    9, 29, 30, 42, 53, 58, 59, 64, 66, 69, 74, 79, 79, 87, 95
    9 swaps were needed.then sort again (descending) and get this:
    95, 87, 79, 79, 74, 69, 66, 64, 59, 58, 53, 42, 30, 29, 9
    1 swaps were needed.I'm pretty sure that isn't correct. But forgive me if I did not specify that it was to work in descending order as well.
    So I tried this:
    void sortSelection(List object)
             int swapCount=0;
               for(int i = list.length; i >= 2; i--)
                 int maxIndex = 0;
                        for(int j = 1; j < i; j++)
                             if(list[j] > list[maxIndex])
                                  maxIndex = j;
                  if (maxIndex != (i-1) && ascending==true)
                       int temp = list[maxIndex];
                        list[maxIndex] = list[i-1];                             
                        list[i-1] = temp;   
                        swapCount++;
                  else if (maxIndex != (i+1) && descending==true)
                       int temp = list[maxIndex];
                        list[maxIndex] = list[i-1];                             
                        list[i-1] = temp;   
                        swapCount++;
    } adding the i+1 (because it is going in a different order)...and I think I solved it!

  • Difference in count(*) query and select * query results

    i am using Oracle9i Enterprise Edition Release 9.2.0.8.0.
    in a table select count(*) returns 34, but when i run select * statement it returns only 6 rows. Results are pasted below. Can any one knows the reason of this mismatch
    SQL> select * from pay_at;
    EMP_CODE STA IN_OUT_DA TIME
    100341 IN 07-MAR-04 09:32
    100341 OUT 07-MAR-04 10:24
    103165 IN 31-MAR-03 14:41
    103165 OUT 31-MAR-03 20:00
    101204 IN 19-APR-04 13:00
    101204 OUT 19-APR-04 23:59
    6 rows selected.
    SQL> select count(*) from pay_at;
    COUNT(*)
    34
    ------------------------------------------------------

    Maybe you pressed CTRL-c during the fetch phase of "select * from" ?
    If this is not the case, try to build a very small test case we can run that shows this behaviour and post it here.
    Regards,
    Rob.

  • RADIO BUTTON TALLY -Auto count & display ticked/selected radio buttons

    Hi, feels like this should be simple & straight forward, but obviously for me.
    I have a Questionnaire with say twenty YES or NO answers.
    I've used grouped Radio Buttons as fields so as to pick one or the other.
    I have a read only "YES Tally" text field at bottom of questionaire, formated as 'Number'.
    I want the form to automatically calculate & display the number of YES answers (radio buttons) selected or ticked.
    How can i achieve this?
    ZB.

    If you have your export value for the buttons set to "1" then you only need to use the calculation option "the field is the sum of: " calculation option., or you can use the 'simplified field notation' to sum the field names. If you use and a different value for the export value,then you need to write a custom script and either test for the export value of the "Yes" button or test for the export value not be "Off".

  • Count all values with a special WHERE clause in a select for a group?

    Hello,
    I have the following table1:
    code, month, value
    *1,1,40*
    *1,2,50*
    *1,3,0*
    *1,4,0*
    *1,5,20*
    *1,6,30*
    *1,7,30*
    *1,8,30*
    *1,9,20*
    *1,10,20*
    *1,11,0*
    *1,12,0*
    *2,1,10*
    *2,2,10*
    *2,3,20*
    *2,4,20*
    *2,5,20*
    *2,6,30*
    *2,7,40*
    *2,8,50*
    *2,9,20*
    *2,10,20*
    *2,11,20*
    *2,12,20*
    This is a table with 3 columns, first column is a code, second one is the number of month, third one is a value.
    Now I want to select the records for each code. For example all records for code=1.
    I want to count how much values=0 for this code=1. After this counting I want to update the value with this count of 0.
    For my example:
    For code 1 there are 4 fields with value 0. Therefore I want to update all values of code1 to 4.
    For the second code=2 there are no value=0. Therefore I want to update the values of code2 to 0.
    This should be the result:
    code, month, value
    *1,1,4*
    *1,2,4*
    *1,3,4*
    *1,4,4*
    *1,5,4*
    *1,6,4*
    *1,7,4*
    *1,8,4*
    *1,9,4*
    *1,10,4*
    *1,11,4*
    *1,12,4*
    *2,1,0*
    *2,2,0*
    *2,3,0*
    *2,4,0*
    *2,5,0*
    *2,6,0*
    *2,7,0*
    *2,8,0*
    *2,9,0*
    *2,10,0*
    *2,11,0*
    *2,12,0*
    My question is:
    Is there any possibility in oracle to count in a select (or in a insert/update statement) all values=0 for one group (in this example named CODE) and do an update in the same statement for this group?
    Hope anyone can give me a hint if this is possible?
    Thanks a lot.
    Best regards,
    Tim

    Here's the select:
    SQL> select code, month
      2        ,count(decode(value,0,1,null)) over (partition by code) ct
      3  from   t
      4  order by code, month
      5  ;
                    CODE                MONTH                   CT
                       1                    1                    4
                       1                    2                    4
                       1                    3                    4
                       1                    4                    4
                       1                    5                    4
                       1                    6                    4
                       1                    7                    4
                       1                    8                    4
                       1                    9                    4
                       1                   10                    4
                       1                   11                    4
                       1                   12                    4
                       2                    1                    0
                       2                    2                    0
                       2                    3                    0
                       2                    4                    0
                       2                    5                    0
                       2                    6                    0
                       2                    7                    0
                       2                    8                    0
                       2                    9                    0
                       2                   10                    0
                       2                   11                    0
                       2                   12                    0

  • Select Count(*) from Sample_table - how to get the count using JDBC?

    Hi All,
    It would be glad if anyone could help me with this. The problem is that I have to get the 'count' of records selected from a arbitrary table say, 'sample_table'. Is that possible to form the SQL in JDBC as
    Select Count(*) from Sample_table
    and get the value of the count? If yes, how?
    Thanks in advance
    Prabz

    stmt = con.createStatement();
    ResultSet recordcnt_rs = stmt.executeQuery("Select Count (*) as record_ctr From Sample_table");
    recordcnt_rs.next();     
    record_ctr = recordcnt_rs.getInt("record_ctr");
    hope this helps.

  • Selecting on a Max Count of rows

    I would like some advice on the following SQL. I am trying to select the max(count(*)) of a selct count(*). (if you know what I mean??)
    select district_id
    from (
         -- retrieve a list of districts, plus the count of employee related records
         select d.district_id, count(*) CountX
         from station s, division d
         where s.resp_empl_num = :empID and
         s.stn_status_code = 'AC' and
         s.resp_div_num = d.division_code
         group by d.district_id
         -- match on the district with the max count #
    where CountX = (
         select max(count(*))
         from station st, division dv
         where st.resp_empl_num = :empID and
         st.stn_status_code = 'AC' and
         st.resp_div_num = dv.division_code
         group by dv.district_id
    )

    Try this
    select district_id
    from ( select district_id,
                  rank() over (order by count(*) desc) rn
           from   station s, division d
           where  s.resp_empl_num = :empID and
                  s.stn_status_code = 'AC' and
                  s.resp_div_num = d.division_code
           group by d.district_id
    ) where rn=1
    ;

  • Select count distinct

    Hi, Anybody can help !
    I have problem with select count distinct.
    example :
    select distinct custid from order_h
    total result : 141 rows selected.
    but :
    select count(distinct custid) from order_h
    result :
    COUNT(DISTINCTCUSTID)
    140
    Why the total difference, for listing 141 but for count 140 ?
    Is my statement wrong ? How to use count and distinct ?
    Thank's

    Look here..
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/functions032.htm#i82697
    Bye
    Acr

Maybe you are looking for

  • Mic can't hear directly from speaker

    pls help me, my laptop compaq V3754TU sound from mic can not hear on speaker. i try check at sound properties, mixer device "conexant HD Audio output" on the PLAYBACK SHOW THE FOLLOWING VOLUME CONTROLS: check box for microphone not have... :-( . only

  • Please help the aged - Can't get my computer to recognise the Zen!

    Hi there , I really hope someone can help me. I bought my Pa a Zen Vplus 4 GB 2 years ago and he still cannot get it to work! I li've overseas so on my few trips back I try desperatley to get it going and still failing. I have checked the posts on he

  • Save Problem With Premiere Pro

    I have been having a problem when saving my project to a new place and I wanted to make sure that I will be okay simply saving it to my desktop. I started the project on a windows computer and then have been working on a mac. My files come from an ex

  • Downgrade from Maps 3.0 to 2.0

    As Nokia Maps 3.0 freezes when I go to settings - internet and has some other issues, I want to get back to the previous version. This was explained at http://betalabs.nokia.com/betas/view/nokia-maps-30-with-ovi-maps but is no longer available. Can s

  • Gather schema status does not do customized tablespace

    My apologies if this question is in the wrong forum, as I was unsure exactly where to post. Under Oracle Financials, Sysadmin responsibility, there is a concurrent program called gather schema stats. We run this weekly. Several years ago, we created