All selected 'n' consecutive rows in retrieved in a single query

hello,
I have table T with 50,000 rows
create table T
(student_id number,
class_id number,
quiz_id number,
marks number)
some sample rows like
INSERT INTO T VALUES (1,1, 1, 50);
INSERT INTO T VALUES (2,2, 2, 40);
INSERT INTO T VALUES (3,1, 3, 34);
INSERT INTO T VALUES (1,1, 4, 10);
INSERT INTO T VALUES (1,1, 5, 30);
INSERT INTO T VALUES (1,1, 6, ‘29);
INSERT INTO T VALUES (3,2, 7, 34);
INSERT INTO T VALUES (3,2, 8, 33);
INSERT INTO T VALUES (3,2, 9, 56);
INSERT INTO T VALUES (1,1, 7, 90);
INSERT INTO T VALUES (2,2, 8, 0,);
INSERT INTO T VALUES (1,1, 8, 80);
INSERT INTO T VALUES (2,2, 8, 65);
INSERT INTO T VALUES (1,1, 9, ‘34);
INSERT INTO T VALUES (2,2, 9, 11);each student belongs to one class_id. each student participates in many quizes. each quiz has its unique id. each student can appear once in a quiz_id
I am doing the below analysis and query:
1. with below query I am finding which student_id had most marks in any 3 successive quizes (see the 3-1 part below) in the query..
SELECT QUIZ_ID,
      STUDENT_ID,
SUM (MARKS) OVER (PARTITION BY STUDENT_ID ORDER BY QUIZ_ID1
RANGE BETWEEN CURRENT ROW AND (3-1) FOLLOWING) consecMARKS , MARKS   FROM
      (SELECT QUIZ_ID,
        STUDENT_ID,
        MARKS,
      ROW_NUMBER() OVER (PARTITION BY STUDENT_ID ORDER BY T.QUIZ_ID) QUIZ_ID1
      FROM T
      WHERE MARKS IS NOT NULL
      ORDER BY 1
    ORDER BY 3 DESC
SQL> /
   QUIZ_ID STUDENT_ID CONSECMARKS
         7          1         170
         6          1         166
         8          1         129
         5          1         106
         8          3          89
         8          2          76
         3          3          68
         7          3          67
         8          2          65
         1          1          60
         9          3          56
   QUIZ_ID STUDENT_ID CONSECMARKS
         9          1          49
         2          2          40
         4          1          40
         9          2          11
15 rows selected.With above query, I can play around and find for any 'n' number of consecutive quizes, like marks in 2 consecutives quizes, 3, 4 and so on but for each 'n' value I've to run a seperate query mentioning (2-1) or (3-1) or (4-1) and so on..
since my table is big and there are about 400 quizes so what I want to find out is for each 'n' consecutive quiz (from 1 to 400) which student had most marks for each consecutie 'n' quiz. Like in 1 (consecutive) quiz which student had the highest marks and then 2 conseuctive quiz who had most marks and then in 3 consecutive quiz who had most marks and so on till 400 consecutive quiz who had most marks... rather than running query for each 'n' value seperately i want a single query that can give me a summary of most marks in each n consecutive quizes...
my sample output is:
Nth consecutive quiz     student_id    sum(marks)
1                        1              90
2                        1              170
3                        1              246
4
100
200
300
400                      ?              ?   Is this possible to get the above output from one single query? If there are two or more students with equal most marks for any 'n' conseutive quizes then both should come in the summary.
Hope I have been able to put up my question clearly.
regards
Ramis

Something like:
SELECT  N,
        QUIZ_ID,
        STUDENT_ID,
        SUM(MARKS) OVER (PARTITION BY N,STUDENT_ID ORDER BY QUIZ_ID1 RANGE BETWEEN CURRENT ROW AND (N-1) FOLLOWING) consecMARKS,
        MARKS
  FROM  (SELECT  QUIZ_ID,
                 STUDENT_ID,
                 MARKS,
                 ROW_NUMBER() OVER (PARTITION BY STUDENT_ID ORDER BY T.QUIZ_ID) QUIZ_ID1
           FROM  T
           WHERE MARKS IS NOT NULL
         SELECT  LEVEL N
           FROM  DUAL
           CONNECT BY LEVEL <= (
                                SELECT  COUNT(DISTINCT QUIZ_ID)
                                  FROM  T
    ORDER BY N,
             consecMARKS DESC
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         1          7          1          90         90
         1          8          1          80         80
         1          8          2          65         65
         1          9          3          56         56
         1          1          1          50         50
         1          2          2          40         40
         1          9          1          34         34
         1          7          3          34         34
         1          3          3          34         34
         1          8          3          33         33
         1          5          1          30         30
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         1          6          1          29         29
         1          9          2          11         11
         1          4          1          10         10
         1          8          2           0          0
         2          7          1         170         90
         2          6          1         119         29
         2          8          1         114         80
         2          8          3          89         33
         2          8          2          76         65
         2          3          3          68         34
         2          7          3          67         34
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         2          8          2          65          0
         2          1          1          60         50
         2          5          1          59         30
         2          9          3          56         56
         2          2          2          40         40
         2          4          1          40         10
         2          9          1          34         34
         2          9          2          11         11
         3          7          1         204         90
         3          6          1         199         29
         3          5          1         149         30
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         3          7          3         123         34
         3          8          1         114         80
         3          2          2         105         40
         3          3          3         101         34
         3          1          1          90         50
         3          8          3          89         33
         3          8          2          76         65
         3          8          2          76          0
         3          4          1          69         10
         3          9          3          56         56
         3          9          1          34         34
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         3          9          2          11         11
         4          6          1         233         29
         4          5          1         229         30
         4          7          1         204         90
         4          4          1         159         10
         4          3          3         157         34
         4          7          3         123         34
         4          1          1         119         50
         4          2          2         116         40
         4          8          1         114         80
         4          8          3          89         33
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         4          8          2          76          0
         4          8          2          76         65
         4          9          3          56         56
         4          9          1          34         34
         4          9          2          11         11
         5          5          1         263         30
         5          4          1         239         10
         5          6          1         233         29
         5          1          1         209         50
         5          7          1         204         90
         5          3          3         157         34
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         5          7          3         123         34
         5          2          2         116         40
         5          8          1         114         80
         5          8          3          89         33
         5          8          2          76          0
         5          8          2          76         65
         5          9          3          56         56
         5          9          1          34         34
         5          9          2          11         11
         6          1          1         289         50
         6          4          1         273         10
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         6          5          1         263         30
         6          6          1         233         29
         6          7          1         204         90
         6          3          3         157         34
         6          7          3         123         34
         6          2          2         116         40
         6          8          1         114         80
         6          8          3          89         33
         6          8          2          76          0
         6          8          2          76         65
         6          9          3          56         56
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         6          9          1          34         34
         6          9          2          11         11
         7          1          1         323         50
         7          4          1         273         10
         7          5          1         263         30
         7          6          1         233         29
         7          7          1         204         90
         7          3          3         157         34
         7          7          3         123         34
         7          2          2         116         40
         7          8          1         114         80
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         7          8          3          89         33
         7          8          2          76          0
         7          8          2          76         65
         7          9          3          56         56
         7          9          1          34         34
         7          9          2          11         11
         8          1          1         323         50
         8          4          1         273         10
         8          5          1         263         30
         8          6          1         233         29
         8          7          1         204         90
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         8          3          3         157         34
         8          7          3         123         34
         8          2          2         116         40
         8          8          1         114         80
         8          8          3          89         33
         8          8          2          76          0
         8          8          2          76         65
         8          9          3          56         56
         8          9          1          34         34
         8          9          2          11         11
         9          1          1         323         50
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         9          4          1         273         10
         9          5          1         263         30
         9          6          1         233         29
         9          7          1         204         90
         9          3          3         157         34
         9          7          3         123         34
         9          2          2         116         40
         9          8          1         114         80
         9          8          3          89         33
         9          8          2          76          0
         9          8          2          76         65
         N    QUIZ_ID STUDENT_ID CONSECMARKS      MARKS
         9          9          3          56         56
         9          9          1          34         34
         9          9          2          11         11
135 rows selected.
SQL> SY.

Similar Messages

  • Select ... in (select ....) retrieves all rows

    Hi guys,
    I have the following situation:
    Table_A
    ID NUMBER
    NAME VARCHAR2(10)
    TABLE_B
    ID_B
    NAME_B VARCHAR2(10)
    By mistake I did the following query:
    Select *
    from table_a
    where id in (select ID from table_b);
    My question is: Why the query return all rows from TABLE_A if in the inner select (select ID...) I'm didn't write any of the columns that exist on TABLE_B.?
    Hope this is clear.!.
    thanks in advance.

    select *
    from table_a
    where id in (select ID from table_b);The subquery will attempt to resolve the column named "ID" first within the subquery itself, then (if it doesn't find it) in the parent query. Since there is no ID column in table_b, the query actually becomes something like
    select *
    from table_a a
    where a.id in (select a.id from table_b);
    in other words
    select *
    from table_a
    where id = id
    which is equivalent to
    select *
    from table_a
    where id IS NOT NULL
    HTH

  • F4 help : how to select all fields of a row

    Hi All,
               I am implementing f4 help in my bsp page. in teh help page i have 3 fields for each row. i get the value of key row in the input field once i select a particular row, bt I also want to access other 2 fields of the row that is selected, How can i do this?
    in the in put field i wnat only the key field tyo be displayed, but in the BSP application i want to access the otehr two fields that are in teh selected row, how can i do this?
    Thanks,
    Vasuki

    Hi all,
    Since this thread is a duplicate, I request every one to post you answer in the orignal thread.
    Please find the orignial thread in the following link.
    [http://forums.sdn.sap.com/post!reply.jspa?messageID=8350499|http://forums.sdn.sap.com/post!reply.jspa?messageID=8350499]
    Hi Vasuki,
    Please close this thread, since this one is a duplicate.
    Thanks,
    Maheswaran

  • Selecting only one row at a time

    Hi experts,
    i have following doubt regarding selecting rows from a db:
    Is there any way of selecting only one row AT A TIME from a dabase just to collect the data in rows instead of in a unique document containing all the rows?
    I would like you to ellaborate on this as i need to send only one row to the IE, and then other row, and so on... without throwing any error!
    I have seen that there are SELECT SINGLE and SELECT UP TO 1 ROW, but these two methods are only useful when retrieving only one row, and that does not match my requirements. I need to process all the rows but one by one..
    I know that we can use the receiver jdbc adapter as if it was a sender by means of its specific datatype, but how to do it row by row??
    Hope i had explained well..
    Thanks in advance and best regards,
    David

    Hi kiran,
    Yes, my table has 5 not null fields but i am selecting and updating fixes values so i think that I will definetely go for the next solution:
    SELECT * FROM t1 WHERE status='0' and ROWNUM<2;
    UPDATE t1 SET status='1' WHERE status='0' and ROWNUM<2;
    My only concern is if the update will take the same row that the select.... BTW, I think it will
    ..What do you guys think?
    I ve been trying to operate with your proposed queries but i received some errors. Your queries are very interesting but i think that with the above ones i meet my requirements as the status field will be 0 for not processed rows and 1 for precessed ones (and the update will look for the row that meets the same 'where' clause than the select, and then, and only then, it will set status='1').
    The only thing i have to care about is what i questioned before.
    Thanks a lot and best regards,
    David

  • Identifying and grouping consecutive rows in sql

    I have following data set:
    CREATE TABLE APPS.T1
      ROW_NUM               NUMBER,
      EFFECTIVE_START_DATE  DATE                    NOT NULL,
      EFFECTIVE_END_DATE    DATE                    NOT NULL,
      STATUS                VARCHAR2(30 BYTE)
    SET DEFINE OFF;
    Insert into APPS.T1
       (ROW_NUM, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, STATUS)
    Values
       (1, TO_DATE('07/01/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('09/06/2009 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'VAC');
    Insert into APPS.T1
       (ROW_NUM, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, STATUS)
    Values
       (2, TO_DATE('03/20/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/31/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'VAC');
    Insert into APPS.T1
       (ROW_NUM, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, STATUS)
    Values
       (3, TO_DATE('08/06/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/22/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'VAC');
    Insert into APPS.T1
       (ROW_NUM, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, STATUS)
    Values
       (4, TO_DATE('08/23/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/26/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'VAC');
    Insert into APPS.T1
       (ROW_NUM, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, STATUS)
    Values
       (5, TO_DATE('08/27/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/27/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'VAC');
    COMMIT;
    SELECT * FROM APPS.T1
       ROW_NUM EFFECTIVE EFFECTIVE STATUS                       
             1 01-JUL-09 06-SEP-09 VAC                          
             2 20-MAR-11 31-MAR-11 VAC                          
             3 06-AUG-11 22-AUG-11 VAC                          
             4 23-AUG-11 26-AUG-11 VAC                          
             5 27-AUG-11 27-AUG-11 VAC                          
    5 rows selected.My requirement was that row number 3, 4 and 5 be grouped and treated as a single vacation record such that
    effective_start_date = 06-AUG-2011 and
    effective_end_date = 27-AUG-2011
    For this I wrote a query:
    SELECT effective_start_date,
           effective_end_date,
           CASE
              WHEN LAG (effective_end_date, 1) OVER (ORDER BY row_num) + 1 = effective_start_date
                 THEN 0
              WHEN LEAD (effective_start_date, 1) OVER (ORDER BY row_num) - 1 = effective_end_date
                 THEN 0
              ELSE 1
           END row_num
      FROM (SELECT * FROM T1)Now the data returned looks like:
    EFFECTIVE EFFECTIVE    ROW_NUM
    01-JUL-09 06-SEP-09          1
    20-MAR-11 31-MAR-11          1
    06-AUG-11 22-AUG-11          0
    23-AUG-11 26-AUG-11          0
    27-AUG-11 27-AUG-11          0
    5 rows selected.Now I can easily use MIN(effective_start_date) and MAX(effective_start_date) group by ROW_NUM to achieve the desired results
    SELECT   MIN (effective_start_date) start_dt,
             MAX (effective_start_date) end_dt,
             row_num
        FROM (SELECT effective_start_date,
                     effective_end_date,
                     CASE
                        WHEN LAG (effective_end_date, 1) OVER (ORDER BY row_num) + 1 = effective_start_date
                           THEN 0
                        WHEN LEAD (effective_start_date, 1) OVER (ORDER BY row_num) - 1 = effective_end_date
                           THEN 0
                        ELSE 1
                     END row_num
                FROM (SELECT *
                        FROM t1))
    GROUP BY row_num
      HAVING row_num = 0
    UNION
    SELECT effective_start_date start_dt,
           effective_start_date end_dt,
           row_num
      FROM (SELECT effective_start_date,
                   effective_end_date,
                   CASE
                      WHEN LAG (effective_end_date, 1) OVER (ORDER BY row_num) + 1 = effective_start_date
                         THEN 0
                      WHEN LEAD (effective_start_date, 1) OVER (ORDER BY row_num) - 1 = effective_end_date
                         THEN 0
                      ELSE 1
                   END row_num
              FROM (SELECT *
                      FROM t1))
    WHERE row_num = 1
    START_DT  END_DT       ROW_NUM
    01-JUL-09 01-JUL-09          1
    20-MAR-11 20-MAR-11          1
    06-AUG-11 27-AUG-11          0
    3 rows selected.All done BUT the problem is that there may be several groups of consecutive rows like this. In that case each group should be identified distinctly for GROUP BY clause to work as expected.
    I want to assign a unique number to each occurence of such group.
    How can I achieve this? Any ideas?
    Regards,
    Faraz
    Edited by: faanwar on May 10, 2012 3:36 PM

    Well, actually, you'll need to tweak it a bit. something such as in :Scott@my11g SQL>l
      1  with t (id, dstart, dend, status)
      2  as (
      3       select 1,to_date('01-JUL-09','dd-MON-YY'),to_date('06-SEP-09','dd-MON-YY'),'VAC' from dual
      4       union all select 2,to_date('20-MAR-11','dd-MON-YY'),to_date('31-MAR-11','dd-MON-YY'),'VAC' from dual
      5       union all select 3,to_date('06-AUG-11','dd-MON-YY'),to_date('22-AUG-11','dd-MON-YY'),'VAC' from dual
      6       union all select 4,to_date('23-AUG-11','dd-MON-YY'),to_date('26-AUG-11','dd-MON-YY'),'VAC' from dual
      7       union all select 5,to_date('27-AUG-11','dd-MON-YY'),to_date('27-AUG-11','dd-MON-YY'),'VAC' from dual
      8  )
      9  ------ end of sample data ------
    10  select min(dstart) dstart, max(dend) dend, status, count(*) aggrows
    11  from (
    12       select
    13       id
    14            ,dstart
    15            ,dend
    16            ,status
    17            ,dend
    18                 -sum(dend-dstart) over (partition by status order by dstart)
    19                 -row_number() over (partition by status order by dstart) grp
    20       from t
    21  )
    22  group by grp, status
    23* order by grp, status
    Scott@my11g SQL>/
    DSTART              DEND                STA    AGGROWS
    01/07/2009 00:00:00 06/09/2009 00:00:00 VAC          1
    20/03/2011 00:00:00 31/03/2011 00:00:00 VAC          1
    06/08/2011 00:00:00 27/08/2011 00:00:00 VAC          3

  • Diffrence b/w select single & select upto one row

    hi
       wat is the diffrence b/w select single & select upto one row?
    deepak

    Hi,
    Select single has to be used with a where condition that has all the key fields:
    It will always return a unique record(If a match is found).
    Select upto 1 rows would get your the first record if multiple matches are found.
    and select up to has to be end with endselect statements.
    According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
    select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
    The best way to find out is through sql trace or runtime analysis.
    Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
    The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
    The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
    Mainly: to read data from
    The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
    Mainly: to check if entries exist.
    When you say SELECT SINGLE, it means that you are expecting only one row to be present in the database for the condition you're going to specify in the WHERE clause. so that means, you will have to specify the primary key in your WHERE clause. Otherwise you get a warning.
    SELECT UP TO 1 ROWS is used in cases where you just want to make sure that there is at least one entry in the database table which satisfies your WHERE clause. Generally, it is meant to be used for existence-check.
    You may not want to really use the values returned by the SELECT statement in this case (thought this may not necessarily be so).And in each case the database optimizer may choose a different strategy to retrieve the data.
    Knowing when to use SELECT SINGLE or SELECT ... UP TO 1 ROWS
    A lot of people use the SELECT SINGLE statement to check for the existence of a value in a database. Other people prefer to use the 'UP TO 1 ROWS' variant of the SELECT statement.
    So what's the difference between using 'SELECT SINGLE' statement as against a 'SELECT .... UP TO 1 ROWS' statement ?
    If you're considering the statements
    SELECT SINGLE field INTO w_field FROM table.
    and
    SELECT field INTO w_field FROM table UP TO 1 ROWS. ENDSELECT.
    then looking at the result, not much apart from the extra ENDSELECT statement. Look at the run time and memory usage and they may be worlds apart.
    Why is this ?? The answer is simple.
    The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
    The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
    According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
    http://sap-img.com/abap/difference-between-select-single-and-select-upto-one-rows.htm
    Check these links -
    The specified item was not found.
    diff between select single and up to one row
    diff b/w SECELT SINGLE *   AND SELECT UPTO ONE ROW
    Regards,
    Priyanka.

  • Difference between select single * & select upto 1 rows

    difference between select single * & select upto 1 rows

    Hi,
       According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
    Select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
    The best way to find out is through sql trace or runtime analysis.
    Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
    The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
    The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
    Mainly: to read data from
    The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
    Mainly: to check if entries exist.
    Select Single
    You need to mention all the key fields of the table.
    No END SELECT required.
    More performance compared to upto 1 row.
    Where as UP to 1 row.
    YOu can use if you do not have all the primiary key fields available.
    END SELECT requeired.
    Since all keys are not passing, possiblities of have other rows which satisfies the condition.
    Select Statement with EndSelect is a loop, which in a single run retrieves a single Record. This Record has to be stored in a Work Area and then appended into an Internal Table.
    Select Statements without EndSelect is not a loop and it retrieves the whole Record set matching the Criteria in a single shot and has to be Stored in an Internal Table Directly.
    The most important thing to remember about the SELECT SINGLE is
    There are several things to remember:
    1) It retrieves only one row
    2) It does not need an ENDSELECT statement
    3) THE FULL KEY OF THE TABLE MUST BE INCLUDED IN
    THE WHERE CLAUSE OF THE SELECT STATEMENT
    Regards

  • Select Single Vs Select upto 1 row

    Hi All,
    Please tell me which of the two statements is better if we want to retrieve only one record from a table.
    Select single or Select upto 1 rows?
    Regards,
    Saurabh

    There is a lot of confusion in the replies and also in the other thread.
    If yoou ask 'A versus B' then this implies that you can use both for the same task, which is here not really true.
    SELEC SINGLE should be used for SELECT with full primary key. where only 1 record CAN come back.
    UP TO 1 ROWS is the special case of UP TO n ROWS can be used with any WHERE condition and gives you the first record which is found.
    >The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the
    > relevant records that are defined by the WHERE clause or lack of, applies any aggregate,
    > ordering or grouping functions to them and then returns the first record of the resultant result
    > set.
    This in incorrect, the UP TO n ROWS does not read all, only if the ORDER BY is added, then you will get the first records in sort order which requires that all rfecords are read.
    If you use UP TO 1 ROWS with a WHERE condition which is fulfilled by many records, then the first record is usually found very fast, even faster than a SELECT SINGLE.
    If you use UP TO 1 ROWS or SELECT SINGLE with the same WHERE condition, then both are more or less the same.
    Siegfried

  • Select single * & Select  * upto one row

    What is the difference between Select single * & Select * upto one row?Performance wise which is a better one?

    Hi,
        According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
    Select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
    The best way to find out is through sql trace or runtime analysis.
    Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
    The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
    The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.
    Mainly: to read data from
    The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
    Mainly: to check if entries exist.
    Select Single
    You need to mention all the key fields of the table.
    No END SELECT required.
    More performance compared to upto 1 row.
    Where as UP to 1 row.
    YOu can use if you do not have all the primiary key fields available.
    END SELECT requeired.
    Since all keys are not passing, possiblities of have other rows which satisfies the condition.
    Select Statement with EndSelect is a loop, which in a single run retrieves a single Record. This Record has to be stored in a Work Area and then appended into an Internal Table.
    Select Statements without EndSelect is not a loop and it retrieves the whole Record set matching the Criteria in a single shot and has to be Stored in an Internal Table Directly.
    The most important thing to remember about the SELECT SINGLE is
    There are several things to remember:
    1) It retrieves only one row
    2) It does not need an ENDSELECT statement
    3) THE FULL KEY OF THE TABLE MUST BE INCLUDED IN
    THE WHERE CLAUSE OF THE SELECT STATEMENT
    Regards

  • Grouping consecutive rows with same value?

    Hi
    I have a table in which three columns are:
    ROWID  SHOPNAME
    1      SHOP_C     
    2      SHOP_A     
    3      SHOP_C     
    4      SHOP_C     
    5      SHOP_A     
    6      SHOP_A     
    7      SHOP_C     
    8      SHOP_B     
    9      SHOP_B     
    10     SHOP_E    
    11     SHOP_A     
    12     SHOP_D     
    13     SHOP_D     
    14     SHOP_F     
    15     SHOP_G     
    16     SHOP_F     
    17     SHOP_C     
    18     SHOP_C     
    19     SHOP_C     
    20     SHOP_C      Rowid is a primary key column with unique ids
    Shopname is varchar2 column
    I want to make groupings of every "shopname" value "order by ROWID" such that when there are consecutive rows of same shopname the group remains same and as soon as there is different value in shopname column for the next row the "group" changes.
    below is my desired output is with 3rd column of groupings as follows:
    ROWID  SHOPNAME    Groups
    1      SHOP_C      1
    2      SHOP_A      2
    3      SHOP_C      3 ------> same grouping because Shop name is same
    4      SHOP_C      3 ------> same grouping because shop name is same
    5      SHOP_A      4 ----> different shopname so group changed.
    6      SHOP_A      4 ----> same group as above because shopname is same
    7      SHOP_C      5
    8      SHOP_B      6
    9      SHOP_B      6
    10     SHOP_E      7
    11     SHOP_A      8
    12     SHOP_D      9
    13     SHOP_D      9
    14     SHOP_F      10
    15     SHOP_G      11
    16     SHOP_F      12
    17     SHOP_C      13
    18     SHOP_C      13
    19     SHOP_C      13
    20     SHOP_C      13I want that to be done via analytics, so that I can partition by clause for different shops over different cities
    regards
    Ramis.

    with data(row_id, shop) as
    select 1,      'SHOP_C' from dual union all
    select 2,      'SHOP_A' from dual union all     
    select 3,      'SHOP_C' from dual union all
    select 4,      'SHOP_C' from dual union all    
    select 5,      'SHOP_A' from dual union all     
    select 6,      'SHOP_A' from dual union all
    select 7,      'SHOP_C' from dual union all
    select 8,      'SHOP_B' from dual union all
    select 9,      'SHOP_B' from dual union all
    select 10,     'SHOP_E' from dual union all
    select 11,     'SHOP_A' from dual union all
    select 12,     'SHOP_D' from dual union all
    select 13,     'SHOP_D' from dual union all
    select 14,     'SHOP_F' from dual union all
    select 15,     'SHOP_G' from dual union all
    select 16,     'SHOP_F' from dual union all
    select 17,     'SHOP_C' from dual union all
    select 18,     'SHOP_C' from dual union all
    select 19,     'SHOP_C' from dual union all
    select 20,     'SHOP_C' from dual
    get_data as
      select row_id, shop,
             lag(shop) over (order by row_id) prev_shop
        from data
    select row_id, shop,
           sum(case when shop = prev_shop
                      then  0
                      else 1
                      end
                ) over (order by row_id) grps
      from get_data;
    ROW_ID SHOP   GRPS
         1 SHOP_C    1
         2 SHOP_A    2
         3 SHOP_C    3
         4 SHOP_C    3
         5 SHOP_A    4
         6 SHOP_A    4
         7 SHOP_C    5
         8 SHOP_B    6
         9 SHOP_B    6
        10 SHOP_E    7
        11 SHOP_A    8
        12 SHOP_D    9
        13 SHOP_D    9
        14 SHOP_F   10
        15 SHOP_G   11
        16 SHOP_F   12
        17 SHOP_C   13
        18 SHOP_C   13
        19 SHOP_C   13
        20 SHOP_C   13
    20 rows selected

  • Selection of a row in  a table

    Hi,
    I have a problem with JTable. ...i have just started working with JTables and still have not got a complete hang of how they function.
    I have a table whose model is "AbstractTableModel". I have different data in each row of the table therefore i have a celledtior and cell renderer per row. Each row is rendered as a combo box. There is only one column in that table. What i need to do is when the user clicks on any of these the rows in the table i need to evaluate what is the content in that row(in the combo box-It should be the values that have not been used up yet in the other rows...so basically as you add rows the combo box content will keep shrinking till all the possible vlaues are exhausted).
    I added a celleditor listener for every row. When the user clicks on the cell the event get's captured but the row value that shows up is not row the user selected. The value is always the previous selected row. Each cell is rendered as a combo box.
    For example if the user selected the first row in the table then the value of the row selected is -1. If the the user adds two more rows and selects the third row then the value selected is 0 (previos selected).
    What i cannot figure out is how in the world is this value obtained.
    Please help !! I tried everything i can think of to get the correct value of the row selected.
    Thanks a lot for your time and patience.
    Archana

    Hello Suman,
    Please, look at 'No First Select from Table' section of this blog: [/people/jarrod.williams/blog/2006/12/14/visual-composer-tips-and-tricks|/people/jarrod.williams/blog/2006/12/14/visual-composer-tips-and-tricks]
    Good luck,
    Ola

  • Selecting an entire row with the help of Checkbox

    Hi and Evening to Everybody,
    I have a Scenario where i need to select an entire row using the check box. Let me first define the Situation. I created a Simple Sql-report where the first column is a Simple Checkbox and the second column is a display only name and followed by the rest 5 columns as a checkbox.
    my table structure is :
    **create table satt (SELECT_ALL VARCHAR2(10), Name VARCHAR2(50), Object1 VARCHAR2(10), Object2 VARCHAR2(10), Object3 VARCHAR2(10), Object4 VARCHAR2(10), Object5 VARCHAR2(10));**
    Now i had a requirement where i need to Check All or Uncheck All Checkbox by clicking SELECT_ALL column header and i made it using
    simple java-script :
    "<input type="Checkbox" onclick="$f_CheckFirstColumn(this)">"
    Now i need to Check all checkbox in a row by clicking any of the SELECT_ALL check boxes. (Say i have 5 checkboxes in SELECT_ALL column and if i click 3rd checkbox... i need the entire 3rd row checkbox to be checked on click of that 3rd check box).
    I hope i was clear with my question. i did my best. Please help me out with this... Im eagerly lookin for the solutions.
    Thanks & Regards,
    - The Beginner.
    Edited by: 854477 on Jul 13, 2011 4:57 AM
    Edited by: 854477 on Jul 13, 2011 5:01 AM

    Solomon Yakobson wrote:
    There is no row order in relational tables unless ORDER BY is used, so there is no way to decide if 3 Mathematics belongs to 100 or to 200.
    SY.That's not how I interpretted it. I thought he was saying that in the first row column B has the value:
    '1 Physics'||chr(10)||'2 Chemistry'||chr(10)||'3 Mathematics'
    in which case something like this would work
    select a,replace(b,chr(10),chr(10)||a||' ') from table;

  • All SELECT statements work except of SELECT *

    Hi,
    I have one table with 2 rows in Oracle 10.1.0.2.0
    All SELECT statements work (e.g. SELECT COUNT(*) FROM, SELECT COLUMNNAME FROM TABLE, etc.)
    But when I execute SELECT * FROM it never shows any response in SQLPlus. I can view the data with Enterprise Manager Console - using View/Edit Content.
    How this can be caused?
    Thanks for reply.

    Hi,
    I don't get any error. I enter the SELECT * FROM statement run it and cursor jumps to next line and is blinking forever...
    So I got no error response from database.
    SELECT COLUMNNAME FROM works normally.
    As I wrote the machine with database is probably used over its limits - I'm not DBA but SGA is set up to use about 90% of memory (as I learnt from other forums recomendation for Solaris/Sun is about 40%).
    Unfortunatelly I have just 2 rows in my table. There are metadata in the table which are needed to get info about other tables in the database.
    So I am wondering why is the database able to work normally but doesn't response to this statement. I run queries on other tables in database with millions of records, but I am not able to run this query.
    Can be table somehow corrupted? It is strange...
    Regards,
    Petr

  • XMLP 5.6.2 - Parameters -  Both Multi-Select and All-Select  Do Not Work

    Both Multi-Select and All-Select Do Not Work.
    What's up with this behavior?
    And when might it be fixed?
    BG...

    The Multi-Select is now working, not sure what I did wrong earlier. However the "Can select all" option when building the parameter does not work as advertised, though I was able to get around that as well.
    Create a Data Model
    select LAST_NAME, FIRST_NAME from SOME_TABLE where SOME_COLUMN =:SOME_VALUE
    OR
    select LAST_NAME, FIRST_NAME from SOME_TABLE where SOME_COLUMN in (:SOME_VALUE)
    Create a LOV
    Name = LOV_SOME_COLUMN
    Type = SQL Query
    select distinct SOME_TABLE.SOME_COLUMN as SOME_COLUMN from SCHEMA.TABLE
    Create a Parameter
    Identifier = PARM_SOME_COLUMN
    Data Type = String
    Default Value = NULL
    Parameter Type = Menu
    Display Label = LOV_SOME_COLUMN
    List Of Value = LOV_SOME_COLUMN
    Option
    Multiple Selection - Checked
    Can select all - Checked
    Refresh other parameters on change - Checked
    Then test your report. Select "All" from the LOV and click view. Result - No Rows Returned.
    Selecting a "Single" or "Multiple Values" values from the LOV does work.
    The way I am getting around the problem is to uncheck the "Can select all" and then manually select all the values in the LOV in order to get the "All".
    BG...

  • Select 2 records (rows) in the results.

    I am using Oracle R12- Forms.
    After searched for all orders for a specific Customer, on the Results, I need to select 2 different rows.
    Also tried ft object,but failed.
    If manual, I use Keep holding the Ctrl Key, and Click for the first row, and Click 2nd row.
    In OATS, I could not do this with the code below:
    int rone=2;
    int rtwo=5
    Robot robot = new Robot();
    robot.delay(5000);
    // press and hold CONTROL key
    robot.keyPress(KeyEvent.VK_CONTROL);      
    forms.textField("//forms:textField[(@name='MY_ORDER_NUM_"+rone+"')]").click();          
    forms.textField("//forms:textField[(@name='MY_ORDER_NUM_"+rtwo+"')]").click();
    robot.keyRelease(KeyEvent.VK_CONTROL);
    Any help is greatly appreciated.
    Thanks,

    What you need is a tree.
    See Tomahawk (www.myfaces.org)

Maybe you are looking for

  • When saving a file in [.jpg] format, i am asked to choose a quality from 1 to 100. what changes in the file created based on the quality parameter chosen?

    when saving a file in [.jpg] format, i am asked to choose a quality from 1 to 100. what changes in the file created based on the quality parameter chosen?. i would like to know what changes, so in the future i can set my camera to a setting that will

  • Problem with adobe flash player on my new computer

    I bought a new computer window 8 and as you know adobe flash player is already installed but now flash player won't play some videos on the windows 8 internet exploer unless i swicthed it to the desktop. when i try to watch it says i need to install

  • Syncing over 1 hour just for contacts?

    Last night I connected my iPhone to the computer to sync. An hour later it was still syncing contacts. I cancelled the sync. This morning I tried again. It has been 30 minutes and it is still syncing contacts, which is the first thing it said it was

  • Total Comp Statement

    Hi All, I am trying to use the SAP standard form for TCS and its displaying only 1 page with the master data. The actual LTI plan info from IT 0761 is not displayed. Can someone help me find an answer? We are using only the CMP category for other com

  • Iconic Buttons Issues

    I upgraded my 6i forms to 9i forms. So far so good. But when I run my forms on the web, the images (.ico) are not showing up on my iconic buttons. Is there any particular setting I need to look at. Please help if anyone has any ideas. Thanks, Rudy