Selecting rows based on monthly history

Hi All
I have a table that looks like -
CREATE TABLE orders(store NUMBER, mnth VARCHAR2(12), orders NUMBER);
store     mnth     orders
243272     May-10     17
226798     May-10     20
631638     May-10     14
225779     May-10     10
226675     May-10     9
245593     May-10     13
534299     May-10     9
534440     May-10     12
539313     May-10     14
510617     May-10     11
248196     May-10     9
250472     May-10     9
235854     May-10     16
232899     May-10     12
233840     Apr-10     13
538425     Apr-10     11
338415     Apr-10     12
534336     Apr-10     15
536788     Apr-10     11
441471     Apr-10     11
509317     Apr-10     10
612752     Apr-10     9
338837     Apr-10     11
337869     Apr-10     11
335505     Apr-10     10
333358     Mar-10     11
315854     Mar-10     15
272751     Mar-10     18
270158     Mar-10     10
267979     Mar-10     13
248292     Mar-10     19
244136     Mar-10     14
231837     Mar-10     18
226894     Mar-10     53
The table has records of the number of orders per month for a number of stores, each store has a unique 6 digit id number. Each store can appear multipe times in the report, as they all order a number of times per month.
What I need to do, is apply a number of rules to create four reports -
If a store has more than 8 orders in the most recent month AND none in the next most recent month, include it in a report (eg 9 in March but none in February)
If a store has more than 8 orders for the most recent month AND more than 8 orders in either of the next two most recent months, include it in a report (eg 9+ in March AND 9+ in Jan OR Feb)
If a store has more than 8 orders for the most recent month AND more than 8 orders in either of the next two most recent months AND more than 8 orders in any of the previous 3 months, include it in a report. (eg 9+ in March AND 9+ in Jan or Feb AND 9+ in Oct. OR Nov. OR Dec.)
If a store has more than 8 orders per month, for the 3 most recent months, include it in a report (eg 9+ in Jan, 9+ in Feb, 9+ in March)
I would need to create 4 tables, one for each report, and the only column I would require in each table would be the store code.
I hope this makes sense, if you need any further info please message me. I am using Application Express, something as a pl/sql example woud be great as I am fairly new to all this and am trying to pick it up as I go along..
Thanks!

Hi,
billlagr wrote:
Hi All
I have a table that looks like -
CREATE TABLE orders(store NUMBER, mnth VARCHAR2(12), orders NUMBER);Thanks for posting the CREATE TABLE statement; that's very helpful.
store     mnth     orders
243272     May-10     17
226798     May-10     20
631638     May-10     14
...It would also be helpful if you posted the data as INSERT statements, for example:
INSERT INTO orders (store, mnth, orders) VALUES (243272, 'May-10', 17);Also, post the results you want from that data.
Data that represents a point in time, such as your mnth column, should be stored in a DATE column.
The table has records of the number of orders per month for a number of stores, each store has a unique 6 digit id number. Each store can appear multipe times in the report, as they all order a number of times per month.
What I need to do, is apply a number of rules to create four reports -
If a store has more than 8 orders in the most recent month AND none in the next most recent month, include it in a report (eg 9 in March but none in February)What is "the most recent month"? I assume you mean the last full month before the report is run. For example, if you create the report on June 2, 2010, then the "most recent month" is may, 2010.
Here's one way to do the first report:
WITH     all_months     AS
     SELECT     LEVEL               AS mnth_num
     ,     ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                  , -LEVEL
                  )          AS mnth_begin_date
     FROM     dual
     CONNECT BY     LEVEL     <= 2
SELECT       o.store
,       NVL (SUM (CASE WHEN m.mnth_num = 1 THEN o.orders END), 0)     AS mnth_1_total
,       NVL (SUM (CASE WHEN m.mnth_num = 2 THEN o.orders END), 0)     AS mnth_2_total
FROM       orders     o
JOIN       all_months     m     ON     TO_DATE (o.mnth, 'Mon-RR')     = mnth_begin_date
GROUP BY  o.store
HAVING       NVL (SUM (CASE WHEN m.mnth_num = 1 THEN o.orders END), 0)     > 8
AND       NVL (SUM (CASE WHEN m.mnth_num = 2 THEN o.orders END), 0)     = 0
ORDER BY  o.store
I would need to create 4 tables, one for each report, and the only column I would require in each table would be the store code.You don't need to create any tables to get the results you want, but if you really want to make tables out of the result sets, use CREATE TABLE AS.
Unfortunately, you can't use a column alias (like mnth_2_total) in the GROUP BY clause of a query if it was defined in the SELECT clause of the same query. That's why I repeated expressions such as <tt>NVL (SUM (CASE WHEN m.mnth_num = 2 THEN o.orders END), 0)</tt>. You may want to define the aliases in a sub-query, so you can use them in a su[per-query, like this:
{code}
WITH     all_months     AS
     SELECT     LEVEL               AS mnth_num
     ,     ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
               , -LEVEL
               )          AS mnth_begin_date
     FROM     dual
     CONNECT BY     LEVEL     <= 2
,     got_aliases     AS
     SELECT     o.store
     ,     NVL (SUM (CASE WHEN m.mnth_num = 1 THEN o.orders END), 0)     AS mnth_1_total
     ,     NVL (SUM (CASE WHEN m.mnth_num = 2 THEN o.orders END), 0)     AS mnth_2_total
     FROM     orders     o
     JOIN     all_months     m     ON     TO_DATE (o.mnth, 'Mon-RR')     = mnth_begin_date
     GROUP BY o.store
SELECT     store
,     mnth_1_total
,     mnth_2_total
FROM     got_aliases
WHERE     mnth_1_total     > 8
AND     mnth_2_total     = 0
ORDER BY store
You may want to create a view that is like got_alaises, but goes back 6 months, and base all four reports on that view.
Edited by: Frank Kulash on Jun 2, 2010 6:14 AM
Added version with got_aliases                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Select records based on monthly anniversary date

    Hi,
    I have a table with a date_added field and I want to select records based on the monthly anniversary date of this field.
    eg. ID, Date_added
    1, 10-DEC-2012
    2, 11-NOV-2012
    3, 10-MAR-2012
    4, 28-FEB-2012
    5, 30-DEC-2012
    So For the 10th of Jan 2013, I would want to return records 1 and 3 only
    I started looking at the extract function, but this soon falls down for records at the end of the month. For example, on the 28th Feb, I would also want to include records where the date_added day is the 29th, 30th or 31st. So, in the table above I would want to return records 4 and 5, but extract would only return 4.
    Is there a simple function to do this month anniversary query - am I missing something very obvious? Or, do I need to write a query to explicitly cope with dates at the end of the month? So far I haven't found a sensible simple solution!
    I'm using 11g
    thanks

    I didn't look into leap year, but this should give you a starting point:
    select  *
      from  t
      where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
                  when to_date(:target_date,'mmddyyyy')
                    then case
                           when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
                             then 1
                         end
                  else case
                           when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
                             then 1
                         end
                end
    /For example, target date is 1/10/2013:
    SQL> variable target_date varchar2(8)
    SQL> exec :target_date := '01102013';
    PL/SQL procedure successfully completed.
    SQL> with t as (
      2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
      3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
      4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
      5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
      6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
      7            )
      8  select  *
      9    from  t
    10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
    11                when to_date(:target_date,'mmddyyyy')
    12                  then case
    13                         when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
    14                           then 1
    15                       end
    16                else case
    17                         when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
    18                           then 1
    19                       end
    20              end
    21  /
            ID DATE_ADDE
             1 10-DEC-12
             3 10-MAR-12
    SQL> And target date is 2/28/2013:
    SQL> exec :target_date := '02282013';
    PL/SQL procedure successfully completed.
    SQL> with t as (
      2             select 1 id,to_date('10-DEC-2012','dd-mon-yyyy') date_added from dual union all
      3             select 2,to_date('11-NOV-2012','dd-mon-yyyy') from dual union all
      4             select 3,to_date('10-MAR-2012','dd-mon-yyyy') from dual union all
      5             select 4,to_date('28-FEB-2012','dd-mon-yyyy') from dual union all
      6             select 5,to_date('30-DEC-2012','dd-mon-yyyy') from dual
      7            )
      8  select  *
      9    from  t
    10    where 1 = case last_day(to_date(:target_date,'mmddyyyy'))
    11                when to_date(:target_date,'mmddyyyy')
    12                  then case
    13                         when to_char(date_added,'dd') >= to_char(to_date(:target_date,'mmddyyyy'),'dd')
    14                           then 1
    15                       end
    16                else case
    17                         when to_char(date_added,'dd') = to_char(to_date(:target_date,'mmddyyyy'),'dd')
    18                           then 1
    19                       end
    20              end
    21  /
            ID DATE_ADDE
             4 28-FEB-12
             5 30-DEC-12
    SQL> SY.

  • Select rows based on condition of same row

    Hello,
    I have the following table structure and rows defind here
    http://sqlfiddle.com/#!4/3f474/3
    I would like to select rows if PRODUCT_NO is null then I need to select rows which are having IS_PAYABLE='Y'. If PRODUCT_NO is not null
    then IS_PAYABLE will be 'N' or IS_PAYABLE will be null.
    E.g.
    For PRODUCT_REG HPO_FABRIC, one PRODUCT_NO null so I need select that row only, not the other two rows.
    I can do
    select * from SUPPLIER_DETAILS where  IS_PAYABLE='Y', but what if there are many records with PRODUCT_NO having null or not null
    How can I do this using sql?

    This simple query!!!
    select *
      from supplier_details
    where (product_no is null
        and is_payable = 'Y')
       or (product_no is not null and is_payable is null);
    PRODUCT_REG                                                                                          PRODUCT_SUPPLIER_CODE PRODUCT_NO START_DATE                END_DATE                  IS_PAYABLE
    HPO_FABRIC                                                                                           JP_008                           01-AUG-01                 31-AUG-01                 Y         
    HPO_FABRIC                                                                                           JP_008                STGA-FABR1 16-AUG-01                 31-AUG-01                            --Edited:- Modified Query and Results for 2nd Requirement
    Edited by: Purvesh K on Sep 17, 2012 4:55 PM
    --Removed NVL
    Edited by: Purvesh K on Sep 17, 2012 5:04 PM

  • Selecting records based on month and year parameters

    Hi. I have a sql 2008 r2 stored procedure which needs modifying to return the data based on a start / end month and year.
    It's a large SP so I'll summarise - It accepts four parameters:
    @StartMonth NVARCHAR(10)
    @StartYear NVARCHAR(4)
    @EndMonth NVARCHAR(10)
    @EndYear NVARCHAR(4)
    The current WHERE clause is:
    WHERE ta.TimeByDay BETWEEN '01' + '-' + ltrim(LEFT(@StartMonth, 3)) + '-' + @StartYear
    AND convert(nvarchar,datediff(day, ta.TimeByDay, dateadd(month, 1, ta.TimeByDay))) + '-' + ltrim(LEFT(@EndMonth, 3)) + '-' + @EndYear
    Example of input parameters:
    @StartMonth = N'January',
    @StartYear = N'2014',
    @EndMonth = N'February',
    @EndYear = N'2014',
    Result:
    The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
    (1 row(s) affected)
    However it executes correctly if we do either of the following:
    1) Run the SQL direct in QA and type in January and February rather than passing in the Start/End Month parameters
    2) As you can see we use the following datediff call to get the number of days per month. IF I replace this with '28', or '31' for example the query also runs (oddly number 1 above then also runs by executing the SP):
    convert(nvarchar,datediff(day, ta.TimeByDay, dateadd(month, 1, ta.TimeByDay)))
    How do I update the WHERE clause to return records between a start/end month and year?
    I'm a day on this so any help appreciated.
    Thanks

    create function NthDayOfMonth (@year int, @month smallint, @weekday varchar(15), @nth smallint)
    returns datetime
    as
    begin
    declare @the_date datetime, @c_date datetime, @cth smallint
    set @cth = 0
    set @c_date = convert(varchar,@year)+'-'+convert(varchar,@month)+'-01'
    while month(@c_date) = @month
    begin
    if datename(weekday,@c_date) = @weekday set @cth = @cth + 1
    if @cth = @nth and datename(weekday,@c_date) = @weekday set @the_date = @c_date
    set @c_date = dateadd(day,1,@c_date)
    end
    return @the_date
    end
    go
    create function Dates(@date datetime)
    returns @table table
    now datetime,
    today datetime,
    Month_start datetime,
    Month_end datetime,
    Prev_Month_Start datetime,
    Prev_Month_End datetime,
    Week_Start datetime,
    Week_End datetime,
    Prev_Week_Start datetime,
    Prev_Week_End datetime,
    Quarter_Start datetime,
    Quarter_End datetime,
    Prev_Quarter_Start datetime,
    Prev_Quarter_End datetime,
    Year_Start datetime,
    Year_End datetime,
    Prev_Year_Start datetime,
    Prev_Year_End datetime,
    Month_End_TS datetime,
    Prev_Month_End_TS datetime,
    Week_End_TS datetime,
    Prev_Week_End_TS datetime,
    Quarter_End_TS datetime,
    Prev_Quarter_End_TS datetime,
    Year_End_TS datetime,
    Prev_Year_End_TS datetime,
    Year smallint,
    Month smallint,
    Day smallint,
    Month_Name varchar(15),
    Day_Name varchar(15),
    WD smallint
    as
    begin
    if @date IS NULL set @date = getdate()
    insert into @table
    select
    @date as now,
    convert(datetime,convert(varchar,@date,101)) as today,
    dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101))) as Month_Start,
    dateadd(day,-1,dateadd(month,1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101))))) as Month_end,
    dateadd(month,-1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101)))) as Prev_Month_start,
    dateadd(day,-1-day(@date)+1,convert(datetime,convert(varchar,@date,101))) as Prev_Month_End,
    dateadd(day,1-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Week_Start,
    dateadd(day,7-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Week_End,
    dateadd(day,-6-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Prev_Week_Start,
    dateadd(day,0-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Prev_Week_End,
    convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01') as quarter_start,
    dateadd(day,-1,dateadd(quarter,1,convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01'))) as quarter_end,
    convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01') as prev_quarter_start,
    dateadd(day,-1,dateadd(quarter,1,convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01'))) as prev_quarter_end,
    dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))) as Year_Start,
    dateadd(year,1,dateadd(day,0-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Year_End,
    dateadd(year,-1,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Prev_Year_Start,
    dateadd(year,-1,dateadd(year,1,dateadd(day,0-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))))) as Prev_Year_End,
    dateadd(ms,-3,dateadd(day,0,dateadd(month,1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101)))))) as Month_End_Ts,
    dateadd(ms,-3,dateadd(day,-1-day(@date)+2,convert(datetime,convert(varchar,@date,101)))) as Prev_Month_End_TS,
    dateadd(ms,-3,dateadd(day,8-datepart(dw,@date),convert(datetime,convert(varchar,@date,101)))) as Week_End_TS,
    dateadd(ms,-3,dateadd(day,1-datepart(dw,@date),convert(datetime,convert(varchar,@date,101)))) as Prev_Week_End_TS,
    dateadd(ms,-3,dateadd(day,0,dateadd(quarter,1,convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01')))) as quarter_end_TS,
    dateadd(ms,-3,dateadd(day,0,dateadd(quarter,1,convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01')))) as prev_quarter_end_TS,
    dateadd(ms,-3,dateadd(year,1,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))))) as Year_End_TS,
    dateadd(ms,-3,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Prev_Year_End_TS,
    Year(@date) as Year,
    Month(@date) as Month,
    Day(@Date) as Day,
    datename(month,@Date) as Month_Name,
    datename(WEEKDAY,@date) as Day_Name,
    datepart(weekday,@date) as WD
    return
    end
    go
    create function Holidays(@year smallint)
    returns @table table
    date datetime,
    type varchar(10),
    name varchar(25)
    as
    begin
    insert into @table
    select convert(datetime,convert(varchar,@year)+'-01-01') as date,'Holiday' as type ,'New Years Day' as name UNION ALL
    select dbo.NthDayOfMonth(@year,2, 'Monday',3),'Holiday','Family Day' UNION ALL
    select dateadd(d,0-case when datepart(weekday,convert(varchar,@year)+'-05-25') in (1,2) then 5+datepart(weekday,convert(varchar,@year)+'-05-25') else datepart(weekday,convert(varchar,@year)+'-05-25')-1 end, convert(varchar,@year)+'-05-25') ,'Holiday','Victoria Day' UNION ALL
    select convert(varchar,@year)+'-01-07' ,'Holiday','Canada Day' UNION ALL
    select dbo.NthDayOfMonth(@year,8, 'Monday',1),'Holiday','Civic Holiday' UNION ALL
    select dbo.NthDayOfMonth(@year,9, 'Monday',1),'Holiday','Labour Day' UNION ALL
    select dbo.NthDayofMonth(@year,10,'Monday',2),'Holiday','Thanksgiving' UNION ALL
    select convert(varchar,@year)+'-11-11' ,'Holiday','Rememberance Day'UNION ALL
    select convert(varchar,@year)+'-12-25' ,'Holiday','Christmas Day' UNION ALL
    select convert(varchar,@year)+'-12-26' ,'Holiday','Boxing Day'
    update @table
    set date =
    case when name != 'Boxing Day' and datepart(weekday,date) = 7 then dateadd(day,2,date)
    when name != 'Boxing Day' and datepart(weekday,date) = 1 then dateadd(day,1,date)
    when name = 'Boxing Day' and datepart(weekday,date) = 7 then dateadd(day,2,date)
    when name = 'Boxing Day' and datepart(weekday,date) = 1 then dateadd(day,2,date)
    when name = 'Boxing Day' and datepart(weekday,date) = 2 then dateadd(day,1,date)
    else date
    end
    return
    end
    go
    Using these functions (in place of a calendar table) you could do something like this:
    DECLARE @forumTable TABLE (sales MONEY, saleDate DATE)
    INSERT INTO @forumTable (sales, saleDate)
    VALUES
    (123.45, '2014-01-05'),(678.90, '2014-01-06'),(111.21, '2014-01-07'),(314.15, '2014-01-08'),(161.71, '2014-01-09'),
    (819.20, '2014-02-05'),(212.22, '2014-02-06'),(324.25, '2014-02-07'),(262.72, '2014-02-08'),(829.30, '2014-02-09')
    SELECT SUM(f.sales), d.month_end
    FROM @forumTable f
    CROSS APPLY sandbox.dbo.dates(f.saleDate) d
    GROUP BY d.month_end

  • Crosstabs- Selecting Rows based on parameters

    Does anyone know a way to display or not display rows in a crosstab based on parameters? I am not looking to suppress rows, rather just include or exclude.
    What I am trying to do is replicate a webfocus report that allows the user to check a checkbox to show or not show items such as fiscal year, period, item, etc.
    Any assistance with this would be very much appreciated.

    I'm not sure about it! but there might be a workaround. Can you try this and see whether it helps you?
    Suppose I have 3 fields like country,region,city and I have inserted a crosstab and inserted year field in column and inserted sales amount as a summary field. For the rows I tried creating a static parameter with values country,region,city
    Now create formula 1 as
    if {?parameter}="country" then {country field}
    formula 2 as
    if {?parameter}="region" then {region field}
    formula 3 as
    if {?parameter}="city" then {city field}
    Now I have inserted all these 3 formula as 3 different rows in crosstab
    I hope this might help you upto some extent!!!
    Raghavendra

  • How to select rows based on values from other table?

    I need to select all rows from TABLE1 whose time column is NOT between fromTime and toTime from TABLE2.
    For example, those table contain following rows:
    TABLE1
    |id |time |...
    |1 | 10 |...
    |2 | 20 |...
    |... | ... |...
    TABLE2
    |fromTime|toTime|
    | 3 | 6 |
    | 10 | 16 |
    | 20 | 25 |
    So, in the case above the first and second rows shouldn't be returned by the query since 10<=10<=16 AND 20<=20<25.

    Try
    -- code #1
    SELECT id, time
    from TABLE1 as T1
    where not exists (SELECT * from TABLE2 as T2
    where T1.time between T2.fromTime and T2.toTime);
    José Diz     Belo Horizonte, MG - Brasil

  • Selecting rows based on one column change

    Hi All -
    I need to select the rows from a table (this table has 4 columns and has a primary key and have many records for that primary key).
    I need to select all the rows for this primary key for which only one column (in this case "flag" column) has been changing from its past value.
    How can I do this?
    for example, table data is:
    ID, flag, Profile,date
    pk, Y, Profile4,01/10/10
    pk, N, Profile4, 01/08/10
    pk,N, Profile3, 01/07/10
    pk,Y, Profile3, 01/05/10
    pk,Y, Profile2, 01/03/10
    pk,N, Profile1, 01/02/10
    The output of the query I am looking for should return the following i.e. only retrieve rows whose previous flag value has been changed:
    ID, flag, Profile,date
    pk, Y, Profile4,01/10/10
    pk,N, Profile3, 01/07/10
    pk,Y, Profile2, 01/03/10
    pk,N, Profile1, 01/02/10
    Please share your expertise.
    Thanks,
    Seenu

    Hi, Seenu,
    Centinul wrote:
    It is always helpful to provide the following:
    1. Oracle version (SELECT * FROM V$VERSION)
    2. Sample data in the form of CREATE / INSERT statements.
    3. Expected output
    4. Explanation of expected output (A.K.A. "business logic")
    5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details.
    Seenu001 wrote:(1) Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE 10.2.0.3.0 Production
    TNS for Solaris: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production Good!
    (2) select 'pk' id,'Y' flag,'Profile4' profile,to_date('01/10/10','mm/dd/yy') dt from dual union all
    select 'pk','N','Profile4',to_date('01/08/10','mm/dd/yy') from dual union all
    select 'pk','N','Profile3',to_date('01/07/10','mm/dd/yy') from dual union all
    select 'pk','Y','Profile3',to_date('01/05/10','mm/dd/yy') from dual union all
    select 'pk','Y','Profile2',to_date('01/03/10','mm/dd/yy') from dual union all
    select 'pk','N','Profile1',to_date('01/02/10','mm/dd/yy') from dualNot bad. It can be made into a CREATE TABLE AS statement without a lot of work, but if you're not willing to do that little bit or work, why should anyone else?     
    (3) Expected output: I want all the rows from this table where the flags have been changed from its previous value.That's a vague description. Post the exat output, like Solomon did:
    ID F PROFILE  DT
    pk N Profile1 01/02/2010
    pk Y Profile2 01/03/2010
    pk N Profile3 01/07/2010
    pk Y Profile4 01/10/2010
    (4) I want all the rows from this table where the flags have been changed from its previous value.That's a repeat of the vague description. Explain how you get the crorrect results, with specific examples, like this:
    "The row dated January 2 should be included, because it is the oldest row in the table, regardless of what its flag is.
    The row dated January 3 should be included because ...
    The row dated January 5 should *not* be included, because ..."
    You don't have to explain every row, just every reason.
    Solomon - I tried your query but the prev_flag is not being populated, it shows null value '-'NULL is not the same as '-'. Did you issue a command like
    SET NULL "-"that would display a '-' instead of NULL?
    Solomon's query works fine for me, as it did for him. Post your code, even if you just copied it from this site. (There may have been an editing problem.)
    Frank - I tried your query, prev_flag is populated in this case but when I am using with the WITH clause, I am getting an error saying "unsupported column aliasing" , I guess WITH command doesnt allow use of column alias. The problem is that I forgot the keyword AS after the sub-query name.
    WITH     got_prev_flag     AS
         SELECT     id, flag, profile, dt     -- DATE is not a good column name
         ,     LAG (flag, 1, '??') OVER (ORDER BY dt)  AS prev_flag
         FROM     t
    --     WHERE     ...     -- any filtering goes here
    SELECT     id, flag, profile, dt
    FROM     got_prev_flag
    WHERE     flag     != prev_flag
    ;If you don't post data in an executable form, then people can't test their ideas. If people can't test their ideas, trivial mistakes like this are very likely.
    This flag column will have either
    Y or N columns. Could you tell me what is the use of "??" value.By default, LAG returns NULL for the first row. If you want to include the first row in the results, then "WHERE     flag     != prev_flag" won't work when prev_flag is NULL. We could explicitly check for "prev_flag IS NULL", but I find it cleared to override the default, and have LAG return '??' on the first row. I guessed that '??' would make the condition "WHERE     flag     != prev_flag" work, no matter what value flag had. There's nothing magical about '??'; you can use any string that will never actually match flag.

  • How to get selected Row Index in a table based ona  VO?

    Hi All,
    I'm writing an ADF form wherein I use a VO based on a SQL query. I'd like to know how to get the index of a selected row. I havea selection Listener in place where I can print the selected Row's data using getSelectedRowData().toString() on the table.
    How can I get certain Attributes from this selected row.
    One solution I thought of is to get the row index and then read attributes. But I cant seem to figure out how to get rowIndex for a selected row. Any sugestions?
    Using JDeveloper 11g.
    Thanks
    P.

    If your selected row is marked as current row you can use
    // Get a attribute value of the current row of iterator
    DCIteratorBinding iterBind= (DCIteratorBinding)dcBindings.get("testIterator");
    String attribute = (String)iterBind.getCurrentRow().getAttribute("field1");Where 'testIterator' is the name of the iterator you use for the table and 'field1' is the name of an attribute you want to get.
    Or you can iterate over the selected row keys (even if it's only one):
    // get  selected Rows of a table 2
    for (Object facesRowKey : table.getSelectedRowKeys()) {
    table.setRowKey(facesRowKey);
    Object o = table.getRowData();
    JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)o;
    Row row = rowData.getRow();
    TestRow testRow = (TestRow)((DCDataRow)row).getDataProvider() ;
    }Where TestRow is the row type of the VO of your table.
    Timo

  • How can I select and delete rows based on the value in one column?

    I searched through the discussion board, and found a thread on deleting blank rows, but not sure how to modify it to work with my issue.
    I have put together a rather complicated spreadsheet for designing control systems, it calculates parts needed based on check boxes selected in a second spreadsheet.
    Since not all systems require all parts there are many rows that have a 0 quantity value, I would like to select these rows and delete them once I have gone through the design phase (checking off required features on a separate sheet).
    I like the way the other thread I found will gather all the blank rows at the bottom without changing the order of the rows with data in them.
    I don't understand exactly how the formula in the other thread works well enough to modify it to look for a certain column.
    I hope I made myself clear enough here, to recap, I would like to sort the rows based on a zero value in one (quantity) column, move them (the zero quantity rows) to the bottom of the sheet, and then delete the rows with a zero quantity (I can delete them manually, but would like to automate the sorting part).
    Thanks for any help anyone can provide here.
    Danny

    I apologize but, as far as I know, Numbers wasn't designed by Ian Flemming.
    There is no "this column will be auto-destructing after two minutes"
    You will have to use your fingers to delete it.
    I wish to add a last comment :
    if your boss has the bad habit to look over your shoulder, it's time to find an other one.
    As I am really pig headed, it's what I did. I became my own boss so nobody looked over my shoulder.
    Yvan KOENIG (VALLAURIS, France) mercredi 13 juillet 2011 20:30:25
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8
    Please : Search for questions similar to your own before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • How to HIde rows based on User Selection

    I have a requirement where in I have to hide some rows based on Plant Name selected by USer at the time of opening the report.
    Here is the screen  shot:-
    Here I am seeing two Tag Types MACT and Average when User selects  Luling Plant. But When User selects some other Plant I don't want to see Tag Type 'MACT' in this table.
    We have a predefine filter for Plant.
    How can I achieve that. The data in White Rows is Average values.
    Please help.
    Regards

    Hi Shalini,
    Is user selecting Plant Name from the report prompt. If Yes, then try below-
    Create a variable-
    FilterSelect= If UserResponce("WritePromptTextHere") =" Luling Plant" and TagType inList ("MACT";"Average") Then 1 ElseIf  UserResponce("WritePromptTextHere") <>" Luling Plant" and TagType inList ("MACT";"Average") Then 3 Else 2
    Apply Filter on "FilterSelect" variable-
    FilterSelect=1 and 2
    ~Anuj

  • How to variable exit read table based on selected row on weblayout ??

    Hi All,
    Greeting,
    I have a question regarding IP.
    I have report where I can choose the line. I've already been able to catch the selected row there using ABAP Planning Function.
    The requirement is to change some value from other customized table ( not info cube ) based on selected row.
    So when user's selecting data and pressing a button, the idea is to prompt a variable where they can see the old value, and they can entry the new value. But to query that value, I need to select based on the values the row.
    My Question is how user-exit in variable can read selected row in web planning layout especially it happens when I_step = 1 ?
    Or the other idea is to get the value on that customized table and put it in text web item as the old value. How can I fetch data from table and put it in text web item ?
    Thanks a lot and have a good day,

    Hi.
    My Question is how user-exit in variable can read selected row in web planning layout especially it happens when I_step = 1 ?
    I think there is no way to do it. I_STEP=1 called before the report shows the data.
    But may be the next approach could help you:
    1. when you select line you can use command for populating variables according to selected line. let's say yohave selected line with costcenterr XXX so you can populate variable (let's say Z_CC) with selected value.
    2. create FOX that reads variable Z_CC value and calls any function module with customized table and variable value (you can use CALL FUNCTION statement within FOX and pass to it variable value).
    This is just an idea of some direction - may be you can take it and develope to required result.
    Regards.

  • Total based on selected rows

    Hi,
    I have a column called record type and I would like to sub-total based on
    selected rows from the record type at the bottom. how do I do it ?
    for ex:
    rtype load
    s - 100
    b - 150
    r - 200
    z - 10
    total load1 b+r - 350
    total load2 b+z - 160
    ------------------------------------------

    You can use calculations in pivot tables to operate with distinct values of a column. You can create a new column (or row) as 'b' + 'r' record types. You can select wether to see original and total columns or only totals.
    See 'Examples of Calculations in Oracle BI Pivot Tables' section in "Oracle® Business Intelligence Answers, Delivers, and Interactive Dashboards User Guide" document.

  • How to get tables the size should be based on 3 months history.

    Hi,
    I am junior DBA, Actually, I want to get the details of columns D,E,F and G. For employee and dept tables the size should be based on 3 months history.
    How to get pls suggest me.
    Regards,
    Venkat.
    Junior DBA

    Venkat wrote:
    Hi,
    I am junior DBA, Actually, I want to get the details of columns D,E,F and G. For employee and dept tables the size should be based on 3 months history.
    How to get pls suggest me.
    Regards,
    Venkat.
    Junior DBAWhat are columns D,E,F, and G? Size of what is based on three months history of what?? How does this size of an unknown entity relate to columns D,E,F and G?

  • Pop up based on selected rows

    Hi,
    I have a multi-row select report with checkboxes. I would like to have it so you can check off your desired rows and click a button to have another window popup with the row information displayed in a different layout. The reason I am doing this is because I want to print specific rows (3 at a time, actually) in a particular format (ie not horizontally). I can get this to work with static fields in the region but cannot make the leap to multi-row select.
    I'm thinking it's a combination of passing htmldb_application.g_f01 to pick the checked rows and then have javascript loop through the values in the report and place them in fields on the pop up. I found other postings, but they were for update of the multiple rows: I'm looking to just copy the text from the rows themselves to another window.
    Is this possible?
    I want a specific format to print out data in non-row fashion for a select set of data.
    Does this make sense?
    Thank you,
    Dean

    I believe I was getting caught up because I was trying to mix server side and client side functions.
    I ended up doing two things slightly different.
    Created a new small table to house my selections:my_print.
    I had check boxes on the rows and an after submit process that would delete the rows in the selection table and insert the key values of those selected.
    BEGIN
    DELETE from MY_PRINT;
    FOR i in 1..HTMLDB_APPLICATION.G_F01.count
    LOOP
    INSERT INTO MY_PRINT (CARD_ID) VALUES (HTMLDB_APPLICATION.G_F01(i));
    END LOOP;
    END;
    Then when I added a Submit "Print" button that took you to another page. That page was a print-friendly formatted report which selected the rows off the my_print keys. To get the format I ended up modifying the report template behind the page. This required a little trial and error with the different template types but I got what I wanted with a vertical named template and custom HTML tagging on the Row Template to build the page like a table.
    It's all server side now and loads within the page, rather than a pop-up.
    It accomplished the ultimate goal of being able to print selected rows in a format I liked. Plus, doing it this way was way easier than trying to code it all with a pop-up.
    good luck
    dean

  • Insert new rows based on user selection on a table display on the screen

    Hi..
    In my requirement i need to display the line items of a PO# to the user on the screen for specific fields. Each row should also include an additonal checkbox when displayed for the user. When the user checks this check box or clicks on it a new row should be inserted below to that row with the existing data of that row being copied to newly inserted row and allowing the user to make any changes.
    The newly inserted row should also include a check box , so that when the user checks it again a new row should get inserted. Finally what ever data user enters on the screen, i should be able to update my internal table with those new values and records.
    Appreciate if anyone can guide me on how to proceed on this or any alternative approaches.
    Will reward helpful answers.
    Thanks.

    Hi ..
    Can you please be more detailed. First I need to know how to create the initial table display for the existing line items and then the techniques for inserting the new rows based on the check marks and finally add those news rows to my existing internal table..
    Appreciate ur help.
    Thanks.

Maybe you are looking for

  • Display data vertically using sap script

    Hi Developers, Is there any way to display data vertically in a form using sap script? thanx in advance, binu

  • Table name for BW Query Where used list

    Hi, I am looking for Table name where the information of  " where used list " of a BW query stores. Any help ? Thanks Satya

  • Kernal How do I upgrade to a version below latest.

    HI. I just installed Oracle Linux 5.1 straight from CD, But I need to be running kernel-2.6.18-53.1.19.0.1.el5.x86_64, This is not the latest and u2date will just upgrade me to the latest version. I know I need the below files, I found this in a Tech

  • Email Button

    How would I create a button that opens up the default email app. with the email address of the person in the "To" box in Catalyst?  In other programs you can put goto email in an action window, does it work the same in Catalyst?

  • Status of the transfer orders

    i have a delivery document if i see the document flow of that delivery document i found there are 4 WMS Transfer orders in that 4 transfer orders 2 are with status completed and 2 are with status open i am an ABAPER in my report i need only transfer