Summ up the amount of two tables with their +/- sign
Hi All
I am stuck up in logic,
Scenerio is .
I have to fetch amount(BSID-WRBTR) field from BSID as well as from BSAD(BSAD-WRBTR) on the input of ( company customer , year period & countyr) it can be with positive or negative value as functional says and it would be recognised on the basis of their debit and credit indicator(BSID-SHKZG), now
My question is If i have to sum the amount of both tables.
first i have to associate the plus or minus sign with them on the basis of their debit and credit indicator (BSID-SHKZG). now how to do it.
please let me know any sample program or logic to sum the amount on the basis of their +/- sign.
points will be Rewarded.
Hi....
Here you hav to go for COLLECT statement in the loop....
loop at it_final1.
read table it_final with key bukrs = it_final1-bukrs
hkont = it_final1-racct.
if sy-subrc = 0.
it_final1-CrAmt = it_final-CrAmt - it_final1-CrAmt .
it_final1-DeAmt = it_final1-DeAmt + it_final-DeAmt .
append it_final1 to it_final2.
else .
append it_final1 to it_final2.
clear it_final1.
endif.
endloop.
loop at it_final .
read TABLE it_final1 with key bukrs = it_final-bukrs
racct = it_final-hkont.
if sy-subrc <> 0.
it_final2-bukrs = it_final-bukrs.
it_final2-racct = it_final-hkont.
it_final2-DeAmt = it_final-DeAmt.
it_final2-CrAmt = it_final-CrAmt.
append it_final2.
clear:it_final2.
endif.
endloop.
write:/ a, (9)it_final2-bukrs ,
a, (15)it_final2-racct,
a, (15)it_final2-deAmt,
a, (15)it_final2-CrAmt,
a.
endloop.
write:/(67) sy-uline.
ENDFORM. " display_output
*& Form sub_get_glt0
FORM sub_get_glt0 .
select bukrs ryear racct drcrk tslvt hslvt
from glt0 into table t_glt0
where bukrs = 'code'
and ryear = '2007'.
ENDFORM. " sub_get_glt0
*& Form it_final1
FORM it_final1 .
loop at t_glt0.
if t_glt0-drcrk = 'S'.
move t_glt0-tslvt to it_final1-DeAmt.
else.
move t_glt0-tslvt to it_final1-crAmt.
endif.
move t_glt0-bukrs to it_final1-bukrs.
move t_glt0-racct to it_final1-racct.
collect it_final1.
clear:it_final1.
clear:t_glt0.
ENDLOOP.
ENDFORM. " it_final1
Thanks,
Naveen.I
Edited by: Naveen Inuganti on Jun 18, 2008 10:49 AM
Similar Messages
-
How to select all the colomns_names from a table, with their datatypes ..
hi :)
i would like to know, how to select in SQL all the columns names from a table with their datatypes so that i get something like this :
Table 1 : table_name
the column ID has the Datatype NUMBER
the column name has the Datatype Varchar2
Table 2 : table_name
the column check has the Datatype NUMBER
the column air has the Datatype Varchar2
and that has to be for all the tables that i own ! ..
P. S : i m trying to do this with java, so it s would be enough if you just tell me how to select all the tables_names with all their colums_names and with all their datatypes ! ..
thank you :)
i ve heard it can be done with USER_TABLES .. but i have no idea how :( ..
Edited by: user8865125 on 17.05.2011 12:22Hi,
The data dictionary view USER_TAB_COLUMNS has one row for every column in every table in your schema. The columns TABLE_NAME, COLUMN_NAME and DATA_TYPE have all the information you need.
Another data dictionary view, USER_TABLES, may be useful, too. It has one row pre table. -
To Select the data from two table one is transp table and onther is cluster
Hi All,
I want to select the data from two tables
Here i am giving with an example.
Fileds: kunnr belnr from bseg. table bseg
fields: adrnr from kna1 table: kna1.
Know i want to put these into one internal table based on kunnr and belnr.
Thanks in advance.
RameshHi,
U cant use joins on cluster table and BSEG is a cluster table so use FOR ALL ENTRIES for taht
refer this code
*& Form sub_read_bsak
text
--> p1 text
<-- p2 text
FORM sub_read_bsak.
*--Select data from BSAK Table
SELECT lifnr
augdt
augbl
gjahr
belnr
xblnr
blart
dmbtr
mwskz
mwsts
sgtxt
FROM bsak
INTO CORRESPONDING FIELDS OF TABLE it_bsak
WHERE belnr IN s_belnr
AND augdt IN s_augdt.
IF sy-subrc EQ 0.
*--Sort table by accounting document and vendor number
SORT it_bsak BY belnr lifnr.
ENDIF.
ENDFORM. " sub_read_bsak
*& Form sub_read_bseg
text
--> p1 text
<-- p2 text
FORM sub_read_bseg.
IF NOT it_bsak[] IS INITIAL.
*--Select data from BSEG table
SELECT belnr
gjahr
shkzg
kostl
hkont
ebeln
ebelp
FROM bseg
INTO CORRESPONDING FIELDS OF TABLE it_bseg
FOR ALL ENTRIES IN it_bsak
WHERE belnr EQ it_bsak-belnr
AND gjahr EQ it_bsak-gjahr
AND shkzg EQ 'S'.
IF sy-subrc EQ 0.
*--Sort table by accounting document
SORT it_bseg BY belnr.
ENDIF.
ENDIF.
ENDFORM. " sub_read_bseg -
MATERIALIZED view on two tables with Fast Refresh
i Wanted to create MV on two tables with Fast refresh on commit.
I followed below steps
create materialized view log on t1 WITH PRIMARY KEY, rowid;
create materialized view log on t2 WITH PRIMARY KEY, rowid;
CREATE MATERIALIZED VIEW ETL_ENTITY_DIVISION_ASSO_MV
REFRESH fast ON commit
ENABLE QUERY REWRITE
AS
select A.ROWID B.ROWID,a.c1, DECODE(a.c1,'aaa','xxx','aaa') c2
from t1 A
join t2 b
on AB.c1= CD.c2;
i am getting below error.
Error report:
SQL Error: ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
12054. 00000 - "cannot set the ON COMMIT refresh attribute for the materialized view"
*Cause: The materialized view did not satisfy conditions for refresh at
commit time.
*Action: Specify only valid options.
Basically i want to take record in MV by joinig two tables and if both of the base tables will updated then record should reflect in materialised view.
Please do the needfull.does the table support PCT? the other restrictions on joins look to be ok in your statement.
maybe try creating first with on demand instead of commit to see does it create.
http://docs.oracle.com/cd/B19306_01/server.102/b14223/basicmv.htm
>
Materialized Views Containing Only Joins
Some materialized views contain only joins and no aggregates, such as in Example 8-4, where a materialized view is created that joins the sales table to the times and customers tables. The advantage of creating this type of materialized view is that expensive joins will be precalculated.
Fast refresh for a materialized view containing only joins is possible after any type of DML to the base tables (direct-path or conventional INSERT, UPDATE, or DELETE).
A materialized view containing only joins can be defined to be refreshed ON COMMIT or ON DEMAND. If it is ON COMMIT, the refresh is performed at commit time of the transaction that does DML on the materialized view's detail table.
If you specify REFRESH FAST, Oracle performs further verification of the query definition to ensure that fast refresh can be performed if any of the detail tables change. These additional checks are:
A materialized view log must be present for each detail table unless the table supports PCT. Also, when a materialized view log is required, the ROWID column must be present in each materialized view log.
The rowids of all the detail tables must appear in the SELECT list of the materialized view query definition.
If some of these restrictions are not met, you can create the materialized view as REFRESH FORCE to take advantage of fast refresh when it is possible. If one of the tables did not meet all of the criteria, but the other tables did, the materialized view would still be fast refreshable with respect to the other tables for which all the criteria are met. -
Cartesian of data from two tables with no matching columns
Hello,
I was wondering – what’s the best way to create a Cartesian of data from two tables with no matching columns in such a way, so that there will be only a single SQL query generated?
I am thinking about something like:
for $COUNTRY in ns0: COUNTRY ()
for $PROD in ns1:PROD()
return <Results>
<COUNTRY> {fn:data($COUNTRY/COUNTRY_NAME)} </COUNTRY>
<PROD> {fn:data($PROD/PROD_NAME)} </PROD>
</Results>
And the expected result is combination of all COUNTRY_NAMEs with all PROD_NAMEs.
What I’ve noticed when checking query plan is that DSP will execute two queries to have the results – one for COUNTRY_NAME and another one for PROD_NAME. Which in general results in not the best performance ;-)
What I’ve noticed also is that when I add something like:
where COUNTRY_NAME != PROD_NAME
everything is ok and there is only one query created (it's red in the Query plan, but still it's ok from my pov). Still it looks to me more like a workaround, not a real best approach. I may be wrong though...
So the question is – what’s the suggested approach for such queries?
Thanks,
Leszek
Edited by xnts at 11/19/2007 10:54 AMWhich in general results in not the best performanceI disagree. Only for two tables with very few rows, would a single sql statement give better performance.
Suppose there are 10,000 rows in each table - the cross-product will result in 100 million rows. Sounds like a bad idea. For this reason, DSP will not push a cross-product to a database. It will get the rows from each table in separate sql statements (retrieving only 20,000 rows) and then produce the cross-product itself.
If you want to execute sql with cross-products, you can create a sql-statement based dataservice. I recommend against doing so. -
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
KKThanx 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;
/ -
How to match columns of two tables with
Hello:
I've two tables like below:
Table1:(Base Table)
Country|Prefix|Prefix_Length
CountryA|001|3
CountryB|0012|4
CountryC|00443|5
CountryD|0091|4
Table2:(Detail Table)
Population|Area|Prefix
500|AreaA|0015921
1000|AreaB|00122
400|AreaC|00443743
300|AreaD|0091333
100|AreaA|001
I need to match these two tables with prefix column (which length is not fixed in both tables: but it starts with 00 in both tables). Two different countries prefix can be similar up to a certain length. So, Prefix_Length can be used to determine (exactly) how long should be taken to search from Table2.
Output:
Country|Prefix|Area|Population
CountryA|001|AreaA|600
CountryB|0012|AreaB|1000
CountryC|00443|AreaC|400
CountryD|0091|AreaD|300
Please help me with your valuable feedback.
-TanvirYou have to explain how CountryA population is calculated:
with base_table as (
select 'CountryA' country,'001' prefix,3 prefix_length from dual union all
select 'CountryB','0012',4 from dual union all
select 'CountryC','00443',5 from dual union all
select 'CountryD','0091',4 from dual
detail_table as (
select 500 population,'AreaA' area,'0015921' prefix from dual union all
select 1000,'AreaB','00122' from dual union all
select 400,'AreaC','00443743' from dual union all
select 300,'AreaD','0091333' from dual union all
select 100,'AreaA','001' from dual
-- end of on-the-fly data sample
select country,
b.prefix,
area,
population
from base_table b,
detail_table d
where b.prefix = substr(d.prefix,1,prefix_length)
COUNTRY PREFI AREA POPULATION
CountryA 001 AreaA 500
CountryA 001 AreaB 1000
CountryA 001 AreaA 100
CountryB 0012 AreaB 1000
CountryC 00443 AreaC 400
CountryD 0091 AreaD 300
6 rows selected.
SQL> SY. -
The amount field in table GLPCT Activity RFBU is reaching max limit of
Hi Experts,
Need your help on below issue.
The amount filed in table GLPCT, company code 123, account 110000, Activity
RFBU and SD00 is increasing every year, and now the sum is close to the
limit of 9,999,999,999,999.99- .
this amount field is continue to increase, if depend on data when
performing year-end closing and balance carry-forward.
So, Each field contains 15 places, which leads to a maximum amount of
999,999,999,999,999 When the value reaches the maximum amount, an
additional update of an amount is not possible anymore.
System cannot update the Balance Carry-forward amount for 2012.Have you communicated with SAP about a possible solution?
-
How to give relationship between two tables with comon column with between oprator
Hi Folks,
I am using Sql Server 2008R2. I am getting a problem to establish relationship between two tables.
I have two Tables, 1.Inventory Details Table another one is Inventory Header Table.
Inventory Details Table having a column Card No and inventory Header Table having columns From card No and To Card No.
I want to give relationship between these two tables with Card no. Could you please provide me the Sql Query.
Your help would be greatly appreciated .
Regards
hasthi.
email:[email protected]Hi Raju,
We have two way that we can relate to the table either join or quality condition use following syntax/Query for relating two tables
select * from Inventory_Details ID inner join Inventory_Header IH on ID.CardNo between IH.FrmCardno and IH.ToCardNo
or
Select * from Inventory_Details ID ,Inventory_Header IH where ID.CardNo=IH.CardNo OrSelect * from Inventory_Details ID ,Inventory_Header IH where ID.CardNo between IH.FrmCardno and IH.ToCardNo
Hope this will help you
Niraj Sevalkar -
How to update two tables with trigger
Hi:
how to update two tables with trigger ?
I have two tables :
(1)ASIA
MI number;
(2)ASIA_P
ID number;
When I insert a new value into the asia.MI ,I also can
insert the same value into the asia_p.id field.
I have write a trigger as follows but it does't work.
create or replace trigger MI_TRG
before insert on asia
for each row
declare
seq number;
begin
select MI_SEQ.Nextval into seq from dual;
:new.MI:=seq;
insert into ASIA_PRO(MI_ID)
values
(seq);
end MI_TRG;
How to realize it ?
thanks
zzmWhy do you say it does not work?
-
Can two people with their own iPhones use the same computer and iTunes?
Can two people with their own iPhones use the same computer and iTunes?
Yes. My wife and I each have our own Apple ID and used the same computer to sync our phones with iTunes. The best way to do this is set up separate user accounts on the computer. If you are logged in to your account on the computer and your wife wants to sync here phone, you would log out and she would log in. These keeps all your stuff separate and avoids a ton of confusion.
-
Insert the data into two tables at a time.
Hi ,
i have these two tables
create table [dbo].[test1](
[test1_id] [int] identity(1,1) primary key,
[test2_id] [int] not null
create table [dbo].[test2](
[test2_id] [int] identity(1,1) primary key,
[test1_id] [int] not null
alter table [dbo].[test1]
add constraint [fk_test1_test2_id] foreign key([test2_id])
references [dbo].[test2] ([test2_id])
alter table [dbo].[test2] add constraint [fk_test2_test2_id] foreign key([test1_id])
references [dbo].[test1] ([test1_id])
I want to insert the data into two tables in one insert statement. How can i do this using T-SQL ?
Thanks in advance.You can INSERT into both tables within one Transaction but not in one statement. By the way, you would need to alter your dbo.Test1 table to allow null for first INSERT test2_id column
See sample code below:
CREATE TABLE #test1(test1_ID INT IDENTITY(1,1),test2_id INT NULL)
CREATE TABLE #test2(test2_ID INT IDENTITY(1,1),test1_ID INT)
DECLARE @Test1dentity INT
DECLARE @Test2dentity INT
BEGIN TRAN
-- Insert NULL as test2_ID value is unknown
INSERT INTO #test1(test2_ID)
SELECT NULL;
-- get inserted identity value
SET @Test1dentity = SCOPE_IDENTITY();
INSERT INTO #test2(test1_ID)
SELECT @Test1dentity;
-- get inserted identity value
SET @Test2dentity = SCOPE_IDENTITY();
-- Update test1 table
UPDATE #test1
SET test2_ID = @Test2dentity
WHERE test1_ID = @Test1dentity;
COMMIT
SELECT * FROM #test1;
SELECT * FROM #test2;
-- Drop temp tables
IF OBJECT_ID('tempdb..#test1') IS NOT NULL
BEGIN
DROP TABLE #test1
END
IF OBJECT_ID('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
web: www.ronnierahman.com -
Fetch the data from two tables
hell all
i want to fetch the data from two tables, one is from internal table and another one is data base table. what syntax i have to use either FOR ALL ENTRIES or INNER JOIN?hi
Use FOR ALL ENTRIES.
see the sample code
select * into table tvbrk from vbrk
where fkart in ('F2', 'F3', 'RE',
'ZVEC' , 'ZVEM' , 'ZVED',
'S1')
and erdat in so_erdat
and kunag in s_kunag.
erdat in so_erdat
and fkart in ('F2', 'F3', 'RE',
'ZVEC' , 'ZVEM').
if not tvbrk is initial.
select * into table t_zregion from zregion
for all entries in tvbrk
where country = tvbrk-land1
and region = s_regio.
endif.
thanks
sitaram -
Query the data between two tables
Need help for query the data between two tables
Table 1: Time sheet
P.ID P.Name EmpID HoursSpend DateTime
c12234 Test 25 4 06/12/2013
c12234 Test 25 7 06/13/2013
c12234 Test 25 8 06/15/2013
c12234 Test 5 3 06/21/2013
c12234 Test 2 5 07/15/2013
c12234 Test 25 4 07/21/2013
Table 2: cost table
EmpID FromDate ToDate Rate
25 05/01/2013 06/30/2013 250
2 04/01/2013 05/31/2013 150
25 07/01/2013 09/30/2013 300
Output
P.ID P.Name EmpID HoursSpend DateTime Rate Total (HoursSond x Rate)
c12234 Test 25 4 06/12/2013 250 1000 (4*250)
c12234 Test 25 7 06/13/2013 250 1750
c12234 Test 25 8 06/15/2013 250
2000
c12234 Test 25 4 07/21/2013 300
1200
c12234 Test 2 5 07/15/2013 150
750
===========================================
Total 28
6700
============================================
Here EmpID =2 don't have rate in the cost table on july month should be pick from last entry from cost table.Hi Gopal,
According to your description, it seems that the output needn’t include the row when EmpID=2. Because the DateTime for it in Table1 doesn’t included between FromDate column and ToDate column. After testing the issue in my environment, we can refer to the
query like below to achieve your requirement:
SELECT time.*,cost.EmpID,cost.Rate,(time.HoursSpend * cost.Rate)as [Total (HoursSond x Rate)]
FROM [Time sheet] as time
INNER JOIN
[cost table]as cost
ON time.EmpID = cost.EmpID
AND time.DateTime BETWEEN cost.FromDate AND cost.ToDate
If there are any other questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
How to compare two rows from two table with different data
how to compare two rows from two table with different data
e.g.
Table 1
ID DESC
1 aaa
2 bbb
3 ccc
Table 2
ID DESC
1 aaa
2 xxx
3 ccc
Result
2Create
table tab1(ID
int ,DE char(10))
Create
table tab2(ID
int ,DE char(10))
Insert
into tab1 Values
(1,'aaa')
Insert
into tab1 Values
(2,'bbb')
Insert
into tab1 Values(3,'ccc')
Insert
into tab1 Values(4,'dfe')
Insert
into tab2 Values
(1,'aaa')
Insert
into tab2 Values
(2,'xx')
Insert
into tab2 Values(3,'ccc')
Insert
into tab2 Values(6,'wdr')
SELECT
tab1.ID,tab2.ID
As T2 from tab1
FULL
join tab2 on tab1.ID
= tab2.ID
WHERE
BINARY_CHECKSUM(tab1.ID,tab1.DE)
<> BINARY_CHECKSUM(tab2.ID,tab2.DE)
OR tab1.ID
IS NULL
OR
tab2.ID IS
NULL
ID column considered as a primary Key
Apart from different record,Above query populate missing record in both tables.
Result Set
ID ID
2 2
4 NULL
NULL 6
ganeshk
Maybe you are looking for
-
Hello, I'm seeing an issue that has been mentioned a couple of times in this forum, but hasn't been resolved -- at least no final resolution is posted. The behavior is that the closing body tag is missing from rendered pages which use the ui:body tag
-
What does it mean when my serial number shows up as Serial Number SystemSeumb? I've never seen that before.
-
Hello all! :) I'm having a bit of trouble with some results of a selection. My current selection includes the following fields: Year | Month | Current Month Measure | Previous Month Measure | Previous Year Measure The Previous Month and Previous Year
-
Hi, Could anyone please let me know how to find the infotype records in SRM. I am not sure whether SRM would have infotypes as in R/3 but I know that there is an HR Org structure maintained in SRM. Regards, Binay.
-
Here's the "non-issue" first. We shot video with green screen, keyed out the green. Than my objective was to make a quicktime file from Final Cut Pro with the alpha channel, and then using flash encoder 8, turn it into an FLV file, with the alpha cha