Query to add a column in between existing cols of a table?

HI All,
I have two questions.
1. Query to add a column in between existing cols of a table? (not at the end. This is view of an order of output fields in a report)
2. How do I swap the contents of two columns in a table. Suppose in a table tab there are 2 cols , col1,col2 populated with some data.
I need a query(probably) to swap the col1 and col2 values . NOT AS A RESULT SET, BUT IT NEEDS TO GET CHANGED IN THE TABLE.
Please help !

> 1. Query to add a column in between existing cols of
a table? (not at the end. This is view of an order of
output fields in a report)
Not really sensible ito DBMS - it does not care how you want to view the data. The sequence and formats of columns are what you/the application need to specify in the SQL's projection clause.
Also keep in mind to achieve this, the DBMS will need to rewrite the entire table to fit this new column in-between existing columns. This is not the best of ideas.
The projection of rows is dealt with SQL statements - not with the physical storage implementation.
> 2. How do I swap the contents of two columns in a
table. Suppose in a table tab there are 2 cols ,
col1,col2 populated with some data.
I need a query(probably) to swap the col1 and col2
values . NOT AS A RESULT SET, BUT IT NEEDS TO GET
CHANGED IN THE TABLE.
This seems to work:
SQL> create table foo_tab( c1 varchar2(10), c2 varchar2(10) );
Table created.
SQL> insert into foo_tab select TO_CHAR(rownum), TO_CHAR(object_id) from user_objects where rownum < 11;
10 rows created.
SQL> commit;
Commit complete.
SQL> select * from foo_tab;
C1 C2
1 55816
2 55817
3 55818
4 55721
5 105357
6 105358
7 105359
8 105360
9 105361
10 60222
10 rows selected.
SQL> update foo_tab set c1=c2, c2=c1;
10 rows updated.
SQL> select * from foo_tab;
C1 C2
55816 1
55817 2
55818 3
55721 4
105357 5
105358 6
105359 7
105360 8
105361 9
60222 10
10 rows selected.
SQL>

Similar Messages

  • How to modify an existing query to add a column

    Hi all, I have an existing query :
    SELECT 2 as SOURCE_SYSTEM_ID, LI.IPROD AS SYSTEM_ID, LI.IPROD AS PART_NUMBER, LI.IDESC AS PART_DESCRIPTION, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE (LZ.CCTABL = 'SIRF2') AND LI.IREF02 = LZ.CCCODE), '') AS PLANT_CODE, LIS.ISSTSD
    AS lifecycle_status, CASE LI.ILDTE WHEN 0 THEN convert(date, GETDATE()) ELSE (SUBSTRing(convert(varchar, LI.ILDTE), 1,4)+'-'+SUBSTRing(convert(varchar,LI.ILDTE), 5,2)+'-'+SUBSTRing(convert(varCHAR,LI.ILDTE), 7,2)) END AS LAST_ACTIVITY_DATE,
    LZPA.DATA AS PRODUCTION_TYPE, (CASE (LI.IITYP) WHEN '6' THEN 'SERVICE' ELSE 'ITEM' END) AS ITEM_TYPE, LI.IVEND AS PRIMARY_VENDOR, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMS AND LZ.CCLANG = '') AS
    UOM_INVENTORY, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMS AND LZ.CCLANG = '') AS UOM_BOM, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI. IUMR
    AND LZ.CCLANG = '') AS UOM_SELL, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMP AND LZ.CCLANG = '') AS UOM_PURCHASE, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='BUYER'
    AND LZ.CCCODE=LI.IPURC), '') AS BUYER, (LI.IMHIGH) AS ITEM_HEIGHT, SUBSTRING(IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='SIRF5' AND LZ.CCCODE=LI.IREF05), ''), 1, 20) AS APPLICATIONS, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC
    LZ WHERE LZ.CCTABL='SIRF3' AND LZ.CCCODE=LI.IREF03), '') AS PORGRAM, (LI.IMWIDE) AS ITEM_WIDTH, LI.IMLONG AS ITEM_DEPTH, LI.IWGHT AS ITEM_WEIGHT_NET, LI.IMNNWU AS ITEM_WEIGHT_TARE FROM HCMEUP01.FRDA385A.LBPCSF.IIM LI, HCMEUP01.FRDA385A.LBPCSF.ISC LIS,HCMEUP01.FRDA385A.LBPCSF.ZPA
    LZPA WHERE LI.ICOND = LIS.ISSTSC AND LZPA.PKEY=('ITEMTYP'+LI.IITYP )
    Now I need to add a column called WORK_CENTER. I added a subquery to the existing one.  It looks like this:
    SELECT 2 as SOURCE_SYSTEM_ID, LI.IPROD AS SYSTEM_ID, LI.IPROD AS PART_NUMBER, LI.IDESC AS PART_DESCRIPTION, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE (LZ.CCTABL = 'SIRF2') AND LI.IREF02 = LZ.CCCODE), '') AS PLANT_CODE, LIS.ISSTSD
    AS lifecycle_status, CASE LI.ILDTE WHEN 0 THEN convert(date, GETDATE()) ELSE (SUBSTRing(convert(varchar, LI.ILDTE), 1,4)+'-'+SUBSTRing(convert(varchar,LI.ILDTE), 5,2)+'-'+SUBSTRing(convert(varCHAR,LI.ILDTE), 7,2)) END AS LAST_ACTIVITY_DATE,
    LZPA.DATA AS PRODUCTION_TYPE, (CASE (LI.IITYP) WHEN '6' THEN 'SERVICE' ELSE 'ITEM' END) AS ITEM_TYPE, LI.IVEND AS PRIMARY_VENDOR, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMS AND LZ.CCLANG = '') AS
    UOM_INVENTORY, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMS AND LZ.CCLANG = '') AS UOM_BOM, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI. IUMR
    AND LZ.CCLANG = '') AS UOM_SELL, (SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='UNITMEAS' AND LZ.CCCODE=LI.IUMP AND LZ.CCLANG = '') AS UOM_PURCHASE, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='BUYER'
    AND LZ.CCCODE=LI.IPURC), '') AS BUYER, (LI.IMHIGH) AS ITEM_HEIGHT, SUBSTRING(IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC LZ WHERE LZ.CCTABL='SIRF5' AND LZ.CCCODE=LI.IREF05), ''), 1, 20) AS APPLICATIONS, IsNULL((SELECT LZ.CCDESC FROM HCMEUP01.FRDA385A.LBPCSF.ZCC
    LZ WHERE LZ.CCTABL='SIRF3' AND LZ.CCCODE=LI.IREF03), '') AS PORGRAM, (LI.IMWIDE) AS ITEM_WIDTH, LI.IMLONG AS ITEM_DEPTH, LI.IWGHT AS ITEM_WEIGHT_NET, LI.IMNNWU AS ITEM_WEIGHT_TARE,
    (SELECT LC.LWRKC FROM HCMEUP01.FRDA385A.LBPCSF.LCR LC WHERE RTRIM(LTRIM(LC.LITEM)) = RTRIM(LTRIM(LI.IPROD)) ) AS WORK_CENTER FROM HCMEUP01.FRDA385A.LBPCSF.IIM LI, HCMEUP01.FRDA385A.LBPCSF.ISC LIS,HCMEUP01.FRDA385A.LBPCSF.ZPA LZPA WHERE
    LI.ICOND = LIS.ISSTSC AND LZPA.PKEY=('ITEMTYP'+LI.IITYP )
    Please see the bolded and italic part.   The problem is that the subquery is non distinct.  I am getting an error "
    Msg 512, Level 16, State 1, Line 2
    Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
    Can you let me know how I can resolve this issue? it is an urgent request.  Thank you
    Thank You Warmest Fanny Pied

    I was given the existing query and the subquery as they were.  The subquery is as it is returning multiple records on the columns selected.  The person who give me the sub query is sure those are the correct tables to use to develope/change the
    query. 
    select
      DISTINCT a.litem,
    b.IPROD,b.idesc,a.lwrkc
    from HCMEUP01.FRDA385A.LBPCSF.LCR
    a, HCMEUP01.FRDA385A.LBPCSF.IIM
    b
    where
    a.litem=b.iprod
    Thank you for your help.
    Thank You Warmest Fanny Pied

  • Query Help - Add a column that doesn't exist in table

    Hello,
    i have an application that runs a query in code off of a main query, and that query in code requires a column that does not exist in the main query that I created. how do I go about adding a column to a query that does not actually exist in the table or database that I am querying?
    For Example:
    SELECT PROJ_ID, PROJECT_NAME
    FROM PROJECT
    I need a column called "DELETE_SESSION_ID" and this database/table does not contain that column anywhere. Is it best to create a view that creates the column somehow? I would prefer not having to create the column inside of the table. Any ideas are greatly appreciated.
    Thanks,
    Jeff

    user10249614 wrote:
    Hello,
    i have an application that runs a query in code off of a main query, and that query in code requires a column that does not exist in the main query that I created. how do I go about adding a column to a query that does not actually exist in the table or database that I am querying?
    For Example:
    SELECT PROJ_ID, PROJECT_NAME
    FROM PROJECT
    I need a column called "DELETE_SESSION_ID" and this database/table does not contain that column anywhere. Is it best to create a view that creates the column somehow? I would prefer not having to create the column inside of the table. Any ideas are greatly appreciated.
    Thanks,
    JeffYou need a way to obtain the value you need - a function to generate it, perhaps. A view using such a function or aquiring the value by other means should work well.
    Pipelined functions work well to generate values that aren't in the database at run time - if your version of Oracle supports them

  • I want a sql query to diffrentiate the column names between two schemas

    The structure of the dba_tab_cols table is owner, table_name, column_name, data_type,.....
    So I have a ACCT_ALT_ID table under owner SEODS01 and in that therse are column names
    IBD_ID
    ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID
    DATA_GRP_CDE
    UPDT_DTE
    AND I have a ACCT_ALT_ID table under owner SEODS02 and in that therse are column names
    IBD_ID
    ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID
    DATA_GRP_CDE
    CRTE_PGM
    CRTE_TSTP
    UPDT_PGM
    UPDT_TSTP
    so I want select query to display like this
    SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
    IBD_ID IBD_ID
    ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID EODS_ACCT_ID
    DATA_GRP_CDE DATA_GRP_CDE
    UPDT_DTE CRTE_PGM
    CRTE_TSTP
    UPDT_PGM
    UPDT_TSTP
    AND ALSO I NEED ONE MORE QUERY THAT I DO NOT WANT DISPLAY HAVING SAME COLUMN_NAME LIKE ACCT_ALT_ID_CNTX_CDE ,EODS_ACCT_ID,IBD_ID
    DATA_GRP_CDE

    Hi,
    Welcome to the forum!
    876793 wrote:
    The structure of the dba_tab_cols table is owner, table_name, column_name, data_type,.....
    So I have a ACCT_ALT_ID table under owner SEODS01 and in that therse are column names
    IBD_ID
    ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID
    DATA_GRP_CDE
    UPDT_DTE
    AND I have a ACCT_ALT_ID table under owner SEODS02 and in that therse are column names
    IBD_ID
    ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID
    DATA_GRP_CDE
    CRTE_PGM
    CRTE_TSTP
    UPDT_PGM
    UPDT_TSTP
    so I want select query to display like this
    SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
    IBD_ID IBD_ID
    ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID EODS_ACCT_ID
    DATA_GRP_CDE DATA_GRP_CDE
    UPDT_DTE CRTE_PGM
    CRTE_TSTP
    UPDT_PGM
    UPDT_TSTPYou may have noticed that this site normally doesn't display multiple spaces in a row.
    Whenever you post formatted text (such as query results) on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
    That way, your message will look like this:SEODS1_ACCT_ALT_ID SEODS02_ACCT_ALT_ID (COLUMN HEADER)
    IBD_ID IBD_ID
    ACCT_ALT_ID_CNTX_CDE ACCT_ALT_ID_CNTX_CDE
    EODS_ACCT_ID EODS_ACCT_ID
    DATA_GRP_CDE DATA_GRP_CDE
    UPDT_DTE CRTE_PGM
    CRTE_TSTP
    UPDT_PGM
    UPDT_TSTP
    This sounds like a jon for a FULL OUTER JOIN:SELECT     s1.column_name     AS seods1_acct_alt_id
    ,     s2.column_name     AS seods2_acct_alt_id
    FROM          dba_tab_cols     s1
    FULL OUTER JOIN     dba_tab_cols     s2 ON s1.column_name     = s2.column_name
    WHERE     s1.owner     = 'SEODS1'
    AND     s1.table_name = 'ACCT_ALT_ID'
    AND     s2.owner     = 'SEODS2'
    AND     s2.table_name = 'ACCT_ALT_ID'
    AND ALSO I NEED ONE MORE QUERY THAT I DO NOT WANT DISPLAY HAVING SAME COLUMN_NAME LIKE ACCT_ALT_ID_CNTX_CDE ,EODS_ACCT_ID,IBD_ID
    DATA_GRP_CDESorry, I don;t understand.
    Post the results you want (formatted, between \ tags, of course).
    Do you want only the column names that appear in one table or the other, but not in both?
    In that case, you can add a condition like this to the WHERE clause:
    AND       (   s1.column_name  IS NULL
           OR  s2.column_name  IS NULL
           )

  • How to add a column to an existing report

    Hi,
    I have a report which I need to modify (add a date column). I included the date in the query but it doesnt show up when I run the report.
    Where else do I have to make changes and how?
    I need some help.
    Thank you all so very much.
    A/A

    Hi,
    mhh, how should we know what you've done? Could it be that you tried to arrange the fields to bring the new field at the right position and you not only arranged them to the top, but additionally to the left ????
    Regards
    Rainer

  • Optimized query to find Min and max of a col in a table

    I have a table doc_boe_rec with record count 12375934
    the primary key columns are (boe_rec_id,psd_serial_num).
    No other ndexes are present on this table.
    I want an optimized query which will give both the results :
    1.Min boe_rec_id (boe_rec_id from 1st record)
    2.Max boe_rec_id from this table with rows limited to a value say 5000.
    i.e (boe_rec_id from 5000th column value from table )
    Thanks
    Manoj

    1.Min boe_rec_id (boe_rec_id from 1st record)It is confusing for me. The min value for the first, hmmm...
    2.Max boe_rec_id from this table with rows limited to a value say 5000.Not more clear...
    Please details your requirements.
    Nicolas.

  • Add columns in the existing Customer table in a Subject Area

    Hi All,
    Well I need to add two columns from Sales table(new table i created which has customer Id) and join it to the Customer Table which has the Customer Id and several columns. The Customer table was already in the RPD which has all the customer information. Now that i have this new requirement to add few columns from the Sales table to the Customer table in one Subject Area.
    What i have done so far is that I imported the sales table into the rpd, made a join in PHYSICAL LAYER on the customer ID of the Sales table to the Customer id of the Customer table and simply dragged the sales table to the Customer LTS in the BMM layer(NO MORE JOINS IN BMM)and dragged and dropped the same into the Presentation layer. The customer table is linked to one fact table in that subject area. So i thought that dragging the sales table columns in the customer table LTS will work because customer is linked to one fact in that subject area.
    Now, In the Answers when I dragged columns from customer & sales table I see only one record that is the first customer_id record from my Sales table but i know there are many common customer_ids between those two tables but its only picking the first customer id from the sales table.
    Any help will be appreciated..
    Normally if we need to add some columns to the existing tables in one subject area what is the best approach??

    Hi Balajee,
    SAPMM06E 0111 CUSTSCR1 SAPLXM06 0111 Subscreen: PO item
    for more information check with these links
    ADDING NEW FIELDS IN me21n
    ADDING NEW FIELDS IN me21n - (Screen exit / enhancment)
    hope this will useful to you.
    Regards!

  • Add additional column in existing table

    Hello guys,
    I need to add new columns to the existing table, but when I'm trying to add, nothing happens. Checked table itself and all Rows and Column fields are read-only. Can't see any property which indicates this locking behavior. I'm using ES2 LC.
    Thanks,
    Uldis

    You can use an APPEND function.
    Append Structures (SAP Library - Tables)

  • Is it possible to add new columns with format "Text" once a table is linked to a form

    Is it possible to add new columns with format "Text" once a table is linked to a form in Numbers for iPhone or is it impossible and thus a serious bug?(Rating stars and numeric vales seem to work.)
    Those bugs happen both for new speadsheets as well as existing onces, like the demo. When you are in the form only the numeric keyboard shows up.
    Pat from the Apple Store Rosenstrasse/Germany approved that it looks like a Bug during the Numbers Workshop I was in: It is not possible to add new columns with format "Text". I reported the error for Version 1.4 but there is no update nor do I get statement of understanding the issue.

    Hi Knochenhort,
    I see what you are talking about now. Without knowing how the program actually works, I think this is what's going on:
    When you add a new column to an already existing table (with already existing formats), the new cells come already formatted like the previous column. So when you add a column to the end of the demo table, the cells are already formatted like stars, and when you add a column to the beginning, they're already formatted like number.
    I think this is why it's different when if you add columns to a table with blank (unformatted) columns. In that case, the new cells aren't already "tainted" with a set format, so you can change to text format without issue.
    It seems like the problem is that you can't format cells that are already formatted as "number" as "text" format (even if it doesn't look like they are, because they are blank). IMO, this is a bug! This is why you don't see this issue when adding columns to a new table, because the new cells don't already come with a format.
    To workaround, you can highlight the body cells after adding the new column, and delete the cells. This will "clear" the formatting, so you can then go in the inspector, format them as text, and the correct keyboard will pop up.
    Hope that helps!

  • HOW TO ADD NEW COLUMN IN EXISTING QUERY

    hi,
    i m working on oracle reorts 10g. i have a multiple query based report. now i want to add a new column in query 1 and order by on that new column .
    i added column in query but when i see that group i dont find that column in that group.wt do i need to do to visible that column in query1 group thats y the order by on that column is not working i think.
    tell how to solve it ?
    thanks

    Hi,
    The new column you have added might be in the least level group of that query.
    Drag it to the desired group level, first postion and set the break order for that item in the property pallette..
    (If still the item not coming, check whether you have put 'comma' in the select query after writing the column name)
    Simply giving the column in the order-by clause will not work in report.
    In Report, record display order is completely based on the break-order of the items given in query-groups
    If you change accordingly it will work
    Regards
    Dora

  • How to add a new column in between two other columns

    I need to add a new column in between two other existing columns. Does anyone know how to do that? The ALTER statement adds the column at the end. Can someone give an example.
    Thanks
    Murali
    null

    There is no simpler way of doing this because it is not strictly necessary: you can always see your data in the order you want simply by going SELECT col1, col2, col98, col4, etc. Thus the column order only matters when you execute SELECT * FROM...
    If that really is important enough to you you'll have to drop and re-create the table, for which there are a number of different strategies.
    Your final comment suggests that you don't retain your database build scripts. If you did then rebuilding your constraints would be painless. There is a lesson there. The good news is that if you have TOAD (download from www.quest.com) you can generate DDL scripts from your existing schema.
    HTH, APC

  • How to add an unique column to an existing table?

    How to add an unique column to an existing table?
    I have a large table which has no unique constraint. and I want to add an unique column for it. How to do it?
    Does adding a sequence is a good choice? How to do it?
    Thank you

    Hi,
    alter table tablename
    add constraint contraint_name unique (columnname);but before that you need to check in the table.column there is no duplicate record exist.
    Does adding a sequence is a good choice?
    Your talking about unique constraint then yes.
    Regards,
    Taj

  • How to add one column in existing search help.

    Hi Folks,
    My quesion is
    How to add one column in existing search help and also Now search help on that field is not an explicit search help. It should be implement using check table.
    Shivam

    Hi,
    If you want to add a field in Elementary search help, get the search help name for the and go to change mode and add the field in it.
    If you want to add a field in collective search help, go to included search helps tab and a new search help name and add the fields to it.
    I think this should help you to certain extent.
    Regards,
    Kranthi
    Edited by: Kranthi on Jan 14, 2010 11:15 AM

  • How to add columns to an existing dimension on the fly?

    Hey,
    1) I already have a cube which is attached to three dimensions. Now if I wanted to add a few columns in the dimensions once they have been deployed, I find it difficult as the changes in the logical model and the physical table is not synchronized. Can some one please let me know, if adding columns to existing dimensions is possible?
    2) I also had one more doubt. I have a fact table which has columns by the name parameter name and parameter value. Now once it is loaded I want to make certain calculations using a few parameters values and store them in the same fact table in the form of a new parameter name and the derived parameter value. How can I go about doing this part in OWB.
    Thanks

    1. Yes you can add existing columns to a dimension, there are a couple of approaches; either add the columns to tje underlying table and then change the dimension to manual binding and add attributes to represent the columns. Or edit the dimension and regenerate the implementation table.
    2. Could these not be just additional measures that you calculate in the cube load?
    Cheers
    David

  • Add new Column to existing Primary Key

    Hi,
    I have a Primary key consisting of 6 columns. Now i want to add new 7th column to that existing key.
    Do i need to drop the existing primary key and then recreate agian with 7th field or is there any other way?
    Regards,
    Jayesh

    I think what Joel was really saying is that dropping/recreating a PK is the only way to change the PK column list in version 6 - 9. Who knows about in 10g? Though I doubt that this has changed.
    If the table only has one or two more columns that are not part of the key you might consider converting the table to being an IOT (indexed organized table).
    HTH -- Mark D Powell --

Maybe you are looking for