Combine two row in sql

Hello Guys,
Is it possible to create this kinda output.
Input:
Table1
Column1
Column
1
A
2
B
3
C
4
D
Table2
A
B
C
D
0
1
0
0
1
0
1
0
0
1
1
1
0
1
0
1
Output:
B
AC
BCD
BD
Actually i want to Combine two different table column deepen where 1 .
thanks

Hello ,
you need to use PIVOT ,See the below sample :
Step 1 :
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('SPIKE', 'MON', 300)
insert into DailyIncome values ('FREDS', 'SUN', 400)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'TUE', 200)
insert into DailyIncome values ('JOHNS', 'WED', 900)
insert into DailyIncome values ('SPIKE', 'FRI', 100)
insert into DailyIncome values ('JOHNS', 'MON', 300)
insert into DailyIncome values ('SPIKE', 'SUN', 400)
insert into DailyIncome values ('JOHNS', 'FRI', 300)
insert into DailyIncome values ('FREDS', 'TUE', 500)
insert into DailyIncome values ('FREDS', 'TUE', 200)
insert into DailyIncome values ('SPIKE', 'MON', 900)
insert into DailyIncome values ('FREDS', 'FRI', 900)
insert into DailyIncome values ('FREDS', 'MON', 500)
insert into DailyIncome values ('JOHNS', 'SUN', 600)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('SPIKE', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'FRI', 300)
insert into DailyIncome values ('JOHNS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'SAT', 800)
insert into DailyIncome values ('SPIKE', 'TUE', 100)
insert into DailyIncome values ('SPIKE', 'THU', 300)
insert into DailyIncome values ('FREDS', 'WED', 500)
insert into DailyIncome values ('SPIKE', 'SAT', 100)
insert into DailyIncome values ('FREDS', 'SAT', 500)
insert into DailyIncome values ('FREDS', 'THU', 800)
insert into DailyIncome values ('JOHNS', 'TUE', 600)
Now :
VendorId IncomeDay IncomeAmount
SPIKE FRI 100
SPIKE MON 300
FREDS SUN 400
SPIKE WED 500
SPIKE TUE 200
JOHNS WED 900
SPIKE FRI 100
JOHNS MON 300
SPIKE SUN 400
SPIKE WED 500
FREDS THU 800
JOHNS TUE 600
Step 3:
select * from DailyIncome
pivot (avg (IncomeAmount) for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) as AvgIncomePerDay
Output :
VendorId MON TUE WED THU FRI SAT SUN
FREDS 500 350 500 800 900 500 400
JOHNS 300 600 900 800 300 800 600
SPIKE 600 150 500 300 200 100 400
More details
Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

Similar Messages

  • Combine two tables T-SQL

    Hi there,
    It might be simple Query for you but I was missing the logic.
    I have Tab A    this does have 4 records.
               Tab B  this does have 14 records.
    Note: those 4 records exists in Tab B
    I want to insert rest of the 10 records into the Tab A.
    Any suggestion pls.
    Thanks,
    Siva 

    Hi,
    Please run the below script and see if it serves your requirement.
    DECLARE @TableA TABLE
    ID INT,
    NAME VARCHAR(10)
    INSERT INTO @TableA 
    VALUES   (1, 'James')
    ,(2, 'Ramz')
    ,(3, 'Chandu')
    ,(4, 'Suraj')
    SELECT *
    FROM @TableA
    DECLARE @TableB TABLE
    ID INT,
    NAME VARCHAR(10)
    INSERT INTO @TableB 
    VALUES   (1, 'James')
    ,(2, 'Ramz')
    ,(3, 'Chandu')
    ,(4, 'Suraj')
    ,(5, 'Krishna')
    ,(6, 'John')
    SELECT *
    FROM @TableB
    Now as per your requirement, you need to insert rows 5 & 6 from TableB into TableA
    INSERT INTO @TableA
    SELECT Tab1.* 
    FROM @TableB Tab1
    LEFT JOIN @TableA Tab2 ON Tab1.id = Tab2.id
    WHERE Tab2.id IS NULL
    The above query gives inserts the non-existing records 
    SELECT *
    FROM @TableA
    Thanks, Satish Chandra

  • Combine two rows in single column data

    hi,
    I have an requirement
    for ex:
    Division              Resourcename   PRojectname    Projectstartdate           Projectenddate    allocated percentage
    development           A                     one                    '01/01/2014'           '03/31/2014'  
               100%
    development           B                     one                    '01/01/2014'           '03/31/2014'  
                100%
    development           C                    one                    '01/01/2014'           '03/31/2014'  
                100%
    development           D                    one                    '01/01/2014'           '03/31/2014'  
                100%
    development           E                   one                    '01/01/2014'             '01/31/2014'  
                    25%
    development           E                  one                    '02/01/2014'             '03/31/2014'  
                    25%
    In the above "Resource E have multiple dates ,will work on separate dates on single project,but the resource allocated percentage will be always same(ie)=25%,so the report output look like
    Division              Resourcename   PRojectname    Projectstartdate           Projectenddate    allocated percentage
    development           A                     one                    '01/01/2014'           '03/31/2014'  
               100%
    development           B                     one                    '01/01/2014'           '03/31/2014'  
                100%
    development           C                    one                    '01/01/2014'           '03/31/2014'  
                100%
    development           D                    one                    '01/01/2014'           '03/31/2014'  
                100%
    development           E                   one                    '01/01/2014'             '01/31/2014'
                   25%
           '02/01/2014'             '03/31/2014'              
    the above SSRS report output look after grouping on division,resource.
    this same output i need from sql query.how to achieve this.
    Thanks
    r.b

    Bhupesh, I do not have any idea how your actual data would look like. So the below snippet can
    only taken as reference not as an actual.
    [Join condition, CASE...WHEN everything would change as per your actual data]
    Try the below:
    create table Bhupesh_test(Division varchar(50), Res_Name varchar(50), Proj_Name varchar(50), startdate date, enddate date, allocated int)
    Insert into Bhupesh_test Values
    ('development','A','one', '01/01/2014','03/31/2014',100),
    ('development','B','one', '01/01/2014','03/31/2014',100),
    ('development','C','one', '01/01/2014','03/31/2014',100),
    ('development','D','one', '01/01/2014','03/31/2014',100),
    ('development','E','one', '01/01/2014','01/31/2014',25),
    ('development','E','one', '02/01/2014','03/31/2014',25)
    ;With cte
    as
    Select *, ROW_NUMBER()OVER(Partition by Division,Res_Name,Proj_Name Order by startdate)Rn
    From Bhupesh_test
    Select Division,Res_Name,Proj_Name,startdate,enddate,allocated From
    Select Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    and A.startdate <>B.startdate Then NULL Else A.Division end Division,
    Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    and A.startdate <>B.startdate Then NULL Else A.Res_Name end Res_Name,
    Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    and A.startdate <>B.startdate Then NULL Else A.Proj_Name end Proj_Name,
    Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    Then A.startdate Else B.startdate end startdate,
    Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    AND A.startdate <> B.startdate Then A.enddate Else B.enddate end enddate,
    Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
    and A.startdate <>B.startdate Then NULL Else A.Allocated end Allocated,1 Ordered
    From cte A
    Inner join cte B on A.Rn = B.Rn+1 and A.Res_Name = B.Res_Name
    Union All
    Select Division,Res_Name,Proj_Name,startdate,enddate,allocated,0 From cte A where Rn=1
    ) A
    Order by Ordered
    Drop table Bhupesh_test

  • How to combine two datatable in c#}

    Hi Everybody,
    I have two datatables I want to combine single output.
    First datatable1 result
    A1    A2
    10   15
    Second datatable2 result
    B1   B2
    5    10
    The final result should be in datatable3
    A1     A2     B1     B2
    10     15     5      10

    Hi,
    SQL Server do not have datatables but tables. Datatables
    (datatable)
    is a dot.Net object. You can read more about DataTable class here: http://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx
    Are you talking about DataTable or tables in database?
    >> Combining two DataTables objects in dot.net can be done with the DataTable.Merge Method.
    >> combining two tables in SQL Server database can be done using the Merge operation in T-SQL (as other mentioned before me)
    [Personal Site] [Blog] [Facebook]

  • How to compare two rows in PL/SQL?

    Hi All,
    How to compare two rows in PL/SQL? Is there any method that I can use instead of comparing them column by column?
    Any feedback would be highly appreciated.

    PhoenixBai wrote:
    Hi All,
    How to compare two rows in PL/SQL? Is there any method that I can use instead of comparing them column by column?What "rows" are you referring to?
    If you're talking of rows within a PL/SQL associative array there are techniques as described in the documentation... e.g.
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    type v1 is table of number;
      3    r1 v1 := v1(1,2,4);
      4    r2 v1 := v1(1,2,3);
      5  begin
      6    if r1 MULTISET EXCEPT DISTINCT r2 = v1() then
      7      dbms_output.put_line('Same');
      8    else
      9      dbms_output.put_line('Different');
    10    end if;
    11* end;
    SQL> /
    Different
    PL/SQL procedure successfully completed.
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    type v1 is table of number;
      3    r1 v1 := v1(1,2,3);
      4    r2 v1 := v1(1,2,3);
      5  begin
      6    if r1 MULTISET EXCEPT DISTINCT r2 = v1() then
      7      dbms_output.put_line('Same');
      8    else
      9      dbms_output.put_line('Different');
    10    end if;
    11* end;
    SQL> /
    Same
    PL/SQL procedure successfully completed.
    SQL>If you're talking about rows on a table then you can use the MINUS set operator to find the rows that differ between two sets of data...
    SQL> select * from emp;
         EMPNO ENAME      JOB              MGR HIREDATE                    SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-1980 00:00:00        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-1981 00:00:00       1600        300         30
          7521 WARD       SALESMAN        7698 22-FEB-1981 00:00:00       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-1981 00:00:00       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-1981 00:00:00       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-1981 00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-1981 00:00:00       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-1987 00:00:00       3000                    20
          7839 KING       PRESIDENT            17-NOV-1981 00:00:00       5000                    10
          7844 TURNER     SALESMAN        7698 08-SEP-1981 00:00:00       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-1987 00:00:00       1100                    20
          7900 JAMES      CLERK           7698 03-DEC-1981 00:00:00        950                    30
          7902 FORD       ANALYST         7566 03-DEC-1981 00:00:00       3000                    20
          7934 MILLER     CLERK           7782 23-JAN-1982 00:00:00       1300                    10
    14 rows selected.
    SQL> select * from emp2;
         EMPNO ENAME      JOB              MGR HIREDATE                    SAL       COMM     DEPTNO
          7521 WARD       SALESMAN        7698 22-FEB-1981 00:00:00       1250        500         30
          7566 JONES      MANAGER         7839 02-APR-1981 00:00:00       2975                    20
          7782 CLARK      MANAGER         7839 09-JUN-1981 00:00:00       2450                    10
          7788 SCOTT      ANALYST         7566 19-APR-1987 00:00:00       3000                    20
          7839 KING       PRESIDENT            17-NOV-1981 00:00:00       5000                    10
          7900 JAMES      CLERK           7698 03-DEC-1981 00:00:00        950                    30
          7934 MILLER     CLERK           7782 23-JAN-1982 00:00:00       1300                    10
    7 rows selected.
    SQL> select * from emp
      2  minus
      3  select * from emp2;
         EMPNO ENAME      JOB              MGR HIREDATE                    SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 17-DEC-1980 00:00:00        800                    20
          7499 ALLEN      SALESMAN        7698 20-FEB-1981 00:00:00       1600        300         30
          7654 MARTIN     SALESMAN        7698 28-SEP-1981 00:00:00       1250       1400         30
          7698 BLAKE      MANAGER         7839 01-MAY-1981 00:00:00       2850                    30
          7844 TURNER     SALESMAN        7698 08-SEP-1981 00:00:00       1500          0         30
          7876 ADAMS      CLERK           7788 23-MAY-1987 00:00:00       1100                    20
          7902 FORD       ANALYST         7566 03-DEC-1981 00:00:00       3000                    20
    7 rows selected.If you actually need to know what columns data is different on "non-matching" rows (based on your primary key) then you'll have to compare column by column.

  • Combining multiple rows to singe row thru SQL Stmt

    Hello,
    I am trying to combine values returned from multiple row into one row,
    thru inner/outer sql or any optimal way.
    In the example i would like to have First name, Last name, email and phone to be
    returned as a single row.
    create table TEMP_AAAAA
      FIRST_NAME VARCHAR2(25),
      LAST_NAME  VARCHAR2(25),
      CON_METHOD VARCHAR2(25),
      CON_VALUE  VARCHAR2(25)
    INSERT INTO TEMP_AAAAA VALUES('TOM','MAC','EMAIL','[email protected]');
    INSERT INTO TEMP_AAAAA VALUES('TOM','MAC','PHONE','12345');Any suggestion in doing it thru sql stmt.
    I have done this thru pl/sql, wondering if this could be achieve thru single SQL Stmt
    DECLARE
    v_FIRST_NAME  VARCHAR2(25);
    v_SECOND_NAME VARCHAR2(25);
    v_EMAIL       VARCHAR2(25);
    v_PHONE       VARCHAR2(25);
    BEGIN
    v_FIRST_NAME := NULL;
    v_SECOND_NAME := NULL;
    v_EMAIL := NULL;
    v_PHONE := NULL;
    FOR IMPL_CUR IN(SELECT * FROM TEMP_AAAAA ORDER BY CON_METHOD DESC)
    LOOP
            IF v_FIRST_NAME IS NULL
            THEN
               v_FIRST_NAME := IMPL_CUR.FIRST_NAME;
            END IF;
            IF v_SECOND_NAME IS NULL
            THEN
               v_SECOND_NAME := IMPL_CUR.LAST_NAME;
            END IF;  
            IF v_PHONE IS NULL AND IMPL_CUR.CON_METHOD = 'PHONE'
            THEN
               v_PHONE := IMPL_CUR.CON_VALUE;
            END IF;
            IF v_FIRST_NAME = IMPL_CUR.FIRST_NAME AND
               v_SECOND_NAME = IMPL_CUR.LAST_NAME AND
               length(v_PHONE) > 0
            THEN
              IF v_EMAIL IS NULL AND IMPL_CUR.CON_METHOD = 'EMAIL'
              THEN
                 v_EMAIL := IMPL_CUR.CON_VALUE;
                 EXIT;
              END IF;       
            END IF;
    END LOOP;
             DBMS_OUTPUT.put_line('firstName...:' || v_FIRST_NAME);    
             DBMS_OUTPUT.put_line('lastName....:' || v_SECOND_NAME);    
             DBMS_OUTPUT.put_line('PHONE.......:' || v_PHONE);
             DBMS_OUTPUT.put_line('EMAIL.......:' || v_EMAIL);
    END;

    Hi Ludy,
    Following query should work -
    P.S. - I have added records for one more person with first name as 'TOM1' and last name as 'MAC1' for testing purpose. Given inserts for these 2 records as well.
    Connected to Oracle Database 11g Release 11.2.0.1.0
    SQL>
    SQL> INSERT INTO TEMP_AAAAA VALUES('TOM1','MAC1','EMAIL','[email protected]');
    1 row inserted
    SQL> INSERT INTO TEMP_AAAAA VALUES('TOM1','MAC1','PHONE','12345');
    1 row inserted
    SQL>
    SQL>
    SQL> SELECT t.first_name
      2        ,t.last_name
      3        ,MAX(decode(t.con_method, 'PHONE', t.con_value, NULL)) phone
      4        ,MAX(decode(t.con_method, 'EMAIL', t.con_value, NULL)) email
      5    FROM temp_aaaaa t
      6   GROUP BY t.first_name
      7           ,t.last_name
      8  /
    FIRST_NAME     LAST_NAME   PHONE   EMAIL
    TOM            MAC         12345   [email protected]
    TOM1           MAC1        12345   [email protected]
    SQL> Hope this helps.
    Cheers,
    - Anirudha
    Edited by: Anirudha Dhopate on Nov 10, 2011 9:12 PM

  • Combine two resultset sidebyside in coulmn using mdx

    hi i am new to mdx,
    i want to combine two select statement resultset (side by side like union) using mdx query,please any body help me to solve this query
    first query:
     SELECT
    { [Last Year] }
    ON COLUMNS,
     { {[Location].[Location].&[7], [Location].[Location].&[12], [Location].[Location].&[11],
    [Location].[Location].&[19], [Location].[Location].&[17], [Location].[Location].&[16],
    [Location].[Location].&[9], [Location].[Location].&[18] },{[Location].[Location].[All]}}
    ON ROWS
    FROM [Cube1]
    WHERE ( [Measures].[Labour %] )
    here [Last Year] is a calculated set
    {STRTOMEMBER("[Date].[Month].&["+ cstr(year(now())-2) +"-11-01T00:00:00]"):
            (STRTOMEMBER("[Date].[Month].&["+ cstr(year(now())-1) +"-10-01T00:00:00]"))}
    2nd query:
    WITH MEMBER [Measures].[Budget ] AS IIF(avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),
                                            avg([Last Year],IIF(([Measures].[Budget]>0),[Measures].[Budget],null)),0.00),
    MEMBER [Measures].[YTD] AS IIF(avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null))<>null,
                                  avg([Last Year], IIF(([Measures].[Labour %]>0),[Measures].[Labour %],null)),0.00),
    FORMAT_STRING = "Standard",
    BACK_COLOR = CASE WHEN [YTD] = 0  THEN /*White*/16777215 /*White*/  
    WHEN [YTD] <= [Measures].[Budget ] THEN 65408
         WHEN [YTD]<= [Measures].[Budget ] +5 THEN 65535
         WHEN [YTD]> [Measures].[Budget ] +5 THEN 255
    END,
    VISIBLE = 1
    SELECT
    {  [Measures].[YTD], [Measures].[Budget ] }
    ON COLUMNS,
    { { [Location].[Location].&[7], [Location].[Location].&[12], [Location].[Location].&[11], [Location].[Location].&[19], [Location].[Location].&[17], [Location].[Location].&[16], [Location].[Location].&[9], [Location].[Location].&[18]
    },{[Location].[Location].[All]} }
    ON ROWS
    FROM [Cube1]
    here   ==> [Measures].[YTD], [Measures].[Budget ] are calculated member
    i want result like in
    coulmns===> ytd,budget,nov,dec,jan,feb.,,,,,,,,,,out
    rows ====> are only locations and total(average of all locations)
    please guide me to get solution like mdx query

    Hi Vsp,
    According to your description, you want to create a calculated member to combine two members, and then set it as Default member, right?
    In SQL Server Analysis Services, we can use the script below to create a calculated members.
    create member currentcube.[Date].[Day of Week].[weekend]
    as
    {[Date].[Day of Week].&[6],[Date].[Day of Week].&[7]
    Every attribute in a dimension in Microsoft SQL Server Analysis Services has a default member, which you can specify by using the
    DefaultMember property for an attribute. This setting is used to evaluate expressions if an attribute is not included in a query. Please refer to the link below to see the detail information about specify a default member.
    http://technet.microsoft.com/en-us/library/ms174822(v=sql.105).aspx
    Regards,
    Charlie Liao
    TechNet Community Support

  • Combine two select query

    Hi All,
    Many thanks for all kind support so far.
    I have two report (sql query)
    I want to add both the sql query
    1st sql query
    select distinct(a.item_number)
          ,a.quantity - b.quantity
    from QUANTITY_ONHAND_ATP a,BACKLOG_ATP_LT_CW b
    where a.item_number = b.item_number
    order by item_number2nd sql query
    select distinct(wb.item_number)
           ,wb.quantity - be.quantity
           ,a.quantity - b.quantity
    from BACKLOG_WEEK_BEFORE_ATP wb
        ,BACKLOG_ATP_ET_CW be
        ,BACKLOG_WEEK_AFTER_ATP a
        ,BACKLOG_ATP_GT_CW b
    where wb.item_number = be.item_number
       and a.item_number = b.item_number
       and wb.item_number = a.item_number
       and be.item_number = a.item_number
       and wb.item_number = b.item_number
       and be.item_number = b.item_number
    order by wb.ITEM_NUMBERIn 1st sql query i have 129 records and in 2nd sql query i have 14 records. I want to combine these two sql query. If the item_number of 1st sql query is not there in 2nd one then display it as it is in 1st sql only.
    Any help how to do that.
    Regards

    Hi,
    the gerenal approach to combine the results of two or more SQL queries would be the
    UNION / UNION ALL
    INTERSECT
    MINUS
    SQL clauses. Union just combines the results; INTERSECT will return the rows which are present
    in both result sets and MINUS will reduce the first result set by the rows of the second.
    Basically it works like this
    select * from emp where deptno = 10
    union all(
    select * from empo where deptno = 20
    All the queries must have identical columns in the result set - same number of columns
    and the same data types. So you would have to add a column to your first SQL query ...
    select distinct(a.item_number) item_number,
    to_number(*,null*) first_value
    ,a.quantity - b.quantity second_value
    from QUANTITY_ONHAND_ATP a,BACKLOG_ATP_LT_CW b
    where a.item_number = b.item_number
    order by item_number
    and in the second query I'd recommend to use the same aliases ....
    Does this help
    -Carsten
    Deutschsprachige APEX-Community: Tipps, Tricks, Best Practice
    http://tinyurl.com/apexcommunity
    SQL und PL/SQL: Tipps, Tricks & Best Practice
    http://sql-plsql-de.blogspot.com
    Twitter: @cczarski

  • Combining two tables without any distinct columns between them

    Folks,

    Hi sidy2j,
    According to your description, we need to verify your table structures, and the actual results which you want to get from your tables. Please post more information for analysis.
    Assume, if you want to a one to one record mapping between  two tables without any common column between them for joining, you can refer to the following scripts to implement your requirement. For more information, see:
    http://sqlhints.com/2013/09/07/joining-two-tables-without-any-common-column-between-them-sql-server/
    If you want to match every row in the first table with every row in the second table, you can refer to the following detail.
    http://stackoverflow.com/questions/1198124/combine-two-tables-that-have-no-common-fields
    Hope it can help you.
    Regards,
    Sofiya Li
    Sofiya Li
    TechNet Community Support

  • Replacing one column value between two rows..help??

    HI,
    I have table T
    it has five columns, all are in number data type
    Col1 and col2 are jointly primary key i.e. they cannot be repeated combined
    when i run this query..
    SQL> Select * from t
    it shows me the data like this
    Col1        Col2              Col3       Col4           Col5
    1             1               78          58            12.76
    2             1               128         446           32.10
    3             1               468         566           52.10
    4             1               878         58            52.05
    5             1               987         348           22.02
    ... so on.
    my requirement is that i want to update this table by replacing the col1 value between any two rows
    suppose this is the first row
    Col1        Col2          Col3        Col4         Col5
    1             1             78          58        12.76
    and this is any 2nd row
    Col1        Col2          Col3        Col4        Col5
    5             1            987         348       22.02
    now i want that for first row the value of col1 replaces the value of col1 of 2nd row
    and for 2nd row the value of col1 replaces the value of col1 of 1st row
    i.e.
    Col1        Col2          Col3        Col4         Col5
    5             1            78           58         12.76
    Col1        Col2          Col3        Col4         Col5
    1             1            987        348          22.02
    please tell how to achieve this in single query/procedureregards,
    Ramis

    SQL> create table t (id1 number, id2 number, name varchar2(1));
    Table created.
    SQL> alter table t add primary key (id1,id2);
    Table altered.
    SQL> insert into t values(1,1,'A');
    1 row created.
    SQL> insert into t values(5,1,'B');
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select * from t;
           ID1        ID2 N
             1          1 A
             5          1 B
    SQL> var val1 number
    SQL> var val2 number
    SQL> exec :val1 := 1;
    PL/SQL procedure successfully completed.
    SQL> exec :val2 := 5;
    PL/SQL procedure successfully completed.
    SQL> update t set id1 = (:val1 + :val2) - id1 where id1 in (:val1,:val2);
    2 rows updated.
    SQL> select * from t;
           ID1        ID2 N
             5          1 A
             1          1 B
    SQL> rollback;
    Rollback complete.
    SQL> select * from t;
           ID1        ID2 N
             1          1 A
             5          1 B
    SQL> commit;
    Commit complete.
    SQL> update t set id1 = decode(id1,:val1,:val2,:val1)
      2  where id1 in (:val1, :val2);
    2 rows updated.
    SQL> select * from t;
           ID1        ID2 N
             5          1 A
             1          1 BRgds.

  • Query help first two rows

    i need to sort desc order by date and need to take two rows only
    id date
    1 1/2/1994
    2 2/2/2000
    3 1/5/88
    4 6/5/2006
    i need out put like this
    id date
    4 6/5/2006
    2 2/2/2000
    plz give query with good perfomence thanks

    You can use any one of the below query which performs better for you
    SQL> with t
      2  as
      3  (
      4  select 1 id,to_date('1/2/1994','dd/mm/yyyy') dt from dual
      5  union all
      6  select 2,to_date('2/2/2000','dd/mm/yyyy') from dual
      7  union all
      8  select 3,to_date('1/5/88','dd/mm/yyyy') from dual
      9  union all
    10  select 4,to_date('6/5/2006','dd/mm/yyyy') from dual
    11  )
    12  select * from
    13  (
    14  select * from t
    15  order by 2 desc
    16  )
    17  where rownum <=2
    18  /
            ID DT
             4 06-MAY-06
             2 02-FEB-00
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=9 Card=2 Bytes=24)
       1    0   COUNT (STOPKEY)
       2    1     VIEW (Cost=9 Card=4 Bytes=48)
       3    2       SORT (ORDER BY STOPKEY) (Cost=9 Card=4 Bytes=48)
       4    3         VIEW (Cost=8 Card=4 Bytes=48)
       5    4           UNION-ALL
       6    5             FAST DUAL (Cost=2 Card=1)
       7    5             FAST DUAL (Cost=2 Card=1)
       8    5             FAST DUAL (Cost=2 Card=1)
       9    5             FAST DUAL (Cost=2 Card=1)
    Statistics
              1  recursive calls
              0  db block gets
              0  consistent gets
              0  physical reads
              0  redo size
            416  bytes sent via SQL*Net to client
            496  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
              2  rows processed
    SQL> with t
      2  as
      3  (
      4  select 1 id,to_date('1/2/1994','dd/mm/yyyy') dt from dual
      5  union all
      6  select 2,to_date('2/2/2000','dd/mm/yyyy') from dual
      7  union all
      8  select 3,to_date('1/5/88','dd/mm/yyyy') from dual
      9  union all
    10  select 4,to_date('6/5/2006','dd/mm/yyyy') from dual
    11  )
    12  select id,dt
    13  from
    14  (
    15  select id,dt,row_number() over(order by dt desc) rn from t
    16  order by 2 desc
    17  )
    18  where rn <=2
    19  /
            ID DT
             4 06-MAY-06
             2 02-FEB-00
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=8 Card=4 Bytes=100
       1    0   VIEW (Cost=8 Card=4 Bytes=100)
       2    1     WINDOW (SORT PUSHED RANK) (Cost=8 Card=4 Bytes=48)
       3    2       VIEW (Cost=8 Card=4 Bytes=48)
       4    3         UNION-ALL
       5    4           FAST DUAL (Cost=2 Card=1)
       6    4           FAST DUAL (Cost=2 Card=1)
       7    4           FAST DUAL (Cost=2 Card=1)
       8    4           FAST DUAL (Cost=2 Card=1)
    Statistics
              1  recursive calls
              0  db block gets
              0  consistent gets
              0  physical reads
              0  redo size
            416  bytes sent via SQL*Net to client
            496  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
              2  rows processed

  • Two rows as one display the data in one row

    Hello All,
    I would like to have data of two rows put in one as:
    Lets say the data is like this:
    SELECT * FROM TEST;
          COL1       COL2       COL3 C
           1.5          3          1 M
                        2          1 C
           2.5                     2 C
                      3.5          2 M
                      4.5          3 M How do i get the output as :
           COL1       COL2       COL3 COL4 COL5
           1.5          3          1 M      2
           2.5                     2 C      3.5
                      4.5          3 M If there are two rows with same value in Col3, then col5(a new dummy column in select stmt) the second row's col2 should be disaplyed in Row 1.
    Can we do it in a Select Statement ?
    Regds,
    Amkotz

    A try :
    SQL> select * from Amkotz;
          COL1           COL2          COL3 C                                             
           1,5              3             1 M                                             
                            2             1 C                                             
           2,5                            2 C                                             
                          3,5             2 M                                             
                          4,5             3 M                                             
    SQL> select a.col1,
      2         a.col2,
      3         a.col3,
      4         a.col4,
      5         max(decode(a.rowid,b.rowid,null,b.col2)) keep (dense_rank last order by a.col1,a.col2) as col5
      6  from   (select Amkotz.*, row_number() over (partition by col3 order by col1,col2) rn from Amkotz) a, Amkotz b
      7  where  a.rn=1
      8  and    a.col3=b.col3
      9  group by a.col1,
    10         a.col2,
    11         a.col3,
    12         a.col4;
         COL1       COL2     COL3 C COL5      
          1,5          3        1 M 2         
          2,5                   2 C 3,5       
                     4,5        3 M            Nicolas.

  • How to combine two datarows (business component data) in BI Publisher

    Hi ,
    We are using BI Publisher in Siebel Environment.
    We have data coming from two business components (like from 2 diff tables)
    a) <?for-each:ssTest1?>
    b) <?for-each:ssTest2?>
    ssTest1 and ssTest2 are the business components
    We need to combine these 2 datarows (a&b) and show the data into a single combined data row for ex like <?for-each:ssTest1ssTest2?> and show all the fields in that.
    I'm not sure how we can combine these two data rows into a single combined data row and show the data.
    Any help from any one would be apprecated.
    Thanks
    PV
    Edited by: user8633002 on Oct 21, 2010 4:05 PM

    Hi sajid
    There was nothing more description about your issue in this site and I found an issue below is mostly like yours
    http://www.codeproject.com/Questions/855487/how-to-combine-two-table-value-in-rdlc-report
    In the issue above, if you want to show the two other tables in the report, I think you could combine the tables into one datatable joining on key. The link below show an example of a DataSet Helper from Microsoft about combine DataSets. Take note of
    the related content for other DataSet Helper examples. And then you could use the datatable in your RDLC.
    # HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET
    http://support.microsoft.com/kb/326080/en-us
    In an alternative way, I think you could create a view in the database which combine your tables and use it in your rdlc.
    In addition, your issue is about asp.net and you could get more support in the asp.net forum whose link as below.
    http://forums.asp.net
    Best Regards
    Edward
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a
    href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

  • How to combine +two fields without submitting page (via AJAX/HTMLDB_Get)?

    Hi all,
    After my last contribution "Request for Scott Spendolini" I was able to create the scripts to update fields in forms without having to refresh the page. Thanks again to Scott, Carl and the others who helped me solving my problem.
    I have another question regarding this however. How do I combine two (or more) values on a form to fill a field with the values, for instance to create a unique Primary-key field?
    This is the example: combine the postcode and the house-number (and maybe an extension to the housenumber) in one field 'adrescode'. This uniquely identifies an adress in the Netherlands. I can 'get' the postcode to fill the first part of the adrescode, but how do I modify the scripts to 'get' the housenumber (huisnummer) and - if not left null - the extension to the housenumber?
    Here are the scripts I created to far:
    in the region source of the page I have the following Javascript:
    <script language="JavaScript1.1" type="text/javascript">
    function f_P52_getAdrescode ()
    var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=P52_getAdrescode',0);
    get.add('P52_POSTCODE',html_GetElement('P52_POSTCODE').value)
    gReturn = get.get();
    if(gReturn)
    {  html_GetElement('P52_ADRESCODE').value = gReturn  }
    else
    {  html_GetElement('P52_ADRESCODE').value = 'null'  }
    get = null;
    </script>
    and in the shared components of the application I have created an process P52_getAdrescode as follows:
    declare
      l_adrescode varchar2(16);
    begin
        l_adrescode := :P52_POSTCODE || :P52_HUISNR ;
        if :P52_HUISNRTOEVOEGING is null then
              NULL ;
        else
              l_adrescode := l_adrescode ||':'||:P52_HUISNRTOEVOEGING ;
        end if ;
        htp.prn(l_adrescode);
    -- dbms_output.put_line(l_adrescode);
    end;
    I ran this PL/SQL in the SQL-section of HTMLDB and it does what a expect: combine the values in a new string.
    In my page however, it only gets the value of postcode and puts this into the adrescode field.
    So my question is: how do I modify the Javascript (I Guess) so that also the fields 'huisnummer' and 'huisnummertoevoeging' end up in the adrescode field?
    Thanks for your help,
    Jan.

    Carl, Scott,
    I have two pages with exact the same fields and codings (as described before in this Post.
    I now have two different named functions and application processes, a javascript an process for each page. They both do the same thing, use the same code except for the page-number (:P52_ and :P92). I tried to make one process to replace both, but ran into problems when I changed the name in the get.add statement.
    Changing the get.add from: get.add('P52_POSTCODE',html_GetElement('P52_POSTCODE').value) to: get.add('POSTCODE',html_GetElement('P52_POSTCODE').value) always ends up in what looks like a complete html-(source)dump of the page in the field that has to be updated. Even when I replace the :P52_POSTCODE in the application process as well Changing back the application process and the javascript to so that both javascript and process use the :P52_ again and the function returns the correct value again. What am I doing wrong?
    I requested an online-workspace to post the application to it, but have no answer to my request yet, so I can't provide a working example at the moment....
    Thanks for your help again,
    Jan.

  • Merge Two Rows of a table to One row but into two columns

    Hi
    I Am struck in writing a query to merge two rows into two columns of one row.
    Here is the Sample data i am working with,
    Col 1     Col 2     Col3 Col4 Col Col6
    5000     573-3000 2     0     Phone      
    5000     573-3036 1     0          Fax
    5000     893-5703 3     0     WOrk      
    3000     232-5656     1     0     Phone     
    3000     353-5656     2     0          FAx
    Here Col,Col3,Col4 form the Key.
    now wht i am trying to do is to Merge these type of rows put them into Columns P,F,W,E respectively to achive a Structure as below
    Col1      P     F     W
    5000     573-3000      573-3036      893-5703
    3000     232-5656     353-5656     
    Can you please help me how could i do this.
    I am pretty ordinary at writing SQL's.
    Thanks a Lot in Advance
    Message was edited by:
    Sreebhushan

    Search the forum for PIVOT and you'll find plenty of examples.

Maybe you are looking for