Using Min and MAX function on Bit Field

Hi All,
I am trying to write a SQL Query where i want to aggregate a bit field along with some  other numerical fields
I  have a Field of type Bit and it can contain data like
Scenario 1:              MyBit Field
                   Row1      Null
                  Row2       Null 
                  Row3       Null
Scenario 2:              MyBit Field
                   Row1      1
                   Row2       1
                  Row3       Null
Scenario 3:              MyBit Field
                   Row1      0
                  Row2       0
                  Row3       Null
Scenario 4:              MyBit Field
                   Row1      1
                   Row2       0
                  Row3       Null
Scenario 5:              MyBit Field
                   Row1      1
    Row2       1
                  Row3       0
Scenario 6:              MyBit Field
                   Row1      1
                  Row2       1
                  Row3       1
Scenario 7:              MyBit Field
                   Row1      0
                   Row2       0
                  Row3       0
These are all scenario i can think of, i want to have sql statement which should return me
null if all values are null,
if there is mix of 1, 0 then return NA,
if there is mix of 1,0,null return NA
if there is mix of 1's with Null then return 1 ,
if there is mix of 0's with null return 0 ,
if there is all 0's return 0
if there is all 1's return 1
IS there an elegant way in SQL to handle this
Note : I have other fields in query which are simply getting SUM.

Try:
DECLARE @T table (
Scenario int NOT NULL,
col1 int NOT NULL,
col2 bit NULL,
UNIQUE CLUSTERED (Scenario, col2, col1)
INSERT INTO @T
(Scenario, col1, col2)
VALUES
(1, 1, NULL),
(1, 2, NULL),
(1, 3, NULL),
(2, 1, 1),
(2, 2, 1),
(2, 3, NULL),
(3, 1, 0),
(3, 2, 0),
(3, 3, NULL),
(4, 1, 0),
(4, 2, 1),
(4, 3, NULL),
(5, 1, 1),
(5, 2, 1),
(5, 3, 0),
(6, 1, 1),
(6, 2, 1),
(6, 3, 1),
(7, 1, 0),
(7, 2, 0),
(7, 3, 0);
WITH T AS (
SELECT
Scenario,
MIN(col2 * 1) AS min_col2,
MAX(col2 * 1) AS max_col2,
COUNT(*) AS cnt
FROM
@T
GROUP BY
Scenario,
col2
, R AS (
SELECT
Scenario,
MIN(min_col2) AS min_col2,
MAX(max_col2) AS max_col2,
MAX(cnt) AS max_cnt,
SUM(cnt) AS total
FROM
T
GROUP BY
Scenario
SELECT
Scenario,
CASE
WHEN max_cnt = total THEN CAST(min_col2 AS char(1))
WHEN min_col2 = 1 AND max_cnt < total THEN CAST(min_col2 AS char(1))
WHEN max_col2 = 0 AND max_cnt < total THEN CAST(max_col2 AS char(1))
WHEN min_col2 <> max_col2 AND max_cnt < total THEN 'NA'
END AS col3
FROM
R;
GO
AMB
Some guidelines for posting questions...
AYÚDANOS A AYUDARTE, guía básica de consejos para formular preguntas

Similar Messages

  • Min and max functions

    Hi,
    I need to perform a calculation:
    (max(min(C,2.375),0)+max(min(Y,2.375),0))/6
    if the value of c is less than 2.375 then it has to accept the value of C else 2.375.
    on it i need to perform the max operation.
    How can i do this?

    Hi,
    MIN and MAX, like all the aggrgate functions, take only one argument.
    Perhaps you're thinking of the row functions LEAST and GREATEST, which can take one or more arguments.
    For example:
    GREATEST ( LEAST ( c
                     , 2.375
             , 0
             )will return
    2.375 if c is greater than 2.375,
    0 if c is less than 0, or
    c otherwise.
    CAUTION: Unlike the aggregate functions, that ignore NULLs, GREATEST and LEAST will return NULL if any of their arguments are NULL.

  • Performance Issue using min() and max() in one SQL statement

    I have a simple query that selects min() and max() from one column in a table in one sql statment.
    The table has about 9 Million rows and the selected column has a non unique index. The query takes 10 secs. When i select min() and max() in separate statements, each takes only 10 msecs:
    This statement takes 10 secs:
    select min(date_key) , max(date_key)
    from CAPS_KPIC_BG_Fact_0_A
    where date_key != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
    This statement takes 10 msecs:
    select min(date_key)
    from MYTABLE
    where date_key != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
    union all
    select max(date_key) from MYTABLE
    Because the first statement is part of an autmatic generated SQL of an application, i can't change it and i have to optimize the data model. How can i speed up the first statement?

    I've ran similar query on a table that has 10 milliion rows, with an index on the date column
    This is what I have found:
    SQL> set timing on
      1  SELECT MIN(ID_DATE) MIN_DATE, MAX(ID_DATE) MAX_DATE
      2      FROM MY_DATE
      3*     WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
    SQL> /
    MIN_DATE  MAX_DATE
    03-APR-76 06-JAN-02
    real: 43383
    SQL> SELECT MIN(ID_DATE) MIN_DATE FROM MY_DATE
      2   WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
      3  UNION ALL
      4  SELECT MAX(ID_DATE) MAX_DATE FROM MY_DATE
      5   WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD'))
      6  /
    MIN_DATE
    03-APR-76
    06-JAN-02
    real: 20
    SQL> SELECT MIN_DATE, MAX_DATE FROM
      2  (SELECT MAX(ID_DATE) MAX_DATE FROM MY_DATE
      3  WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD')) ) A,
      4  (SELECT MIN(ID_DATE) MIN_DATE FROM MY_DATE
      5  WHERE ID_DATE != TO_DATE(('1900-1-1' ),( 'YYYY-MM-DD')) ) B
      6  /
    MIN_DATE  MAX_DATE
    03-APR-76 06-JAN-02
    real: 10
    SQL> My conculsion, there is nothing you can do to the tables that will improve that particular statement.
    Why can't you modify the application?

  • Avg, Min and Max with a serious twist.....ASO

    Hi,
    I need to calc the Avg, Min and Max on Revenue. That's simple. However Every row of data the revenue falls into a bucket/range. For example revenue is in buckets '0 to 50' or '51 to 100' or '101 to 150' lets say. As I load data I load a '1' into a 'Count' measure depending on which bucket that row of data falls into. So I can get a total count for each bucket. So '0 to 50' might have a Count of 3. '51 to 100' might also have 3 and '101 to 150' has a 2. So my range count looks like:
    0 to 50 - 3
    51 to 100 - 3
    101 to 150 -2
    The trick is they only want the Avg, Min and Max on the rows that makeup the highest 'Count'. In this case that's 3. Problem is there are two ranges that meet this criteria. When that happens they want the Min, Max and Avg of the rows that make up the '51 to 100' bucket since it's the highest range.
    I can easily get the '3' by using the Max formula. So I know what the highest bucket is. Problem is I can't figure out for the life of me, how to pass only those rows that make up the '51 to 100' bucket into the Avg, Min and Max function.....
    I'm stumped and in dire need of something here. I have a spreadsheet that explains the problem better. If somehow I can get a flag on those rows I can easily Avg, Min and Max it. I just can't seem to figure out how to get a flag on only those rows of data.
    I'm willing to share my mocked up example and spreadsheet and .otl and sample data etc....
    Please help :)

    Why does this verify with Min?
    Min ( Filter ( CROSSJOIN ( Descendants ( [Service].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Segment].CurrentMember),
    Filter ( CROSSJOIN ( Leaves ( [Ranges].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Customer Type].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Zip Code].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Disposal Option].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Tickets].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Yardages].CurrentMember),
    Filter ( Descendants ( [Contract Year].CurrentMember),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) ) , [FHRev] )
    And when I just change to Tail and put a 1 at the end it fails?
    Tail ( Filter ( CROSSJOIN ( Descendants ( [Service].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Segment].CurrentMember),
    Filter ( CROSSJOIN ( Leaves ( [Ranges].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Customer Type].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Zip Code].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Disposal Option].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Tickets].CurrentMember),
    Filter ( CROSSJOIN ( Descendants ( [Yardages].CurrentMember),
    Filter ( Descendants ( [Contract Year].CurrentMember),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) )),
    [High Range Max] = ( [High Range Max] , [All FHRev Ranges] ) ) , 1 )

  • To find out the min and max memory been used by each parameter under SGA_MA

    Hi,
    Can any please tell me how to find out the min and max memory been used by each parameter under SGA_MAX and SGA_TARGET ? below is the db CRMS65T. If any such script is there please provide me
    SQL> select name from v$database;
    NAME
    CRMS65T
    SQL> show parameter sga
    NAME                                 TYPE        VALUE
    lock_sga                             boolean     FALSE
    pre_page_sga                         boolean     FALSE
    sga_max_size                         big integer 1000M
    sga_target                           big integer 1000MThanks in advance

    Can any please tell me how to find out the min and max memory been used by each parameter under SGA_MAX and SGA_TARGET ? below is the db CRMS65T. If any such script is there please provide meI guess your question is, each memory components of SGA? if so
    SQL> select * from v$sgainfo;
    NAME                                  BYTES RES
    Fixed SGA Size                      2088504 No
    Redo Buffers                       18882560 No
    Buffer Cache Size                 616562688 Yes
    Shared Pool Size                  301989888 Yes
    Large Pool Size                     4194304 Yes
    Java Pool Size                      4194304 Yes
    Streams Pool Size                         0 Yes
    Granule Size                        4194304 No
    Maximum SGA Size                  947912704 No
    Startup overhead in Shared Pool   125829120 No
    Free SGA Memory Available                 0Also check
    SQL> select COMPONENT,CURRENT_SIZE,MIN_SIZE,MAX_SIZE,USER_SPECIFIED_SIZE from v$sga_dynamic_components;
    shared pool                                                         301989888  301989888          0           209715200
    large pool                                                            4194304    4194304          0             4194304
    java pool                                                             4194304    4194304          0             4194304
    streams pool                                                                0          0          0                   0
    DEFAULT buffer cache                                                616562688  616562688          0           603979776
    KEEP buffer cache                                                           0          0          0                   0
    RECYCLE buffer cache                                                        0          0          0                   0
    DEFAULT 2K buffer cache                                                     0          0          0                   0
    DEFAULT 4K buffer cache                                                     0          0          0                   0
    DEFAULT 8K buffer cache                                                     0          0          0                   0
    DEFAULT 16K buffer cache                                                    0          0          0                   0
    DEFAULT 32K buffer cache                                                    0          0          0                   0
    ASM Buffer Cache                                                            0          0          0           603979776
    13 rows selected.
    SQL>Edited by: CKPT on Sep 19, 2011 8:55 AM

  • Find more than one min and max in 2D array contain 0 rows

    Hi
    I have a 2D array and I would like to find the max and min elements between series of 0s. As you can see in the picture If I remove all 0s from the array and use the max and min function then I will have just one min and one max but I need to find min and max after every 0 rows so you can see from the picture ( just as an example) I will have 3 min and 3 max numbers. Would you please help me with this code. Do you now any algorithm that can find min and max between 0s?
    I have also attached my code to remove 0s and then search for max and min numbers but as I mentioned I need min and max for every part
    Many thanks
    Attachments:
    2.jpg ‏82 KB
    3.jpg ‏27 KB

    Thanks altenbach
    I have attached the vi to this post. I would really apperciate if you help me with this example. The min values should be 100, 1500 and 4000 and the max values should be 1200,2600,5400 so as u mentioned the output should be this 2D array 
    100,1200
    1500,2600
    4000,5400
    Attachments:
    new.vi ‏6 KB

  • Min and max constraints

    In my database design and implementation module I'm at the implementation stage. I've set up all of my tables with PK and FK constraints and I'm wondering if you can enforce min and max column constraints. One of the rules of my DB is for my staff table there has to be a minimum of 10 and a maximum of 50, how would I enforce this rule as a constraint on the staff table or isn't this possible?
    We have only been learning database design and SQL fundamentals so I don't think I'd be allowed to use triggers or procedures.
    Many thanks
    Mike

    mharper wrote:
    thanks guys these commands were accepted
    alter table staff
    add constraint chk_staff_max check (staff_id <= 50);
    alter table staff
    add constraint chk_staff_min check (staff_id >= 10);
    Thing is my staff_id's are in the format 'S110001' will these constraints only check for integers 10 - 50? maybe I need to use a count function on the column or something?
    thanks again for your help!
    MikeCheck constraint can also be used to check a list of values.
    example:
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name> LIKE <condition>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name> NOT LIKE <condition>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name>
    IN (<comma delimited list of values>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name>
    NOT IN (<comma delimited list of values>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name> BETWEEN <lower_value>
    AND <higher_value>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name>
    NOT BETWEEN <lower_value> AND <higher_value>);
    ALTER TABLE <table_name>
    ADD CONSTRAINT <constraint_name>
    CHECK (<column_name>) > (<condition>);

  • Use DO and AI function in one DAQ Device

    Hi, everyone
    I try to use DO and AI function in DAQ USB-6008.
    When I run my program it shows one error message as below,
    Please suggest me how to solve this problem?
    Thank you!
    My Code:
    DAQmxCreateTask("", &gTaskHandle_DAQ1);
    DAQmxCreateTask("", &gTaskHandle_DAQ1_DO);
    DAQmxCreateAIVoltageChan(gTaskHandle_DAQ1, cChan_DAQ1, "", DAQmx_Val_Cfg_Default, min,max, DAQmx_Val_Volts, NULL);
    DAQmxCfgSampClkTiming(gTaskHandle_DAQ1, "", rate, DAQmx_Val_Rising, DAQmx_Val_ContSamps, sampsPerChan);
    DAQmxGetTaskAttribute(gTaskHandle_DAQ1, DAQmx_Task_NumChans, &gNumChannels);
    DAQmxCreateDOChan(gTaskHandle_DAQ1_DO, cChan_DAQ1_DO, "", DAQmx_Val_ChanForAllLines);  <--- Occur error

    hi, Billingsley
    My DAQ version is 7.4.4f4
          OS version is  XP Profession SP3
          LabWindows/CVI version is 8.5.0
    Thank you!

  • Highlighting Min and Max Values on each line

    Hello,
    Is it possible to highlight the Min and Max values on each row of a Query. For example I have sales below by product line for the last seven quarters. The user
    would like the Max value (150) for Product Line ABC Highlighted as Green and the Min value (1055) color coded as Red. Like wise for each line - DEF 200 as green and 100 as red and GHI - 400 as green and 100 as red.
    FYQ Q207 Q307 Q407 Q108 Q208 Q308 Q408
    ABC     1500     1200     1400     1050     1100     1100     1100
    DEF     1550     1000     1560     1220     1340     1640     2000
    GHI     1000     2000     3000     4000     3250     2220     3750
    Is this possible using Exceptions? or may be any other means? We are on Bex 7.0
    Best Regards,
    Sanjiv

    Hello Sanjiv,  
    I think it can be done by JavaScript, but needs more effort
    [Use of JavaScript Functions|http://help.sap.com/saphelp_nw04/helpdata/en/d9/4a853bc623c075e10000000a114084/content.htm]
    Try the exception available in the BW[BW Stylesheets|http://help.sap.com/saphelp_nw04/helpdata/en/3f/ca453afbf37b54e10000000a11402f/content.htm]
    SAPBEXformats - Formatting cell (scaling factors)
    SAPBEXexcGood1 - Exception with priority good 1
    SAPBEXexcGood2 - Exception with priority good 2
    SAPBEXexcGood3 - Exception with priority good 3
    SAPBEXexcCritical4 - Exception with priority critical 4
    SAPBEXexcCritical5 - Exception with priority critical 5
    SAPBEXexcCritical6 - Exception with priority critical 6
    SAPBEXexcBad7 - Exception with priority bad 7
    SAPBEXexcBad8 - Exception with priority bad 8
    SAPBEXexcBad9 - Exception with priority bad 9
    See this thread,
    Change the colour of a cell text depending on the value Web Reports (NW04s)
    Thanks
    Chandran
    Edited by: Chandran Ganesan on Mar 19, 2008 4:27 PM

  • Global Change or filter based on Min and Max dates

    Hi Guys,
    Hopefully there is a genius out there that is an expert with filters and or global changes, I am after a solution and can’t work out if it is possible to do in P6.
    Based on a filter (using codes to select a group of activities) I want to write a value into two date UDF Fields can I:
    Run a global change to give the earliest and latest date in the group and write the result to all tasks in the group? or
    Is there a mindate maxdate option in Global change or filter? Or
    If these tasks were grouped in the activity view can a global change be written to fill down the dates that the summary level is displaying, which are essentially the min and max dates of the groups.  
    I could do this pretty easily in excel but I have over a 100 projects and doing the import one at a time is not feasible.
    I could create LOE’s but as my variables are constantly changing and due to the volume also not an option.
    Summary bars are not an option eithers as I am writing the UDF’s to get all the bars I want on one line, 
    An option for me might be to do it using Legare but I would prefer to see if I can get it done in P6 first.
    Cheers
    Rob

    Yes, you can do it in one report.
    I created a sample report from efashion:
    Year | Lines | Sales Revenue
    2001 | Accessories | $250
    2003 | Accessories | $550
    2001 | City Skirts | $1050
    2003 | City Skirts | $1150...........
    Create 2 variables 1) Count and 2) Difference:
    1) Count  as formula - =Count([Lines]) In Report
    2) Difference as formula - =Sum([Sales revenue]) Where (Max([Year]) In Report = [Year]) - Sum([Sales revenue]) Where (Min([Year]) In Report = [Year])
    You can replace the formula with your report variables. Then just drag Site ID, Count and Difference variables to your report.
    Thanks
    Jai

  • Capturing min and max of a func. generated sine wave

    I need to identify (in real time) the min and max values of a continuous sine wave (whose amplitude may vary). I need to capture the value of each min and max and the time of each such occurrence.

    If the sine wave is in the form of a waveform data type, then all you have to use is the Waveform Min Max function. This does all that you want including returning the time values. There's even an example under Help>Fundamentals>Waveforms>Waveform Operations.

  • Min and MAx Value in a SELECT Statement

    Hi,
    I have a scenario where I am Selecting the values BETWEEN MIN and MAX values:
    SELECT * FROM ABC WHERE CODE BETWEEN MIN(CODE) AND MAX(CODE)
    ITS GETTING Error as:ORA-00934: group function is not allowed here
    Any help will be needful for me.

    select substr(no,1,3)||to_char(substr(no,4,1)+1) "first missing number"
    from
    with t as
    (select 'ABC1' no from dual
    union select 'ABC2' from dual
    union select 'ABC3' from dual
    union select 'ABC5' from dual
    union select 'ABC6' from dual
    union select 'ABC8' from dual
    select no, lead(no,1,0) over (order by no) next_no from t
    where substr(next_no,4,1) - substr(no,4,1) > 1
    and rownum = 1;

  • Difference of value of a dimension based on min and max

    Database: Oracle 10g
    BO-BOXIr3
    Let me explain the exact problem again.
    As per the below code, I have the data in this format in my table:
    Code:
    Date              Site ID     KWH
    1/2/2009 00:00     IN-1     22
    1/2/2009 01:00     IN-1     28
    1/3/2009 03:00     IN-2     25
    1/3/2009 04:00     IN-2     46
    1/4/2009 00:00     IN-3     28
    1/4/2009 10:00     IN-3     34
    1/5/2009 08:00     IN-4     31
    1/5/2009 09:00     IN-4     55
    1/5/2009 11:00     IN-4     77
    1/6/2009 00:00     IN-5     34
    Now want to build a report with following columns:
    Site     Count     KWH
    IN-1     2     6 (ex.-28-22)
    IN-2     2     21
    IN-3     2     6
    IN-4     3     46 (ex.-77-31)
    IN-5     2     34
    SITE- distinct site name.
    COUNT-count is number of repetitions of site id between min and max date.
    KWH -(Delta between the min and max date)
    To get the above result I have created 3 report from different queries since not able to get these al in a single report viz Count, Max Value and Min value. Well I have all these 3 reports or table on a single page.
    Count-this report will give the count between the dates
    Max Value-this report will give me the values of kwh for max dates for each site id
    Min Value-this report will give me the values of kwh for min dates for each site id
    Now want to create a single report based on these 3 reports which contains the column
    Site|Count|KWH
    IS IT POSSIBLE?
    Or
    Is it possible to build such report in a single one with all the required column which I mentioned?
    The variables which I created to get the max & min dates,
    Mx_dt= =Max([Query 2].[Hourly]) In ([Query 2].[SITE_ID])
    Mn_dt= =Min([Query 3 (12)].[Hourly]) In ([Query 3 (12)].[SITE_ID])
    For filtering on report used following variables:
    if_st_mn=If([mn_dt])=[Hourly] Then "ok" Else "no"
    if_st_mx =If([mx_dt])=[Hourly] Then "ok" Else "no"
    will filter on "ok" to get the max and min date values.
    rest of the variable in the snap are not usable.

    Yes, you can do it in one report.
    I created a sample report from efashion:
    Year | Lines | Sales Revenue
    2001 | Accessories | $250
    2003 | Accessories | $550
    2001 | City Skirts | $1050
    2003 | City Skirts | $1150...........
    Create 2 variables 1) Count and 2) Difference:
    1) Count  as formula - =Count([Lines]) In Report
    2) Difference as formula - =Sum([Sales revenue]) Where (Max([Year]) In Report = [Year]) - Sum([Sales revenue]) Where (Min([Year]) In Report = [Year])
    You can replace the formula with your report variables. Then just drag Site ID, Count and Difference variables to your report.
    Thanks
    Jai

  • MIN and MAX datetimes ti find  range

    I am using Oracle 11g version
    create table re(Name char(20),Datetime char(45),val1 number);
    insert into re values('abc','10/29/2012 13:00','1.5')
    insert into re values('abc','10/29/2012 13:05','1.5')
    insert into re values('abc','10/29/2012 13:10','1.5')
    insert into re values('abc','10/29/2012 13:15','1.5')
    insert into re values('abc','10/29/2012 13:20','0.00')
    insert into re values('abc','10/29/2012 13:25','0.00')
    insert into re values('abc','10/29/2012 13:30','0.00')
    insert into re values('abc','10/29/2012 13:35','0.00')
    insert into re values('abc','10/29/2012 13:40','2.1')
    insert into re values('abc','10/29/2012 13:45','2.3')
    insert into re values('abc','10/29/2012 13:50','2.1')
    insert into re values('abc','10/29/2012 13:55','2.1')
    insert into re values('abc','10/29/2012 14:00','2.2')
    O/P:
    In this way data is stored in database.Needed output is, I want the datetime column data range with min and max values where val1>0 only.
    Expected result while we consider the above data is::
    Name mintime maxtime
    abc 10/19/2012 13:00 10/19/2012 13:15
    abc 10/29/2012 13:40 10/29/2012 14:00
    For this I tried something like this,
    select name, min(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) start, max(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) end from (
    select name, Datetime ,to_date(Datetime ,'mm/dd/yyyy hh24:mi') - rank() over (partition by loc_name order by t1 asc) Val_col from re where val1 > 0
    ) group by lname, Val_col
    but I am getting the output like this for above query.
    name start end
    abc 10/29/2012 13:00 10/29/2012 13:00
    abc 10/29/2012 13:05 10/29/2012 13:05
    so.on.
    Edited by: 913672 on Apr 3, 2013 3:07 AM

    913672 wrote:
    I am using Oracle 11g version
    create table re(Name char(20),Datetime char(45),val1 number);
    insert into re values('abc','10/29/2012 13:00','1.5')
    insert into re values('abc','10/29/2012 13:05','1.5')
    insert into re values('abc','10/29/2012 13:10','1.5')
    insert into re values('abc','10/29/2012 13:15','1.5')
    insert into re values('abc','10/29/2012 13:20','0.00')
    insert into re values('abc','10/29/2012 13:25','0.00')
    insert into re values('abc','10/29/2012 13:30','0.00')
    insert into re values('abc','10/29/2012 13:35','0.00')
    insert into re values('abc','10/29/2012 13:40','2.1')
    insert into re values('abc','10/29/2012 13:45','2.3')
    insert into re values('abc','10/29/2012 13:50','2.1')
    insert into re values('abc','10/29/2012 13:55','2.1')
    insert into re values('abc','10/29/2012 14:00','2.2')
    O/P:
    In this way data is stored in database.Needed output is, I want the datetime column data range with min and max values where val1>0 only.
    Expected result while we consider the above data is::
    Name mintime maxtime
    abc 10/19/2012 13:00 10/19/2012 13:15
    abc 10/29/2012 13:40 10/29/2012 14:00
    For this I tried something like this,
    select name, min(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) start, max(to_date(Datetime ,'mm/dd/yyyy hh24:mi')) end from (
    select name, Datetime ,to_date(Datetime ,'mm/dd/yyyy hh24:mi') - rank() over (partition by loc_name order by t1 asc) Val_col from re where val1 > 0
    ) group by lname, Val_col
    but I am getting the output like this for above query.
    name start end
    abc 10/29/2012 13:00 10/29/2012 13:00
    abc 10/29/2012 13:05 10/29/2012 13:05
    so.on.Firstly and most importantly do NOT store dates as char columns: that's what the DATE type is for.
    Secondly, i'm not sure how you get your output from the input, particularly as your SQL contains 'partition by loc_name' which
    doesn't even exist in your example table.
    Why has the same name got two rows with those min/max times. Are you partitioning by val1?

  • Min and Max in a series

    I am creating an application where I need a data structure that returns the min and max that I have in the series of data. Before I write my own I was wondering if there is a pre-built that does that. I don't want to re-invent the wheel.
    Thanks!

    Don't reinvent the wheel, but don't put racing tires on your dog cart...
    What about using java.util.Collections static max and min methods:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#max(java.util.Collection)
    This will be simpler than sorting all the data or using a sorted tree.

Maybe you are looking for

  • Fiscal year 2009 is already closed in Financial Accounting

    Dear All, we have done all  depreciation postings for 2008. we have closed 2008 in OB52 and as per OAAQ 2007 is closed. I want to change depreciation key / shift factor for 2009.  when am trying to change the costcenter it is allowing me But  if i  t

  • Printing Non-Work Intervals (Necking)

    Hello, I have enabled the Activity non-work intervals in the Bar Necking Settings within Bar Settings. Although the 'necks' appear on screen, they do not appear in a PDF printout or a regular print. Is there a setting or workaround for this issue? Pl

  • Image preview problem

    I have Macbook Pro Retina with Mountain Lion preinstalled and I have installed the recent updated. My problem is with the image preview. Normally it works fine until I tryied to change the image too fast. Then the preview starts to act very strangely

  • URGENT ::  ORA-12154: TNS:could not resolve service name

    Hi All, I am connecting to the db using oracle thin driver in java. I am invoking a SOAP call and after getting the response, i am inserting the response(gen. a pdf file) into my DB. I am using a static connection. I have to insert 130 pdf's in DB. A

  • RE: AR process

    Hi Experts Can anyone explain me in briefly AR processes in SAP-FICO end to end. Points would be assined. thanks in advace Venkat