Select from table in group of columns and generate a sequence number

I have to select data from a table in group of columns and generate a sequence for every group resetting the sequence to start from 1 onwards.
For example:
Data:
Col1 Col2 Col3 Col4
A NA KA 2009-08-13
B NA KA 2009-08-13
C NA KA 2009-08-13
A NA KA 2009-08-13
B NA KA 2009-08-13
A NA KA 2009-08-13
Expected output from Select Statement:
Col1 Col2 Col3 Col4 Seq_No
A NA KA 2009-08-13 1
A NA KA 2009-08-13 2
A NA KA 2009-08-13 3
B NA KA 2009-08-13 1
B NA KA 2009-08-13 2
C NA KA 2009-08-13 1
How can this be possible with a SELECT statement? Is it possible to assign seq numbers for a group of columns and reset it when it changes? In the above example, all columns form the key to generate the seq number
I know it can be done using Stored procedures and that is how I am doing it now by introducing a temporary table.
Can anyone help me in this regard? Please let me know if the question is vague to understand!
Thanks,
Nachi

with t as(select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'C' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual)
select t.*,row_number() over (partition by col1,col2,col3,col4 order by col1,col2,col3,col4) from tYou can replace partition by col1,col2,col3,col4 with only columns that you need for grouping condition
and also order by you can just do on the column you need.

Similar Messages

  • How to achieve that "SELECT * FROM table WHERE age BETWEEN 18 AND 23 AND n"

    How to achieve the SQL like that "SELECT * FROM table WHERE age BETWEEN 18 AND 23 AND name = 'Will' " with BDB C-API
    The primary key in the primary database is 'age' ,and the secondary key in the secondary index is 'name' ,in a lot of examples ,there are all simple , but how to do that complex one.thx~

    but this means that the prepared statement is created
    each time I call my method and so I'm not sure that
    the optimizer will find it easy to cope with.You are right, the optimizer won't find that easy to deal with (presuming that is even relevant for your driver/database.) But most optimizers won't do anything with statements that change and that is what you are doing.
    You could create several prepared statements which have a common number of bind variables. For example 10 statements with from 1 to 10 bind values. This will work if most of the queries use those.

  • Select from table without specifying the column list.

    I want to reference the columns in select list without specifing the column name of a table.
    For example i am having a table called emp(id number,name varchar2(30)).
    I want to write a query which will display the name of the employees.
    But i don't want to specify the column name in the select list.
    Can anybody help me in this regard.
    Thanks in advance.

    I want to write a query which will display the name
    of the employees.
    But i don't want to specify the column name in the
    select list.So what do you want to specify in the select list?
    One way to display the names of the employees without specifying that you want the names of the employees would be
    select * from emp

  • How to write a query for grouping them the columns and give the sequence order to each group/

    Hi i have table that contains country columns .
    India,USA,UK like these when ever the group changed into the differt country i make a group and arrange them the sequence into those Countries
    like below
    1)India
    2)India
    1)USA
    2)USA
    like these to write a query ..........pls help me for this query

    Assuming you're using SQL Server you can ask here:
    http://www.sqlteam.com/forums/forum.asp?FORUM_ID=23
    Otherwise, please ask in the relevant forum for the type of technology you're using.
    Basically it's either:
    select *
    from [table name]
    order by country
    If you want to do something with groups do something like:
    select (max) income, country
    from [table name]
    group by country
    Kind regards,
    Margriet Bruggeman
    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

  • What index is suitable for a table with no unique columns and no primary key

    alpha
    beta 
    gamma
    col1
    col2
    col3
    100
    1
    -1
    a
    b
    c
    100
    1
    -2
    d
    e
    f
    101
    1
    -2
    t
    t
    y
    102
    2
    1
    j
    k
    l
    Sample data above  and below is the dataype for each one of them
    alpha datatype- string 
    beta datatype-integer
    gamma datatype-integer
    col1,col2,col3 are all string datatypes. 
    Note:columns are not unique and we would be using alpha,beta,gamma to uniquely identify a record .Now as you see my sample data this is in a table which doesnt have index .I would like to have a index created covering these columns (alpha,beta,gamma) .I
    beleive that creating clustered index having covering columns will be better.
    What would you recommend the index type should be here in this case.Say data volume is 1 milion records and we always use the alpha,beta,gamma columns when we filiter or query records 
    what index is suitable for a table with no unique columns and primary key?
    col1
    col2
    col3
    Mudassar

    Many thanks for your explanation .
    When I tried querying using the below query on my heap table the sql server suggested to create NON CLUSTERED INDEX INCLUDING columns    ,[beta],[gamma] ,[col1] 
     ,[col2]     ,[col3]
    SELECT [alpha]
          ,[beta]
          ,[gamma]
          ,[col1]
          ,[col2]
          ,[col3]
      FROM [TEST].[dbo].[Test]
    where   [alpha]='10100'
    My question is why it didn't suggest Clustered INDEX and chose NON clustered index ?
    Mudassar

  • Select from table containing clob

    If i try to select from table containing clob column in SQL PLus it gives error.
    Tab1 contains 3 clob columns and 1 blob column
    select * from tab 1;
    SP2-0678: Column or attribute type can not be displayed by SQL*Plus
    The same statement works in SQL Developer and I am able to see the result.
    Actually i am writing the queries and they will be used by Java developers in their JSP page.
    So what happens here? Can Java use these select statements or will it throw error like SQL Plus?

    BLOB column content can't be displayed in SQL*Plus:
    SQL> create table t_blob (b blob);
    Table created.
    SQL> edit
    Wrote file afiedt.buf
      1* insert into t_blob values('01')
    SQL> /
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select * from t_blob;
    SP2-0678: Column or attribute type can not be displayed by SQL*PlusBlob and clob columns content can be processed using DBMS_LOB
    package procedures and functions or using client's language (like Java)
    methods. See JDBC specification.
    Rgds.

  • Select * from table not working with Oracle OBDC driver

    Hello,
    In our web development we have been using the MS ODBC for Oracle
    driver to connect to our Oracle db. We decided to try the
    Oracle ODBC driver because it supports the commandTimeout
    property in ASP which the MS driver does not. The problem I'm
    running into now is that all of our select * from table
    statements appear not to be working. The Oracle ODBC driver
    version we are using is ver 8.00.05.00. Is there something that
    I'm not doing properly? If I take the same select * from table
    statement and name the columns, I dont get any error. Otherwise
    I'm getting a Subscript out of range error. It seems strange to
    me that this driver would not support a select * from table
    statement (which I''m told is the case by another developer
    here).
    Is there something I'm missing?
    Thanks,
    Pete

    I'm positive I have a connection. Otherwise I wouldn't get a
    response when I name the columns instead of using *.
    There must be something else that I'm missing or doing wrong.
    I've actually been looking into alternative ODBC drivers to see
    if I have the same problems but none that I have found support
    commandTimeout.
    Any other ideas?

  • Select * from table as of Scn fails in powercenter

    Hi all, I have written the below query in SQL override of a Powercenter mapping. This fails with 'FROM keyword not found where expected' error. Can you please suggest how else I can fetch last committed data from my source?Select * from table AS OF SCN <Scn no>

    Hi All, I have multiple flat files which i need to load in a single table.I did that using indirect option at session level.But need to dig out on how to populate substring of header in name column in target table. i have two columns Id and Name. in all input file I have only one column 'id' with header like H|ABCD|Date. I need to populate target like below example. File 1                                    File2     H|ABCD|Date.                      H|EFGH|Date.1                                            42                                            5  3                                            6 Target tale: Id    Name1     ABCD2     ABCD3     ABCD4     EFGH5     EFGH6     EFGH can anyone help on what should be the logic to get this data in a table in informatica.

  • Select * from table where rownum 5; no rows returned? why is it so?

    select from table where rownum > 5;*
    no rows returned. why is it so?
    can anyone explain me?

    Hi,
    rownum is pseudo column, and it is based on the sort order.
    For ex, if you want to get the first 5 employees who get the least salary, you use,
    select ename,sal from
        (select ename, sal
         from emp
         order by sal )
        where rownum<=5
    ENAME      SAL                   
    SMITH      800                   
    ALLEN1     890                   
    JAMES      951                   
    TURNER     998                   
    ADAMS      1100Suppose, if you want to use highest salary, you change the order by and not the rownum. So, it becomes,
    select ename,sal from
        (select ename, sal
         from emp
         order by sal desc)
        where rownum<=5
    ENAME      SAL                   
    KING1      5000                  
    FORD       3000                  
    SCOTT      3000                  
    JONES      2975                  
    BLAKE      2850 So, its not the rownum you would want to change, but the order by.
    -Arun

  • Is select from view faster then select from table..???

    Hello Gurus,
    I want to query some data from two tables, both of table have many columns (attributes) and many rows...
    I use several where clauses to retrieve data from those tables..
    witch one is faster, I create a view or I just "select" from those tables???
    Regards.
    Nia...

    riedelme wrote:
    3360 wrote:
    riedelme wrote:
    Selecting through a view almost never helps performance and frequently hurts.Views do not affect performance.
    Views are simply queries and like queries there are fast and slow ones.I disagree.
    First of all, to use a view you are executing a query to get a result set, then accessing the data from that result set - a built-in extra step to perform to get data.First of all that entire explanation of how views work is not correct. The optimizer will rewrite the query to make the view go away if possible.
    SQL> create or replace view v as select * from dual;
    View created.
    SQL> explain plan for select * from dual where dummy = 'X';
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 272002086
    | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("DUMMY"='X')
    13 rows selected.
    SQL> explain plan for select * from v where dummy = 'X';
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 272002086
    | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("DUMMY"='X')
    13 rows selected.Exactly the same.
    >
    Second, when accessing the data from the view the result sets don't have indexes for fast lookups and efficient joins on later steps.This is also known as just making stuff up and is not how the database works. care to share any references at all for any of this?
    >
    Third, the systems I've seen that use views extensively - I am looking at one now - tend to perform joins on views using the same tables with the same data over and over. This is a design issue and not specifically a problem with views but they lend themselves to this misuse much too easilyCorrect as I said a view is just a query, and just like queries there are good fast views and bad slow views
    >
    I'll concede that the problem is not specifically with views themselves but as I just said they lend themselves to misuse. Tuning views of views and views joined to views is difficultYes, queries can be misused as can almost all SQL functions and functionality.
    As I said - Views are simply queries and like queries there are fast and slow ones.
    Nothing that you have posted that is accurate changes that.

  • How to get sum amount (wrbtr) from table BSIS group by gjahr hkont monat

    Hello! I am new to this forum, and to ABAP. Hope my question is not obvious.
    I want to get sum amount from table BSIS, group by year, period an account. Normally I will try a SQL select like this:
    SELECT gjahr monat hkont sum( wrbtr ) as wrbtr INTO CORRESPONDING FIELDS OF TABLE itab FROM BSIS WHERE (itabcond) GROUP BY gjahr monat hkont .
    The problem is that the amount in field wrbtr is all positive. The result gets wrong. The debet/credit indicator shkzg determines what is positive/negative. How to solve this in the most efficient way? Should I read all accounting documents into an internal table, and loop through them, changing the sign, and then make some new select, or is there a better way?
    Regards Dag-Egil
    Message was edited by:
            Dag-Egil Bull Sletholt

    Here are the objects in the trace list:
    SKB1
    USR05
    TRDIR
    SKA1
    SKAT
    SKB1
    BSIS
    PRPS
    BSAS
    V_LTDX
    USR02
    RFBLG
    V_LTDX
    LTDX
    RTXTH
    RTXTF
    RTXTT
    TRDIR
    The most of this call to this tables returns 1-2 records, apart from BSIS, where it returns 10 records. This is the same as accounting documents in the transaction FBL3N. When i try this again with more records returned I still have no call to table GLT0.

  • How to find max(time) from table using group by

    how to find max(time) from table using group by
    select var max(time)
              from table
              into (var1, time1)
               where .....
                 group by var.
    it is fetching record which is top in table.
    if u can help?
    regards.

    No this will fetch the maximum time from teh table.
    select var max(time)
    from table xxxx
    into (var1, time1)
    where .....
    group by var.
    Refer this code
    TABLES SBOOK.
    DATA:  COUNT TYPE I, SUM TYPE P DECIMALS 2, AVG TYPE F.
    DATA:  CONNID LIKE SBOOK-CONNID.
    SELECT CONNID COUNT( * ) SUM( LUGGWEIGHT ) AVG( LUGGWEIGHT )
           INTO (CONNID, COUNT, SUM, AVG)
           FROM SBOOK
           WHERE
             CARRID   = 'LH '      AND
             FLDATE   = '19950228'
           GROUP BY CONNID.
      WRITE: / CONNID, COUNT, SUM, AVG.
    ENDSELECT.

  • Html form for select * from table a

    Hi all,
    I m looking for report on header/footer part from one apex pages
    which will display context from table a, like sql statement
    select * from table a, written in html.
    Is any examples in java script/html select from tables
    (and same insert into table A (...) select * from table b)
    thanks,
    Gordan

    Hello Gordan,
    Do you want to show the whole report or just some data of it?
    If it's for ex just the name and address you could create two items and have a process or computation to fill it with your select statement.
    If it's the whole report, just create the report and put it in for ex. Region 1. In your page template you can specify that Region 1 needs to come into the footer.
    Regards,
    Dimitri
    -- http://dgielis.blogspot.com/
    -- http://apex-evangelists.com/
    -- http://apexblogs.info/

  • Xmlgen.getxml("select * from table") returns null pointer exception

    I am running oracle 8i on solaris server and clinet on windows
    NT and i am this select statement
    select xmlgen.getxml("select * from table") from dual ,its
    returning null pointer exception,i have tried it through
    jdbc,even then its returning xml as
    <?xml version = '1.0'?>
    <ERROR>java.lang.NullPointerException</ERROR>
    can any body tell me the error.Help will be really appreciated.I
    need an urgent response,if some one can guide me please.
    My email is [email protected],if you can give me a quick
    response on this email,your effot will be appreciated.
    thanks
    Masood

    What is actually throwing the NullPointerException? rs.getMetaData() or table.setModel()?

  • How do I do SELECT * FROM TABLE WHERE KEY IN ({list})?

    The title says it all really.
    Is there a reasonable performant way to perform the query
    SELECT * FROM TABLE WHERE KEY IN ({list})where {list} is String []?
    I am currently creating a PreparedStatement with a for loop like this   StringBuffer sb = new StringBuffer ("SELECT * FROM TABLE WHERE ID IN (");
      for (int ii=0;ii<keys.length;ii++) {
        sb.append (keys [ii]);
        if (ii != keys.length-1) sb.append (",");
      sb.append (")");but this means that the prepared statement is created each time I call my method and so I'm not sure that the optimizer will find it easy to cope with. Is there a construction that I'm missing along the lines of SELECT * FROM TABLE WHERE KEY = ? where I can create the PreparedStatement once and just call setObject or something on it when the values in {list} change?

    but this means that the prepared statement is created
    each time I call my method and so I'm not sure that
    the optimizer will find it easy to cope with.You are right, the optimizer won't find that easy to deal with (presuming that is even relevant for your driver/database.) But most optimizers won't do anything with statements that change and that is what you are doing.
    You could create several prepared statements which have a common number of bind variables. For example 10 statements with from 1 to 10 bind values. This will work if most of the queries use those.

Maybe you are looking for

  • Discoverer viewer -- export to pdf doesn't retain page settings.

    Hi, The report output in discoverer viewer is much different from that of plus. I am concerned abt the report headers. In discoverer plus, the headers are left, center and right aligned. But in viewer, every thing is aligned center. How can I get the

  • Refreshing sql report region based on values from another region - 4.0

    Greetings All, I have a page with two regions, say region 1 and region 2. I have a before header process that fetches values from db based on criteria entered from another page. Region 2 is a sql report region with bind variables from region 1. When

  • Reprocess raw

    Hi, In iPhoto if i right click on a photo it gives me the option to reprocess raw, even when i have not modified the photo. I also have auto rotation turned on in my camera and when i reprocess a portrait photo it goes into landscape, should I turn a

  • Customised Option In Planning Web forms.

    Hi All Is there a way in the planning webform, to restrict members showing in the form, for example, I have Entity, Employee, Position. Depending on the entity selection I need to display Employees on the form. Is there anything I have to do from the

  • Organizer crashes during minimization

    Photoshop Elements 9 - 11:  I thought that, maybe, with the new version out that Adobe would have fixed the organizer 'disappearing act', but I guess not.  Finally, when you start Photoshop Elements 11 the organizer actually appears without you havin