Getting Record Count From Date Range - IDE: PLSQL Developer

I would like to count the number of member records that fall within a specified date range based on the members effective and expiration dates and their 'elg_code'. I have posted the SQL for some sample data. What I would like to see returned is three columns of counts where the members eff_date exp_date fall within the date range specified by the SQL and have an Elg_code of ' ' (one blank space).
So, what I would like is all members with elg_code ' ' where there eff_dt and exp_dt range falls within APR 2012, MAY 2012 & JUN 2012. So, based on the sample data I posted, Mark, where his elg_code record is ' ', his eff_dt is 1/1/2011 and his exp_dt is within APR 2012 (4/30/2012). Mark's range falls within APR 2012, but not MAY or JUNE of 2012. Marty would tally for both APR and MAY since his eff_dt is before MAY 2012 and his exp is within MAY 2012. etc..
Based on the data below, the results should look like:
APR MAY JUN
4 3 2
APR should have FRANK, MARK, MARTY, MARY,
MAY should have FRANK, MARTY, MARY
JUN should have FRANK and MARY
NOAM and JEAN should not show up as their records with elg_code ' ' do not have eff_dt and exp_dt records that fall within APR-JUN 2012.
So what I tried without success as it appears I have some kind of Cartesian issue (?), is:
select count(m1.mbr_name) APR,
count(m2.mbr_name) MAY,
count(m3.mbr_name) JUN
from mbr2 m1,
mbr2 m2,
mbr2 m3
where m1.eff_dt < '01-may-2012'
and m1.exp_dt > '01-apr-2012'
and m1.elg_code = ' '
and m2.eff_dt < '01-jun-2012'
and m2.exp_dt > '01-may-2012'
and m2.elg_code = ' '
and m3.eff_dt < '01-jul-2012'
and m3.exp_dt > '01-jun-2012'
and m3.elg_code = ' '
Below is the DML
Thanks for any assistance!
create table mbr2 (mbr_name varchar(10), grpid varchar(1), eff_dt date, exp_dt date, elg_code varchar(1))
commit
insert into mbr2 values ('MARK', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('04-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARK', 'A', to_date('05-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('MARTY', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('05-31-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARTY', 'A', to_date('06-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('FRANK', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('FRANK', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('MARY', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('MARY', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('JEAN', 'C', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('07-01-2011', 'MM-DD-YYYY'), ' ')
insert into mbr2 values ('JEAN', 'C', to_date('07-01-2011', 'MM-DD-YYYY'), to_date('01-01-2012', 'MM-DD-YYYY'), 'C')
insert into mbr2 values ('NOAM', 'D', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), ' ')
commit

Hi,
Here's one way:
WITH     all_months     AS
     SELECT     LEVEL                         AS month_num
     ,     ADD_MONTHS (first_month, LEVEL - 1)     AS month_begin
     ,     ADD_MONTHS (first_month, LEVEL)           AS next_month_begin
     FROM     (
               SELECT  TO_DATE ('04-01-2012', 'MM-DD-YYYY')     AS first_month
               ,     TO_DATE ('06-01-2012', 'MM-DD-YYYY')     AS last_month
               FROM     dual
     CONNECT BY     level     <= 1 + MONTHS_BETWEEN (last_month, first_month)
SELECT    COUNT (CASE WHEN month_num = 1 THEN 1 END)     AS month_1
,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2
,       COUNT (CASE WHEN month_num = 3 THEN 1 END)     AS month_3
FROM       all_months  a
JOIN       mbr2        m  ON  a.month_begin       <= m.exp_dt
                        AND a.next_month_begin      >  m.eff_dt
WHERE     m.elg_code      = ' '
;This assumes you know how many months will be in the output. If you want to derive that from the data, or to give the columns meaningful aliases (such as APR-2011 instead of MONTH_1), then you'll need dynamic SQL (to get separate columns for each month) or string aggregation (if you don't mind one big VARCHAR2 column, formatted to look like several columns). See {message:id=3527823}
If you don't mind modifying the query a little every time you run it, you can hard-code the number of columns and the month names. In the main query, put exactly as many lines as you need, with the alias you want at the end. That is, instead of
,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2say
,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS may_2012I'm not sure exactly what the problem was with the query you posted. It looks like this site cut off parts of the query.
One thing that is definitely a mistake is that you're comparing DATE columns to VARCHAR2 literals. Never compare a DATE to a VARCHAR2; compare DATEs to DATEs (or compare VARCHAR2s to VARCHAR2s). So instead of
and m1.exp_dt > '01-apr-2012'you should say
and m1.exp_dt > TO_DATE ('01-apr-2012', 'dd-mon-yyyy')Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful.
Edited by: Frank Kulash on Jun 26, 2012 3:32 PM

Similar Messages

  • File to Jdbc: how to get record count from DB table

    Hello,
    i have a scenario file to JDBC....ihave to insert input file data to DB table.
    now my requirement is ..if i insert a file with 50 records to db table on first time, and then next time if i insert the file with 100 records to db table....the count should start from 51 in db table.
    if i insert another file 3rd time, with 25 records, the count shold start from 151...so how can i achieve this functionality....
    i think following options:
    1. in mapping write lookup to call JDBC sender channel and fetch count and map same to target filed of DB. if this is ok...please provide UDF code..2. db triggers (not suitable for my req.)
    So kindly let meknow possible solutions and required UDF codes..please
    Thanks in advance...SARAN

    >>>..if i insert a file with 50 records to db table on first time, and then next time if i insert the file with 100 records to db table....the count should start from 51 in db table.
    if i insert another file 3rd time, with 25 records, the count shold start from 151...so how can i achieve this functionality....
    i think following options:
    Suggestions:
    1)Create an id column in db table.  Talk to DB guy to create oracle sequencer for that column. So, every time you insert record that field will be updated with oracle sequencer. You dont need to handle this.
    Refer this link
    http://www.techonthenet.com/oracle/sequences.php
    2) If you want to handle via pi,  following suggestions..
    If you use PI 7.1 or above, use jdbc lookup in the mapping and do the query  something like this
    Refer this link
    /people/jin.shin/blog/2008/02/15/sap-pi-71-mapping-enhancements-series-graphical-support-for-jdbc-and-rfc-lookups
    Do select query as below
    select count(*) from tablename;
    the above query will return number of rows exist in the table. So use that value and map it in the target field.
    If you use pi 7.0 or below then use the previous reply and do the UDF implementation for jdbc lookup during mapping. Because jdbc lookup does not support in those versions.
    Hope that helps.
    Baskar

  • Get the record count from a query

    Hi,
    does anyone know how to get the record count from a sql query?
    e.g: I've got a ResultSet from the Statement.excuteQuery(), and I want to know how many records this ResultSet contains, how to get this?
    I'd read thoughout the documents of Statement and ResultSet, but couldn't find a solution, do I have to use another seperate query such as "select count(*)" to do this?
    thanks.
    Yang Liu

    If you are not using a scrollable result set then the following is the best way to do it.
    there are several key words in SQL that can be used, the one you are interested in is count();
    so if your query at the moment is
    "select col1, col2, col3 from my_table where col2=? and col3=?"you can work out how many rows will be returned by executing this command first
    "select count(col1) from my_table where col2=? and col3=?"this will return a result set with one row and one column, you can get the row count as follows:
    ResultSet rs = ps.executeQuery();
    int rowCount = rs.getInt(1);I hope this helps :)

  • Selecting last record in a date range

    Post Author: Alien8n
    CA Forum: Data Connectivity and SQL
    I need to extract the last record for a group and display it. Normally this would simply mean selecting the max date in the SQL but...I need the last record from within a specified date range. By using the max date it won't pull out any records for a group that has a record after my date range. ie I have 2 group items and they both have a record for each day of the weekBy leaving the date open it pulls out the last record, Friday, for each item in the group.I want to set a range saying give me the last record from Mon - Wed.What I want returned is Wed's records and if there's no records on Wednesday the next last record for that itemSo If I have...Item Date    RecordA     MON     1A     TUE      2A     FRI       3B     MON    4B     TUE      5B     WED    6B     THUR    7B     FRI       8I get A     TUE      2 B     WED    6FYI CR XI with SQL 2005

    Post Author: Alien8n
    CA Forum: Data Connectivity and SQL
    The database actually contains sample tests by date with each sample being tested multiple times over several months.I need to be able to pull a date range from the whole dataset, but I only want the last record in each date range. Samples can be tested every week or every 2 weeks and then the last record is used to show how the sample is doing over time. So you would run the report for say 3 weeks and show the last record for each sample. This wouldn't be a problem if you were only interested in the last 3 weeks data, but if you want to look at historical records you have to be able to pull from that date range. In effect the query has to work regardless of whether you ask for Mon-Fri (using my example above) or Mon-Wed or even just Mon alone. I'll give your example above a try and see how it goes

  • How can I get record count with a query?

    In Client/Server pplication,the client send a query to server.
    How can server get record count by oracle call interface?
    Is it need execute "select count(*) from ...."?

    Yes.
    Either that or increment a counter for each record fetched and
    loop round until you hit the last record.
    The first method would be more efficient on large datasets.

  • Getting the record count from result set

    i'm retreiving the result set using the executeQuery method, now i want to know how many records are there in the result set, that is the record count of the result set.
    one solution to that is to first use the executeUpdate and then use the executeQuery but i think that is not the right way.
    so please tell me is there any method in jdbc to get that thing done
    Tanx

    Hi
    Do you know if your DB supports "insensitive scrolling"?
    SQL generally do, but some don't - I had the same problem with the
    open source version of Interbase from Phoenix...
    Anyway - try creating your statement this way:
    public Statement createStatement(int resultSetType, int resultSetConcurrency)
    throws SQLException
    ...where resultset type should be:
    ResultSet.TYPE_SCROLL_INSENSITIVE
    Then you can do this:
    ResultSet rs = stm.executeQuery(q);
    int size = rs.last(); //this what you looking for?
    rs.beforeFirst();
    while(rs.next()){
    }

  • How get Mailbox Folder Item Count for date range?

    How to make query to Exchange 2013 like this:
    query ItemCount (specified Mailbox, specified Folder (with subfolders), specified Date Range)?
    I find this script for Exchange 2010: http://gsexdev.blogspot.ru/2012/10/item-age-sample-one-reporting-on-item.html
    This script dont work for Exchange 2013 ((

    I believe you are making the change on both the places in code, as this path version would be different in 2013
    C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
    Cheers,
    Gulab Prasad
    Technology Consultant
    Blog:
    http://www.exchangeranger.com    Twitter:
      LinkedIn:
       Check out CodeTwo’s tools for Exchange admins
    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

  • SQL DISTINCT COUNT IN DATE RANGE

    Dear All
    Thanks for your attention.
    A table is storing the task summary of each order
    e.g.
    Date                            | Order Number    | Task Number
    2015-01-01 00:00:01   | ABC123456        | JOB001
    2015-01-01 00:01:02   | ABC123456        | JOB002
    2015-01-01 00:10:02   | ABC123444        | JOB001
    2015-01-01 10:12:59   | ABC123456        | JOB002   (Since the job002 is not done correctly, need to work again)
    2015-01-01 18:20:05   | ABC888888        | JOB001
    2015-01-01 20:22:42   | ABC789456        | JOB001
    2015-01-01 21:02:11   | BBB121212        | JOB002
    I would like to write a single query that get the distinct count of order number in sepcific date range
    result as expected in three columns:
    2015-01-01
    task_number            | AM | PM
    JOB001         | 2    | 2
    JOB002         | 1    | 1
    explain the figures here:
    JOB001 AM = 2 (ABC123456, ABC123444)
    JOB002 AM = 1  (two records of ABC123456 count as 1 since we would like to know the number of orders)
    JOB001 PM = 2  (ABC888888, ABC789456)
    JOB002 PM = 1  (BBB121212)
    I wrote a query get similar result but the count of order is not distinct
    SELECT task_number,
    AM=SUM(CASE WHEN date >= ('2015-01-01 00:00:00') AND date < ('2015-01-01 12:00:00') THEN 1 ELSE 0 END),
    PM=SUM(CASE WHEN date >= ('2015-01-01 12:00:00') AND date < ('2015-01-02 00:00:00') THEN 1 ELSE 0 END)
    FROM MyTable
    WHERE task_number = 'JOB001'
    Could anyone advise how to enhance this query to let the result become disintct of order number?
    Many Thanks,
    swivan

    Try
    select task_number,
    count(distinct case when datepart(hour, [date]) >=0 and datepart(hour, [date]) < 12 then [OrderNumbers] END) as AM,
    count(distinct case when datepart(hour, [date]) >=12 and datepart(hour, [date]) < 24 then [OrderNumbers] END) as PM FROM myTable
    GROUP BY Task_Number
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles
    Thank you very much,  short and good answer

  • SUM(Case how to use this structure to get average values over date range

    I am using:
    Oracle SQL Developer (3.0.04) Build MAin-04.34 Oracle Database 11g Enterprise Edition 11.2.0.1.0 - 64bit Production
    How do I use the sum function with a case structure inside.
    so I have data that looks like has an ID, date, and value. I am looking to get the 7 day average for the date range of 4/1/2013 thru 4/20/2013
    with t as (
    select 1 ID_Key,to_date('4/1/2013','mm-dd-yyyy') date_val, 10 Value_num from dual union all
    select 1 ID_key,to_date('4/2/2013','mm-dd-yyyy'), 15 from dual union all
    select 1 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 20 from dual union all
    select 1 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/8/2013','mm-dd-yyyy'), 12 from dual union all
    select 1 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 8 from dual union all
    select 1 ID_key,to_date('4/10/2013','mm-dd-yyyy'), 6 from dual union all
    select 1 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 10 from dual union all
    select 1 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/14/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 10 from dual union all
    select 1 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 5 from dual union all
    select 1 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 2 from dual union all
    select 1 ID_key,to_date('4/20/2013','mm-dd-yyyy'), 3 from dual union all
    select 2 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 12 from dual union all
    select 2 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 15 from dual union all
    select 2 ID_key,to_date('4/6/2013','mm-dd-yyyy'), 5 from dual union all
    select 2 ID_key,to_date('4/7/2013','mm-dd-yyyy'), 7 from dual union all
    select 2 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 10 from dual union all
    select 2 ID_key,to_date('4/11/2013','mm-dd-yyyy'), 5 from dual union all
    select 2 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 6 from dual union all
    select 2 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 8 from dual union all
    select 2 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/18/2013','mm-dd-yyyy'), 10 from dual union all
    select 2 ID_key,to_date('4/19/2013','mm-dd-yyyy'), 5 from dual
    )**Please let me know if the table does not load.
    I would like to get the 7 day average as long as there is date for that row has enough previous dates, it not then it will return null.
    the results should look like this
    ID_Key      date_val     Value_num     7Day_Avg     7Day_Avg2
    1     4/1/2013     10          null          null
    1     4/2/2013     15          null          null
    1     4/3/2013     20          null          null
    1     4/5/2013     0          null          null
    1     4/8/2013     12          6.71          11.75
    1     4/9/2013     8          5.71          10.00
    1     4/10/2013     6          3.71          6.50
    1     4/12/2013     10          5.14          9.00
    1     4/13/2013     0          5.14          7.20
    1     4/14/2013     0          5.14          6.00
    1     4/15/2013     10          4.86          5.67
    1     4/16/2013     5          4.42          5.17
    1     4/17/2013     2          3.85          4.50
    1     4/20/2013     3          2.86          4.00
    2     4/3/2013     12          null          null
    2     4/5/2013     15          null          null
    2     4/6/2013     5          null          null
    2     4/7/2013     7          5.57          9.75
    2     4/9/2013     10          7.00          9.80
    2     4/11/2013     5          6.00          8.40
    2     4/12/2013     0          3.86          5.40
    2     4/13/2013     0          3.14          4.40
    2     4/15/2013     6          3.00          4.20
    2     4/16/2013     8          2.71          3.80
    2     4/17/2013     0          2.71          3.17
    2     4/18/2013     10          3.43          4.00
    2     4/19/2013     5          4.14          4.83As you may notice, there are gaps in the dates, so the value are then treated as zeros for the 7Day_Avg and then ignored for teh 7Day_Avg2 (not counted as number of days averaged do to no valu_num row)
    I was trying something like this to start, but getting error "missing keyword"
    select
    t.*/,
    sum(
          case
            when date_val between :day2 - 6 and :day2
            then value_num between date_val - 6 and date_val
            else null
            end
            as 7Day_avg
    form tShould I have the case structure outside the sum function?
    Any thoughts??
    Edited by: 1004407 on Jun 7, 2013 11:06 AM

    Hi,
    If you want the average of the last 7 days, including the current day, then then RANGE should be 6 PRECEDING, not 7.
    Try this:
    WITH     got_min_date_val AS
            SELECT  id_key, date_val, value_num
            ,       MIN (date_val) OVER () AS min_date_val
            FROM    t
            WHERE  date_val BETWEEN TO_DATE ('04-01-2013', 'mm-dd-yyyy')
                             AND   TO_DATE ('04-20-2013', 'mm-dd-yyyy')
    SELECT    id_key, date_val, value_num
    ,         CASE
                  WHEN  date_val >= min_date_val + 6
                  THEN  SUM (value_num) OVER ( PARTITION BY  id_key
                                               ORDER BY      date_val
                                               RANGE         6 PRECEDING
                        / 7
              END  AS avg_7_day
    ,         CASE
                  WHEN  date_val >= min_date_val + 6
                  THEN  AVG (value_num) OVER ( PARTITION BY  id_key
                                               ORDER BY      date_val
                                               RANGE         6 PRECEDING
              END   AS avg_7_day_2
    FROM      got_min_date_val
    ORDER BY  id_key
    ,         date_val
    Output:
       ID_KEY DATE_VAL   VALUE_NUM  AVG_7_DAY  AVG_7_DAY_2
             1 01-APR-13         10
             1 02-APR-13         15
             1 03-APR-13         20
             1 05-APR-13          0
             1 08-APR-13         12       6.71        11.75
             1 09-APR-13          8       5.71        10.00
             1 10-APR-13          6       3.71         6.50
             1 12-APR-13         10       5.14         9.00
             1 13-APR-13          0       5.14         7.20
             1 14-APR-13          0       5.14         6.00
             1 15-APR-13         10       4.86         5.67
             1 16-APR-13          5       4.43         5.17
             1 17-APR-13          2       3.86         4.50
             1 20-APR-13          3       2.86         4.00
             2 03-APR-13         12
             2 05-APR-13         15
             2 06-APR-13          5
             2 07-APR-13          7       5.57         9.75
             2 09-APR-13         10       7.00         9.80
             2 11-APR-13          5       6.00         8.40
             2 12-APR-13          0       3.86         5.40
             2 13-APR-13          0       3.14         4.40
             2 15-APR-13          6       3.00         4.20
             2 16-APR-13          8       2.71         3.80
             2 17-APR-13          0       2.71         3.17
             2 18-APR-13         10       3.43         4.00
             2 19-APR-13          5       4.14         4.83
    Message was edited by: FrankKulash
    Sorry; I meant to reply to OP, not to Greg

  • [Solved] get records with a date of 11/6/2007 I have to add -1 [Solved]

    Hello - really simple question here, I think anyway.
    I have this line in my query -
    WHERE table2.trans_date_time BETWEEN :p_from_date -1 AND :p_to_date + 1
    Notice the -1 + 1, I had to put this in there because if I wanted all the records that have a date of 11/6/2007, and I enter 11/6/2007 and 11/6/2007 for the variables I get nothing back. But when I added the -1 + 1 it worked.
    Why is this? The table2.trans_date_time column is a DATE data type.
    Thanks!
    Message was edited by:
    computerGal

    Hello compterGal
    I agree with Boneist. What more your where clause statement example, will actually will return all records where the trans_date_time is between 10/06/2007 00:00 and 12/06/2007 00:00 inclusive. That is 2 days and 1 second's worth of data.
    A possible solution if you're looking just for records on a specific day in this senario, but also allow date range capabilities then the following where clause example would be suitable:
    WHERE trunc(table2.trans_date_time) BETWEEN :p_from_date AND :p_to_date
    If the query runs slowly and you have an 8i or later database you can use function indexing to help spead the query up.
    hope this helps.
    Cheers
    Q

  • Get Record count and text of PreparedStatement

    Hi,
    Is it possible to get a count of the records returned in a ResultSet?
    Also, is it possible to get the text of a query(PreparedStatement). I am using a PreparedStatement, and want to know what the actual query looks like, with the parameters. i.e. Are the parameters being set correctly or not.
    Thanks,
    Dewang

    Is it possible to get a count of the records returned
    in a ResultSet?You will have to count them - I don't know of any other way.
    I am using a
    PreparedStatement, and want to know what the actual
    query looks like, with the parameters. i.e. Are the
    parameters being set correctly or not.You will have to use debugging statements (possibly on both sides - in the Java and in your stored procedures if you are using them). You have to assume that your JDBC driver is managing the conversions properly, as long as you match the data types right. It's really pretty straight-forward, just a little tedious.

  • Get records count of all tables

    Hi ,
    I am trying to get the record count of all tables using dynamic query. I don't know how to put the value in placeholder. I tried the below code.
    SET SERVEROUTPUT ON SIZE 1000000
    DECLARE
         CURSOR table_list
         IS
         select OBJECT_NAME from user_objects
         where object_type in ('TABLE')
         and object_name not like '%AUDIT_DDL%'
         AND object_name not like 'MD_%'
         AND object_name not like 'EXT_%'
         AND object_name not like 'STG_%'
         AND object_name not like 'SYS_%'
         AND object_name not like 'TMP_%'
         AND object_name not like 'TEMP_%'
         order by 1;
         v_count     NUMBER :=0;
         query_str VARCHAR2(1000);
    BEGIN
         FOR table_name IN table_list
         LOOP
              query_str :='SELECT COUNT(1) FROM  ' || table_name.OBJECT_NAME;
    dbms_output.put_line(query_str);
              dbms_output.put_line('Table Name:' || table_name.OBJECT_NAME );
              v_count:= execute immediate query_str;
              dbms_output.put_line('Table Name:' || table_name.OBJECT_NAME || ', Count ' || v_count );
         END LOOP;
    END;
    I know I am doing wrong in the bold lines. But not sure how to fix it. Please help. Thanks in advance.

    Hi,
    Welcome to the forum!
    What you posted is basically right, assuming you really want to do dynamic SQL t all.
    The only problem with
    961618 wrote:
              query_str :='SELECT COUNT(1) FROM  ' || table_name.OBJECT_NAME; would be if the object name included special characters (such as single-quotes) or lower-case letters. To avoid any possible problems, I would put the object name inside double-quotes:
    ...     query_str := 'SELECT COUNT (*) FROM "' || table_name.OBJECT_NAME
                                               || '"';
              v_count:= execute immediate query_str;
    The correct syntax is
    execute immediate query_str INTO v_count;V_count will be the number of rows in a single table. Keep another variable (say total_v_count) that keeps the total count so far.
    Do you really need dynamic SQL?
    SELECT     SUM (num_rows)     AS total_rows
    FROM     user_tables
    WHERE     table_name     NOT_LIKE '%AUDIT_DDL%
    AND     ...
    ;gets the same information, accurate as of the last time statistics were gathered, and some of the numbers may be approximate. Depending on how you use the results, that may be good enough for you. If you actually have 10,000,123 rows, and the query says you have 10,000,000, does it really matter?

  • Report from date range

    I am creating a report from a series of inputs using a customizable date range. I need to select a start and end date (i was going to use two calendars for that) and place the data from the range into a pivot table. Any help on how to do this would be
    great.

    use the date picker control, see the thread:
    http://superuser.com/questions/59939/date-selector-popup-in-excel
    KR

  • FM for getting MONTH name from DATE.

    Hi all ,
    We have a requirement to get Month name from the DATE,e.g IF date is 01/01/2008 we need a FM which can generate month name as 'JANUARY'.
    \[removed by moderator\]
    Thanks and Regards
    Kiran
    Edited by: Jan Stallkamp on Jul 30, 2008 4:26 PM

    Hi,
    Here is the code u required...
    REPORT  ZASHU_MONTH_NAMES_GET.
    data:r_code type sy-subrc.
    data:mnames type standard table of T247 with header line.
    CALL FUNCTION 'MONTH_NAMES_GET'
    EXPORTING
       LANGUAGE                    = SY-LANGU
    IMPORTING
         RETURN_CODE                 = r_code
    TABLES
        month_names                 = mnames.
    EXCEPTIONS
      MONTH_NAMES_NOT_FOUND       = 1
      OTHERS                      = 2
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    write:/ mnames.
    Thanks & Regards
    Ashu Singh

  • Getting records based on date entered by the user

    Gurus,
    I have a valid to and valid from dates in ODS. The user is allowed to enter a date on the selection screen. If the user entered date lies in between Valid from and Valid to dates only these records need to be displayed in the report.
    E.g.
    ODS:
    Valid From                   Valid to                       Functional Area
    01/01/2009                  01/01/2099                             X
    01/01/2010                  01/01/2011                             Y
    01/01/2011                  01/01/2012                             Z
    User Entered Date: 01/02/2010
    Report Output:
    X
    Y
    How do i do this? Appreciate your immedite assistance.
    Thanks,
    Ravi Ananthuni

    Hi Ravi,
    You can do this by using cust exit, but that would affect the query performance adversely.
    I would suggest that you use condition in your report.
    Include Valid to and valid from dates in your report layout. Make 2 replacement path type formula variables on these 2 dates.
    And also the date you are entering in selection option, make it a formula variable ready for input.
    Now make a CKF. In the calculation write:  (user date > valid from)AND(user date <valid to)*1.
    And apply a condition to show only those rows where this CKF is 1.
    The replacement path variables will work only when you have those 2 dates in the report layout. If you don't wanna do that , you can make 2 date type KFs and use them directly in calculation.
    I hope this helps.
    Best Wishes,
    Mayank

Maybe you are looking for