Need to find the Difference between two table

Hello ,
I have stucked in program as below scenario:-
I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
Example TableA
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
80 DEF 6005 YY 446 YY8
TableB
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
81 DEF 6005 Yu 447 YY8
I need to the out put like this :-
The Diffence between two table is
TableA.COL1=80 TableB.Col1=81, Different
TableA.Col4=YY TableB.col4=Yu,Different
TableA.Col5=446TableB.col5=447,Different
Please suggest me to write the pl/sql program for the same
thanx in advance
KK

Thanx friends for all your efforts
I have a sample code for the same,this will compare the two tables for single row in each table .
what r the modification needed for the multiple rows of values in the two tables??
Please suggest!!
CREATE OR REPLACE PROCEDURE test_compare
IS
TYPE t_col
IS
TABLE OF VARCHAR2 (30)
INDEX BY PLS_INTEGER;
l_col t_col;
j NUMBER := 0;
l_sql VARCHAR2 (2000);
col1 VARCHAR2 (30);
col2 VARCHAR2 (30);
val1 NUMBER;
val2 NUMBER;
status VARCHAR2 (30);
CURSOR c1
IS
SELECT column_id, column_name
FROM all_tab_columns
WHERE table_name = 'TEST1';
BEGIN
FOR i IN c1
LOOP
j := j + 1;
l_col (j) := i.column_name;
END LOOP;
FOR k IN 1 .. j
LOOP
l_sql :=
'SELECT '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST2.'
|| l_col (k)
|| ', '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST1.'
|| l_col (k )
|| ', '
|| 'DECODE(TEST2.'
|| l_col (k)
|| ' -TEST1.'
|| l_col (k)
|| ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
IF status = 'CHANGED'
THEN
DBMS_OUTPUT.put_line( 'TEST2.'
|| col1
|| '='
|| val1
|| ', TEST1.'
|| col2
|| '='
|| val2
|| ', '
|| status);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
END;
/

Similar Messages

  • Extract Time from date and Time and Need XLMOD Funtion to find the Difference between Two Time.

    X6 = "1/5/15 5:16 AM" & NOW ....................difference by Only Time
    not date
    X6 date and Time will be changing, Its not Constant
                Dim myDateTime As DateTime = X6
                Dim myDate As String = myDateTime.ToString("dd/MM/yy")
                Dim myTime As String = myDateTime.ToString("hh:mm tt")
                Dim myDateTime1 As DateTime = Now
                Dim myDate1 As String = myDateTime1.ToString("dd/MM/yy")
                Dim myTime1 As String = myDateTime1.ToString("hh:mm tt")
    Need to use this function to find the Difference between Two Time. due to 12:00 AM isuue
    Function XLMod(a, b)
        ' This replicates the Excel MOD function
        XLMod = a - b * Int(a / b)
    End Function
    Output Required
     dim dd  = XLMod(myTime - myTime1)
    Problem is myTime & myTime1 is String Need to convert them into Time, Later use XLMOD Funtion.

    Induhar,
    As an addendum to this, I thought I'd add this in also: If you have two valid DateTime objects you might consider using a class which I put together a few years ago
    shown on a page of my website here.
    To use it, just instantiate with two DateTime objects (order doesn't matter, it'll figure it out) and you'll then have access to the public properties. For this example, I'm just showing the .ToString method:
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    Dim date1 As DateTime = Now
    Dim date2 As DateTime = #1/1/1970 2:35:00 PM#
    Dim howOld As New Age(date1, date2)
    MessageBox.Show(howOld.ToString, "Age")
    Stop
    End Sub
    End Class
    I hope that helps, if not now then maybe at some point in the future. :)
    Still lost in code, just at a little higher level.
      Thanx frank, can use this in Future....

  • Find the difference between two columns in an ssrs matrix ? MSCRM

    Hi All,
    I am working in reporting part of our project (On-line MSCRM 2013) & in reporting services.
    I am trying to create report using fetch xml based. Below is the snap what we required the result.
    Kindly help me, how to get the difference in both column. (Its a matrix table where year is grouped).
    We need difference between both year Like (Plan Revenue of 2013 & Plan Revenue of 2014 difference in Plan Revenue Diff section) and same for Actual
    Revenue.
    https://social.microsoft.com/Forums/en-US/054d5ca4-0d38-4dc6-84a8-88866cc228fe/find-the-difference-between-two-columns-in-an-ssrs-matrix-mscrm?forum=crmdevelopment
    Thanks,
    Mohammad Sharique

    Hi Bro,
    I used parametrized option for year and done the report,Currently we are getting values in Difference column now i want to show
    that value in percentage. How can we show the percentage based on that value. Means i want to show the Difference in Percentage. 
    Kindly help me i tried but getting some issue. Below i am mentioning the code and snap with result.
    Below expression using to showing Plan Revenue in Percentage for year.
    =
    Sum(IIF(Fields!new_year.Value =Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))
    - Sum(IIF(Fields!new_year.Value =Parameters!EndYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))
    /IIF(Sum(IIF(Fields!new_year.Value = Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0)))>0,
    (Sum(IIF(Fields!new_year.Value = Parameters!StartYear.Value,cdec(Fields!new_planrevenueValue.Value/1000), cdec(0))))
    ,1)
    )*100))
    Result issue is as below in snap with highlighted in red colour.
    Kindly help me on this issue also :)

  • Function Module to find the Difference between two times.

    Hi All,
    Wud you plz let me know the Function Module to find the Difference between two times.
    Input Time1( Hours:Minutes) Time2 ( Hours:Minutes)
    Need Output in Hours:Minutes only . ( No seconds Needed )
    Ex :
    Input :
           06:00 to 18:00 Output : 12:00
    and  20:00 to 06:00 Output: 10:00 with +ve sign only. No -ve sign.
    Thanks,
    N.L.Narayana

    check this .
    data : p_timel like sy-uzeit,
           p_timeh like sy-uzeit,
           diff like sy-uzeit,
           di(8) type c .
           p_timel = '200000'.
           p_timeh = '060000'.
           diff = p_timeh - p_timel.
           concatenate diff+0(2) ':' diff+2(2) into di.
           write:/ di.
    also check for this.
           p_timel = '060000'.
           p_timeh = '180000'.
    see if this can be implemented in ur code .
    or else  u can try with  Fm L_TO_TIME_DIFF passing startdate enddate starttime endtime with UOM as MIN
    hope this helps regards,
    vijay

  • Find the difference between two internal table

    how can i see the difference between two interal tables?
    The requirement is as follows
    1. We have a transparent table, which stores the employee data with EMP ID as key.
    2. We load the transp table data into a interal table (B).
    3. We get data from legecy system as file and it gets loaded into another internal table (A) (this also has the same EMP ID key and this will have latest addition/update to those emplyees).
    Now we need to seperate out these data into three interal table Inserted (I), Deleted (D) and Updated (U).
    We want to do followign things
    I = A - B
    D = B - A
    Both A and B will have around 40k records. Hence we are trying to avoid the looping.
    Please suggest the best option for us.
    Thank you in advance.
    Raghavendra

    >
    RAGHAV URAL wrote:
    > how can i see the difference between two interal tables?
    > The requirement is as follows
    >
    > 1. We have a transparent table, which stores the employee data with EMP ID as key.
    > 2. We load the transp table data into a interal table (B).
    > 3. We get data from legecy system as file and it gets loaded into another internal table (A) (this also has the same EMP ID key and this will have latest addition/update to those emplyees).
    >
    > Now we need to seperate out these data into three interal table Inserted (I), Deleted (D) and Updated (U).
    >
    > We want to do followign things
    > I = A - B
    > D = B - A
    >
    > Both A and B will have around 40k records. Hence we are trying to avoid the looping.
    >
    > Please suggest the best option for us.
    >
    > Thank you in advance.
    > Raghavendra
    Hi Raghavendra,
      Currently as of my knowledge, these operations are only possible through LOOPs. But LOOPign can be really fast here if you properly utilize the SORTING, READ with BINARY SEARCH and FIELD-SYMBOLS usage. I would say:-
    Steps for Insert:-
    SORT: A, B.
    LOOP AT A ASSIGNING <WA_A>.
      READ TABLE B WITH TABLE KEY key = <WA_A>-key BINARY SEARCH.
      IF SY-SUBRC NE 0.
        APPEND <WA_A> TO I.
      ENDIF.
    ENDLOOP.
    Steps for Delete:-
    SORT: A, B.
    LOOP AT B ASSIGNING <WA_B>.
      READ TABLE A WITH TABLE KEY key = <WA_B>-key BINARY SEARCH.
      IF SY-SUBRC NE 0.
        APPEND <WA_B> TO D.
      ENDIF.
    ENDLOOP.
    Regards,
    Ravi.

  • Need to get the difference between two period

    Hi All,
    I need to get the difference of two posting period like period is in format YYYYMM.
    I need to subtract posting perid '200512' FROM '200601' how will be it possible.
    Please suggest.
    Thanks,
    Sandy

    Hi,
    YOu can also do like the following.
    Determine you first day of first period and last day of second period.
    data: first_day type datum,
          last_day type datum,
          days type i.
    first_day = date from FM RKE_GET_FIRST_DAY_IN_PERIOD  
    last_day = date from FM RKE_GET_LAST_DAY_IN_PERIOD   
    days = last_day-first_day.
    Thanks,
    Ramakrishna

  • How to find structural differences between two tables?

    I want to find the difference in structure between objects in two 11g databases:
    Here I want to identify:
    tables/ Views with same columns, same datatype, precision and constraints
    Identify if the same indexes and triggers are present.
    I do not want to compare the data.
    can someone give suggestions on this please?

    my first resource for SQL Developer questions is (That) Jeff Smith: http://www.thatjeffsmith.com/archive/2012/09/sql-developer-database-diff-compare-objects-from-multiple-schemas/.

  • Help I need to subtract times to find the difference between two times?

    Hello there, I really need help.
    I'm trying to make this attendance tracking system so that when a user enters his or her ID the system wll remember his/her check in time. However I also need to find out either how late the person is or how early to store their over time and late.
    So I get the time by using a timestamp, so I'm left with a timestamp that I store into the db. But the problem comes when trying to compute for the late or over time. To do that I have to get the normal login/checkin time from the employee schedules table in the db and either subtract that time from the checkInTime to compute for how late or subtract the checkIn time from it to find early.
    The thing is, the schedule is stored in datetime format in the db since the db is sql. Also I have no idea on how to subtract the two dates via java. The month, date and year stored in the sql db are just fillers, all I need computed is the time difference of the hours and minutes between the checkInTime and the schedule.
    I've tried to use datediff: SELECT e.employeeNumber, datediff('hh', startTime, checkInTime) as lateTime from employeeschedules e, timandattendance, contractualemployees c where c.scheduleNumber=e.scheduleNumber and t.employeeNumber=c.employeeNumber and t.checkInNumber=1;
    But it keeps on giving out various errors: You have an error in your sql syntax check your manual that corresponds to your sql server version. for the right syntax to use near , checkInTime) as lateTime from employeeschedules e, timandattendance, contractualemployees c where c.scheduleNumber=e.scheduleNumber and t.employeeNumber=c.employeeNumber and t.checkInNumber=1;
    My sql server is My SQL 5.0.5
    Now I'm thinking of useing Gregorian instead, but I don't know how to use Gregorian and dont know the various methods. Can someone tell me what to do, provide some sample codes and references that I can use to accomplish what I need to do?

    NewtonsApple_2 wrote:
    Now I'm thinking of useing Gregorian instead, but I don't know how to use Gregorian and dont know the various methods. Can someone tell me what to do, provide some sample codes and references that I can use to accomplish what I need to do?it may help...
    GregorianCalendar

  • How to do find the difference between two time stamps

    Hi all,
    i have a table with 2 columns called GMT time and Local time . so i need to find difference between these two time stamps.
    i tried like this
    select to_date(GMT_TIME,'yyyy-mm-dd hh24:hi:ss')-to_date(LOCAL_TIME,'yyyy-mm-dd hh24:hi:ss') from date_table
    the result is coming as follows ..
    0.291666666667
    i did not under stand the out put.
    In my table GMT_TIME= 2011-06-26 00:00:00 and LOCAL_TIME=2011-06-25 17:00:00 ..
    please help me how to get exact hours between two dates ..
    Thanks
    Sreedhar

    Hi Sreedhar,
    Your output (0.2916666) has the unit day.
    You should multiply with 24 to get the unit hour.
    select ( to_date(GMT_TIME,'yyyy-mm-dd hh24:hi:ss')-to_date(LOCAL_TIME,'yyyy-mm-dd hh24:hi:ss') ) * 24 from date_table;Rgds,
    Tycho

  • Find the difference between two dates for the specific month and year

    Hi,
    I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
    for that date. In this case I input Jan 2013. How can I sql this ?

    I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
    First let me understand you:
    You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
    declare @for_month int = 1 --January
    declare @for_year int = 2013
    declare @StartDate date = '2012-12-20'
    declare @EndDate date = '2013-01-04'
    SELECT
    CASE
    WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
    DATEDIFF(DAY, @StartDate,@EndDate)
    WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
    WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
    WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
    DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
    ELSE
    0
    END as [DD]
    I don't know how you calculate day range between 01/01/2013 and 04/01/2013
    is 4, it is actually is 3 but if that is the case, you can add 1 from the condition.

  • How to find the difference between two date?

    Hi,
    I currently writing a date comparision program. Below is the idea analogy,
    Currently i need to find how many day differences between 30 July 2003 and 22 June 2004. How can i use java to code it?
    Thanks.

    there doesn't seem to be a direct way but try this:int daysBetween = 0;
    Calendar c = new GregorianCalendar(2004, Calendar.JULY, 30);
    Calendar d = new GregorianCalendar(2003, Calendar.JUNE, 22);
    while (c.get(Calendar.YEAR) != d.get(Calendar.YEAR)) {
        daysBetween += 360;
        d.add(Calendar.DAY_OF_YEAR, 360);
    daysBetween += c.get(Calendar.DAY_OF_YEAR) - d.get(Calendar.DAY_OF_YEAR);This gives the correct result of 404 (= 8 days from June 22 to June 30 + 366 days between July 1 of 2003 and 2004 + 30 days from July 1 to July 30)

  • How to find the difference between two timestamp column

    Dear All,
    please Solve my issue,
    I have Table name Record which has the following columns,
    Empid in number column, dat in timestamp
    which has the following values
    Expand|Select|Wrap|Line Numbers
    empid dat
    ====== ====
    101 4/9/2012 9:48:54 AM
    101 4/9/2012 9:36:28 AM
    101 4/9/2012 6:16:28 PM
    101 4/10/2012 9:33:48 AM
    101 4/10/2012 12:36:28 PM
    101 4/10/2012 8:36:12 PM
    101 4/11/2012 9:36:28 AM
    101 4/11/2012 4:36:22 PM
    Here I need to display the following columns,
    empid,min(dat) as start,max(dat) as end and difference(max(dat)-min(dat) for each day,
    for eg,
    Empid Strart end difference
    101 4/9/2012 9:48:54 AM 4/9/2012 6:16:28 PM 8.28
    like this.
    Here 3 different days are exists so It should return 3 records with the above mentioned columns,
    Please Help me to get this.
    Thank you,
    Regards,
    Gurujothi
    Edited by: Gurujothi on Apr 25, 2012 4:45 AM

    >
    101 4/9/2012 9:48:54 AM 4/9/2012 6:16:28 PM 8.28
    >
    why 4/9/2012 9:48:54?
    why not 4/9/2012 9:36:28 AM ?
    SQL>
    SQL> with t as
      2  (select  101 empid, to_timestamp('4/9/2012 9:48:54 AM', 'dd/mm/yyyy hh:mi:ss AM') dat from dual union all
      3  select 101, to_timestamp('4/9/2012 9:36:28 AM', 'dd/mm/yyyy hh:mi:ss AM') dat from dual union all
      4  select 101, to_timestamp('4/9/2012 6:16:28 PM', 'dd/mm/yyyy hh:mi:ss PM') dat from dual union all
      5  select 101, to_timestamp('4/10/2012 9:33:48 AM', 'dd/mm/yyyy hh:mi:ss AM') dat from dual union all
      6  select 101, to_timestamp('4/10/2012 12:36:28 PM', 'dd/mm/yyyy hh:mi:ss PM') dat from dual union all
      7  select 101, to_timestamp('4/10/2012 8:36:12 PM', 'dd/mm/yyyy hh:mi:ss PM') dat from dual union all
      8  select 101, to_timestamp('4/11/2012 9:36:28 AM', 'dd/mm/yyyy hh:mi:ss AM') dat from dual union all
      9  select 101, to_timestamp('4/11/2012 4:36:22 PM', 'dd/mm/yyyy hh:mi:ss PM') dat from dual
    10  )
    11  select empid, min(dat) as start_dat
    12  , max(dat) as end_dat
    13  , max(dat)-min(dat) diff
    14  from t
    15  group by empid, trunc(dat)
    16  /
         EMPID START_DAT                                         END_DAT                                           DIFF
           101 04.09.12 09:36:28,000000000                       04.09.12 18:16:28,000000000                       +000000000 08:40:00
           101 04.11.12 09:36:28,000000000                       04.11.12 16:36:22,000000000                       +000000000 06:59:54
           101 04.10.12 09:33:48,000000000                       04.10.12 20:36:12,000000000                       +000000000 11:02:24
    SQL>

  • Trying to find the difference between two sub-totals (sales - credits)

    Hi Everyone,
    I have the following code which essentially lists the total sales for an items on the first row, and the total credits for the same item on the second row.
    SELECT T0.ItemCode, SUM(T0.LineTotal) as 'Total Sales'
    FROM INV1 T0
    WHERE T0.ItemCode = 'ACR2401010'
    GROUP BY T0.ItemCode
    UNION ALL
    SELECT T1.ItemCode, SUM(T1.LineTotal) as 'Total Sales'
    FROM RIN1 T1
    WHERE T1.ItemCode = 'ACR2401010'
    GROUP BY T1.ItemCode
    The results of the query are shown below (with some alterations for confidentiality).
    What I would like to do is write a code block that subtracts the total credits from the total sales, leaving me with only one row of data for the ItemCode.
    If anybody can help with writing the code to achieve this it will be greatly appreciated.
    Kind Regards,
    Davo

    Hi, Please take a look and tweak accordingly. You may pay attention to nulls and manipulate accordingly. Best of luck!
    --Option 1
    SELECT t2.ItemCode,( SUM(T2.TotalSales)-SUM(TotalCredits)) AS 'Total'
    FROM
    SELECT T0.ItemCode, SUM(T0.LineTotal) as 'TotalSales',SUM(0) as 'TotalCredits'
    FROM INV1 as T0
    WHERE T0.ItemCode = 'ACR2401010'
    GROUP BY T0.ItemCode
    UNION ALL
    SELECT T1.ItemCode, SUM(0) as 'TotalSales', SUM(T1.LineTotal) as 'TotalCredits',
    FROM RIN1 as T1
    WHERE T1.ItemCode = 'ACR2401010'
    GROUP BY T1.ItemCode
    ) AS t2
    GROUP BY t2.ItemCode
    --Option 2
    SELECT t2.ItemCode, ( SUM(T2.TotalSales)-SUM(TotalCredits)) AS 'Total'
    FROM
    SELECT T0.ItemCode, SUM(T0.LineTotal) as 'TotalSales',SUM(0) as 'TotalCredits'
    FROM INV1 as T0
    --WHERE T0.ItemCode = 'ACR2401010'
    GROUP BY T0.ItemCode
    UNION ALL
    SELECT T1.ItemCode, SUM(0) as 'TotalSales', SUM(T1.LineTotal) as 'TotalCredits',
    FROM RIN1 as T1
    --WHERE T1.ItemCode = 'ACR2401010'
    GROUP BY T1.ItemCode
    ) AS t2
    WHERE t2.ItemCode = 'ACR2401010'
    GROUP BY t2.ItemCode
    --Option 3
    SELECT t2.ItemCode, ( SUM(T2.TotalSales)-SUM(TotalCredits) ) AS 'Total'
    FROM
    SELECT T0.ItemCode, T0.LineTotal as 'TotalSales', 0 as 'TotalCredits'
    FROM INV1 as T0
    UNION ALL
    SELECT T1.ItemCode, 0 as 'TotalSales', T1.LineTotal as 'TotalCredits',
    FROM RIN1 as T1
    ) AS t2
    WHERE t2.ItemCode = 'ACR2401010'
    GROUP BY t2.ItemCode
    --Assuming credit part is optional..also assuming each table should return only one row else results would inflate...option 4
    SELECT t0.ItemCode, ( SUM(T0.TotalSales)-SUM(T1.TotalSales)) AS 'Total'
    FROM
    INV1 as t0
    left outer join
    RIN1 as t1
    ON t0.ItemCode = t1.ItemCode
    WHERE t0.ItemCode = 'ACR2401010'
    GROUP BY t0.ItemCode
    --option 4 with grouping to ensure single row from each table
    SELECT t0.ItemCode, ( isnull(T0.TotalSales,0)-isnull(T1.TotalSales,0)) AS 'Total'
    FROM
    SELECT T0.ItemCode, SUM(T0.LineTotal) as 'TotalSales'
    FROM INV1 as T0
    --WHERE T0.ItemCode = 'ACR2401010'
    GROUP BY T0.ItemCode
    )as t0
    LEFT OUTER JOIN
    ( SELECT T1.ItemCode, SUM(T1.LineTotal) as 'TotalCredits',
    FROM RIN1 as T1
    --WHERE T1.ItemCode = 'ACR2401010'
    GROUP BY T1.ItemCode
    ) as t1
    ON t0.ItemCode = t1.ItemCode
    WHERE t0.ItemCode = 'ACR2401010'
    --Option 5 with grouping to ensure single row from each table.
    --Also assuming that sales or credits can be optional
    SELECT
    ISNULL(t0.ItemCode,t0.ItemCode) AS ItemCode,
    ( isnull(T0.TotalSales,0)-isnull(T1.TotalSales,0)) AS 'Total'
    FROM
    SELECT T0.ItemCode, SUM(T0.LineTotal) as 'TotalSales'
    FROM INV1 as T0
    --WHERE T0.ItemCode = 'ACR2401010'
    GROUP BY T0.ItemCode
    )as t0
    FULL OUTER JOIN
    ( SELECT T1.ItemCode, SUM(T1.LineTotal) as 'TotalCredits',
    FROM RIN1 as T1
    --WHERE T1.ItemCode = 'ACR2401010'
    GROUP BY T1.ItemCode
    ) as t1
    ON t0.ItemCode = t1.ItemCode
    WHERE (t0.ItemCode = 'ACR2401010' OR t1.ItemCode = 'ACR2401010')

  • Find the difference in Two Tabels in two different schema?

    Hello Gurus,
    In DB which have millions of records -
    I have a Tabel A under Schema A and i have another Tabel B under Schema B and i have to pick the refrence from Tabel C under Schedma B altough my DB is same. Both of the tables (Table A and Table B) have exatly the same structure in both the schema and when i tried finding the difference like -
    Find the refrence from Table C and pick the values which are in Tabel A under Schema A but which are not present in the Tabel B under Schema B .
    I have to compare each and ever field value which is present under all of the columns under Table A to ever field value which is present under all of the columns under Table B.
    then wrote the following query-
    SELECT * FROM
    SchemaA.Table A t1,SchemaB.Table C t2
    WHERE t1.SV_ID = t2.SV_ID order by t1.SV_ID
    Minus
    Select * from SchemaB.TableB order by SV_ID
    Then this query is not working. And display the following error -
    "query block has incorrect number of result columns"
    Even both of the tables have the same column's and same structure.
    Kindly help me .
    Looking forward for some guidance -
    Thanks in advance.

    1-There are some 70 Columns in both of the tables so thats why i don't want to use the query based on columns as it will involve writing all of the columns.
    2- By using the query something like -
    select <column_name(s)> from table1
    minus
    select <column_name(s)> from table2, table1
    where <condition>
    It is throwing the error -ORA-01789: query block has incorrect number of result columns
    3- Using the query
    SELECT t1.*
    FROM SchemaA.TableA t1,SchemaB.TableC t2
    WHERE t1.SV_ID = t2.SV_ID
    MINUS
    SELECT t3.*
    FROM SchemaB.TableB t3
    order by SV_ID
    It gives me the reuslt but when i corss check them individually in both of the tables then there is no difference.
    4- SELECT t1.* FROM
    SchemaA.Table A t1,SchemaB.Table C t2
    WHERE t1.SV_ID = t2.SV_ID order by t1.SV_ID
    Minus
    Select * from SchemaB.TableB order by SV_ID
    Query is not working.
    Kindly help me to find the difference between two tables , column by column based on the condition both of the tables have the same SV_ID.

  • Need a sql query to get the difference between two timestamp in the format of hh:mm:ss.msec

    I have a database table where it keeps record of the transaction when it starts at StartTime and when it ends at EndTime. Both these entries are having the timestamp entries. Say for example, I have a tuple with Entries like 'Transaction A' starts at '2014-05-07
    20:55:03.170' and ends at '2014-05-08 08:56:03.170'. I need to find the difference between these two timestamps and my expected output is 12:01:00.000. Let me know how to achieve this ? 

    Hi,
    You can use below script which calculates difference as DD:HH:MM:SS. You can modify the same:
    DECLARE @startTime DATETIME
    DECLARE @endTime DATETIME
    SET @startTime = '2013-11-05 12:20:35'
    SET @endTime = '2013-11-10 01:22:30'
    SELECT [DD:HH:MM:SS] =
    CAST((DATEDIFF(HOUR, @startTime, @endTime) / 24) AS VARCHAR)
    + ':' +
    CAST((DATEDIFF(HOUR, @startTime, @endTime) % 24) AS VARCHAR)
    + ':' +
    CASE WHEN DATEPART(SECOND, @endTime) >= DATEPART(SECOND, @startTime)
    THEN CAST((DATEDIFF(MINUTE, @startTime, @endTime) % 60) AS VARCHAR)
    ELSE
    CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, @endTime), @endTime) % 60)
    AS VARCHAR)
    END
    + ':' + CAST((DATEDIFF(SECOND, @startTime, @endTime) % 60) AS VARCHAR),
    [StringFormat] =
    CAST((DATEDIFF(HOUR , @startTime, @endTime) / 24) AS VARCHAR) +
    ' Days ' +
    CAST((DATEDIFF(HOUR , @startTime, @endTime) % 24) AS VARCHAR) +
    ' Hours ' +
    CASE WHEN DATEPART(SECOND, @endTime) >= DATEPART(SECOND, @startTime)
    THEN CAST((DATEDIFF(MINUTE, @startTime, @endTime) % 60) AS VARCHAR)
    ELSE
    CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, @endTime), @endTime) % 60)
    AS VARCHAR)
    END +
    ' Minutes ' +
    CAST((DATEDIFF(SECOND, @startTime, @endTime) % 60) AS VARCHAR) +
    ' Seconds '
    Reference:
    http://sqlandme.com/2013/12/23/sql-server-calculating-elapsed-time-from-datetime/
    - Vishal
    SqlAndMe.com

Maybe you are looking for

  • JAVA Float to int exception

    Hi I am facing a serious problem after migrating application from oracle 9i to oracle 10g. Application was earlier connected with oracle 9i as database after migration to 10g . I am facing a problem of float to int exception. In database,in table whe

  • Getting an error package ..... does not exist

    Hi, I am trying to learn Java through Sams Teach Yourself Java 6. In the book it asked me to create a directory called c:\dev\java\org\cadenhead\ecommerce I have added the c:\dev\java into the CLASSPATH variable in Vista The program I am trying to co

  • Install windows 8.1 enterprise trial on new machine

    I want to install windows 8.1 enterprise trial iso from microsoft site. Now I want to install it on new machine. My new machine has ubuntu install and I want to clean install Windows 8.1. How I create bootable disc from ISO ?

  • Edit Column Width in Expanded View

    I'm not too thrilled about the new iTunes, mostly because of the loss of easy navigation. My library is full mostly of classical music, and the track names are often very long [eg. "Couperin: Pièces de Clavecin, Onziême Ordre - Les Jongleurs, Sauteur

  • How to select all the rows of table control in BDC

    Hi All, While I am doing BDC for Transaction Code MC88, After the first screen I will get another screen with tabble control, Here I need to select all the rows of table control, I am assignng the 'X' to the first field of Table Control which is the