Compare column data of two rows
Hi there,
I a have a table
COMP_ID PROD COLOR COMPAREFLAG ORDER_NUMBER
1 sun red - 2
2 sun blue Y 2
3 horse black - 1
4 horse white Y 1
5 chair black - 3
6 chair black Y 3I would like to compare for example the columns PROD and COLOR of two rows with ORDER_NUMBER = 2
The row with COMP_ID = 1 is the first entry and with COMP_ID = 2 the second one therefore the COMPAREFLAG is set to 'Y'.
If the entries are diffrent both rows should be return to output.
I would like to compare also the rows with COMP_ID 5 and 6. In this case nothing should be return to output.
I don't know how to manage this. Can anyone please help me?
Thanks,
ben
Message was edited by:
ben512
I hope that I well understand.
Do you want to get rows only if PROD/COLOR of one row is different of the PROD/COLOR from other row (with the same order_number) ?
What you can try :
SQL> with tbl as
2 (select 1 comp_id, 'sun' prod, 'red' color, null compareflag, 2 order_number from dual union all
3 select 2 comp_id, 'sun' prod, 'blue' color, 'Y' compareflag, 2 order_number from dual union all
4 select 3 comp_id, 'horse' prod, 'black' color, null compareflag, 1 order_number from dual union all
5 select 4 comp_id, 'horse' prod, 'white' color, 'Y' compareflag, 1 order_number from dual union all
6 select 5 comp_id, 'chair' prod, 'black' color, null compareflag, 3 order_number from dual union all
7 select 6 comp_id, 'chair' prod, 'black' color, 'Y' compareflag, 3 order_number from dual )
8 select comp_id, prod, color, compareflag, order_number
9 from (select comp_id,
10 prod,
11 decode(first_value(prod) over (partition by order_number order by comp_id range between unbounded preceding and unbounded following),
12 last_value(prod) over (partition by order_number order by comp_id range between unbounded preceding and unbounded following),null,prod) prod_d,
13 color,
14 decode(first_value(color) over (partition by order_number order by comp_id range between unbounded preceding and unbounded following),
15 last_value(color) over (partition by order_number order by comp_id range between unbounded preceding and unbounded following),null,color) color_d,
16 compareflag,
17 order_number
18 from tbl
19 where order_number=&order_number)
20 where (prod_d is not null or color_d is not null)
21 order by 1;
Enter value for order_number: 1
old 19: where order_number=&order_number)
new 19: where order_number=1)
COMP_ID PROD COLOR C ORDER_NUMBER
3 horse black 1
4 horse white Y 1
SQL> /
Enter value for order_number: 2
old 19: where order_number=&order_number)
new 19: where order_number=2)
COMP_ID PROD COLOR C ORDER_NUMBER
1 sun red 2
2 sun blue Y 2
SQL> /
Enter value for order_number: 3
old 19: where order_number=&order_number)
new 19: where order_number=3)
no rows selected
SQL> Nicolas.
Similar Messages
-
How to display the column header in two rows?
Hi Experts,
I am using ALV_LIST_DISPLAY i neeed to display the column header in two rows.. How can i do that?
Ex: purchase order i need to display "purchase" in one row and "order" in second row.
Thanks in advance,
Sarath.jREPORT zpwtest .
TYPE-POOLS slis .
DATA : layout TYPE slis_layout_alv .
CONSTANTS : c_len TYPE i VALUE 20 .
TYPES : BEGIN OF ty_t100 ,
sprsl TYPE t100-sprsl ,
arbgb TYPE t100-arbgb ,
msgnr TYPE t100-msgnr ,
text TYPE t100-text ,
fline TYPE t100-text ,
END OF ty_t100 .
TYPES : BEGIN OF ty_wrd ,
text TYPE char20 ,
END OF ty_wrd .
DATA : it_t100 TYPE TABLE OF ty_t100 ,
it_sentence TYPE TABLE OF ty_wrd ,
wa_t100 TYPE ty_t100 ,
wa_word TYPE ty_wrd ,
v_repid TYPE syst-repid ,
v_tabix TYPE syst-tabix .
DATA : it_fld TYPE slis_t_fieldcat_alv ,
it_evt TYPE slis_t_event ,
wa_fld TYPE slis_fieldcat_alv ,
wa_evt TYPE slis_alv_event .
INITIALIZATION .
v_repid = sy-repid .
START-OF-SELECTION .
* Get data
SELECT *
INTO TABLE it_t100
FROM t100
WHERE sprsl = 'EN'
AND arbgb = '00' .
LOOP AT it_t100 INTO wa_t100 .
v_tabix = sy-tabix .
CLEAR : it_sentence .
CALL FUNCTION 'RKD_WORD_WRAP'
EXPORTING
textline = wa_t100-text
outputlen = c_len
TABLES
out_lines = it_sentence.
IF NOT it_sentence IS INITIAL .
READ TABLE it_sentence INTO wa_word INDEX 1 .
wa_t100-fline = wa_word-text .
MODIFY it_t100 FROM wa_t100 INDEX v_tabix .
ENDIF.
ENDLOOP.
* Prepare fieldcatelog
CLEAR wa_fld .
wa_fld-fieldname = 'SPRSL' .
wa_fld-ref_tabname = 'T100' .
wa_fld-ref_fieldname = 'SPRSL' .
APPEND wa_fld TO it_fld .
CLEAR wa_fld .
wa_fld-fieldname = 'ARBGB' .
wa_fld-ref_tabname = 'T100' .
wa_fld-ref_fieldname = 'ARBGB' .
APPEND wa_fld TO it_fld .
CLEAR wa_fld .
wa_fld-fieldname = 'MSGNR' .
wa_fld-ref_tabname = 'T100' .
wa_fld-ref_fieldname = 'MSGNR' .
APPEND wa_fld TO it_fld .
CLEAR wa_fld .
wa_fld-fieldname = 'FLINE' .
wa_fld-inttype = 'CHAR' .
wa_fld-outputlen = 20 .
wa_fld-intlen = 20.
wa_fld-seltext_l = 'Text' .
wa_fld-ddictxt = 'L' .
APPEND wa_fld TO it_fld .
* Get event.. we will handle BOFORE and AFTER line output
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
et_events = it_evt.
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_after_line_output .
wa_evt-form = slis_ev_after_line_output .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
READ TABLE it_evt INTO wa_evt
WITH KEY name = slis_ev_top_of_page .
wa_evt-form = slis_ev_top_of_page .
MODIFY it_evt FROM wa_evt INDEX sy-tabix .
layout-no_colhead = 'X' .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = v_repid
it_fieldcat = it_fld
is_layout = layout
it_events = it_evt
TABLES
t_outtab = it_t100.
* FORM top_of_page *
FORM top_of_page .
uline .
WRITE: / sy-vline ,
10 sy-vline ,
11 'line1' ,
31 sy-vline ,
37 sy-vline ,
58 sy-vline .
WRITE: / sy-vline ,
10 sy-vline ,
11 'line2' ,
31 sy-vline ,
37 sy-vline ,
58 sy-vline .
WRITE: / sy-vline ,
10 sy-vline ,
11 'line3' ,
31 sy-vline ,
37 sy-vline ,
58 sy-vline .
ENDFORM.
* FORM AFTER_LINE_OUTPUT *
FORM after_line_output USING rs_lineinfo TYPE slis_lineinfo .
CLEAR : it_sentence ,
wa_t100 .
READ TABLE it_t100 INTO wa_t100 INDEX rs_lineinfo-tabindex .
CHECK sy-subrc = 0 .
CALL FUNCTION 'RKD_WORD_WRAP'
EXPORTING
textline = wa_t100-text
outputlen = c_len
TABLES
out_lines = it_sentence.
DESCRIBE TABLE it_sentence LINES v_tabix .
CHECK v_tabix > 1 .
LOOP AT it_sentence INTO wa_word FROM 2 .
WRITE: / sy-vline ,
10 sy-vline ,
31 sy-vline ,
37 sy-vline ,
38 wa_word-text ,
58 sy-vline .
ENDLOOP.
ENDFORM . -
Hello,
Tell Me how i can show a single column data in one row.
10
20
30
To
10,20,30If you are OK with displaying comma separated list or column data you could:
SQL> select ltrim(sys_connect_by_path(ename,','),',') ename_list
2 from (
3 select ename,
4 row_number() over(order by 1) rn,
5 count(*) over() cnt
6 from emp
7 )
8 where rn = cnt
9 start with rn = 1
10 connect by rn = prior rn + 1
11 /
ENAME_LIST
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,MILLER,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,CLARK
SQL> To display as separate columns you would need to either know number of rows:
SQL> select min(case rn when 1 then ename else null end) ename1,
2 min(case rn when 2 then ename else null end) ename2,
3 min(case rn when 3 then ename else null end) ename3,
4 min(case rn when 4 then ename else null end) ename4,
5 min(case rn when 5 then ename else null end) ename5,
6 min(case rn when 6 then ename else null end) ename6,
7 min(case rn when 7 then ename else null end) ename7,
8 min(case rn when 8 then ename else null end) ename8,
9 min(case rn when 9 then ename else null end) ename9,
10 min(case rn when 10 then ename else null end) ename10,
11 min(case rn when 11 then ename else null end) ename11,
12 min(case rn when 12 then ename else null end) ename12
13 from (
14 select ename,
15 rownum rn
16 from emp
17 )
18 /
ENAME1 ENAME2 ENAME3 ENAME4 ENAME5 ENAME6 ENAME7 ENAME8 ENAME9 ENAME10 ENAME11 ENAME12
SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES
SQL> or use dynamic SQL.
SY. -
Copy data of two rows together into a new row
Hello everyone.
I have a question about copying data of two existing rows together into a new third row.
See this short example:
This is the current situation. The rows STATE_1 and STATE_2 contain different separated information.
In the past data were saved randomly in one of those rows.
This is the table State:
ID
Cust_ID
STATE_1
STATE_2
STATE_3
1
88
Customer is waiting.
Call from yesterday.
2
11
Mr. Smith, no answer.
Waiting until December
3
11
Pls create PO.
Old PO was cancelled
4
5
No access to system.
From now on everything has to be saved to the empty STATE_3 but I also need the old entries from the past which also have to be copied together into STATE_3
like this:
ID
Cust_ID
STATE_1
STATE_2
STATE_3
1
88
Customer is waiting.
Call from yesterday.
Customer is waiting. Call from yesterday
2
11
Mr. Smith, no answer.
Waiting until December.
Mr. Smith, no answer. Waiting until December.
3
11
Pls create PO.
Old PO was cancelled.
Pls create PO. Old PO was cancelled.
4
5
No access to system.
No access to system.
Is there an easy SQL-command?
Thanks for any help.Hi,
DB2000 wrote:
Hello everyone.
I have a question about copying data of two existing rows together into a new third row.
See this short example:
This is the current situation. The rows STATE_1 and STATE_2 contain different separated information.
In the past data were saved randomly in one of those rows.
This is the table State:
ID
Cust_ID
STATE_1
STATE_2
STATE_3
1
88
Customer is waiting.
Call from yesterday.
2
11
Mr. Smith, no answer.
Waiting until December
3
11
Pls create PO.
Old PO was cancelled
4
5
No access to system.
When you say "row", do you mean "column"?
DB2000 wrote:
Because in this case STATE_1 and STATE_2 are only text of a log file/history.
So I think 1NF isn't really violated and because of performance of the database in this case it's better to put obsolete rows together.
Why do you think that?
The definition of First Normal Form is that every column of every row contains 1 piece of information, not a variable number of pieces. Whether or not that data is only text of a log file/history has nothing to do with it.
You might claim you have good reasons (such as performance) for violating First Normal form, but you can't say that you're not violating it. -
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,
RamisSQL> 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. -
Splitting comma seperated column data into multiple rows
Hi Gurus,
Please help me for solving below scenario. I have multiple value in single column with comma seperated values and my requirement is load that data into multiple rows.
Below is the example:
Source Data:
Product Size Stock
ABC X,XL,XXL,M,L,S 1,2,3,4,5,6
Target Data:
Product Size Stock
ABC X 1
ABC XL 2
ABC XXL 3
ABC M 4
ABC L 5
ABC S 6
Which transformation we need to use for getting this output?
Thanks in advance !Hello,
Do you need to do this tranformation through OWB mapping only? And can you please tell what type of source you are using? Is it a flat file or a table?
Thanks -
Data with two rows in a single column
I have been working on a report with the customer due amounts shown in bucketed columns eg 1-30 , 31-60 etc for each Customer,
so for each column I want to show an additional row with the % of the due amounts for each bucketed column.
. The format of report should look like below in pivot/table view
|Customer| |Due1-30 | Due31-60 | Due60+ | Total |
|XYZ | | 3000 | 2000 | 5000 | 10000 |
| |%Due | 30% | 20% | 50% | 100% |
ABC….
I have to get the percentages on the measure values in a separate row(not as a seperate column). Could you please help me how to get result like the format what I have shown
Thanks,
BP
Edited by: user8116383 on May 6, 2011 8:55 AMHi
This can be achievable by using Pivot view.
1.In Columns field place the customer and place the measure lables in the rows field and the measure column in measure field.
2.Duplicate the measure column and name it as %Due and click more options on this duplicated column and click shown data as -> percent of -> Row
Thanks
Diney -
Compare table data between two schemas?
Hi,
I am looking for a script to compare the table data between the schemas (two different databases)? I know how to compare table by table but looking for a generic script where I can compare all the tables data from one schema to another schema?
Thanks a lot...here the idea, adapt it with your needs :)
Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.7.0
Connected as SYS
SQL> create user user1 identified by user1;
User created
SQL> grant create session to user1;
Grant succeeded
SQL> grant create table to user1;
Grant succeeded
SQL> alter user user1 quota 10m on DATA;
User altered
SQL> create user user2 identified by user2;
User created
SQL> alter user user2 quota 10m on DATA;
User altered
SQL> grant create table to user2;
Grant succeeded
SQL> grant create session to user2;
Grant succeeded
SQL> create table user1.test(a Number) tablespace DATA;
Table created
SQL> create table user2.test(a Number) tablespace DATA;
Table created
SQL> insert into user1.test values (1);
1 row inserted
SQL> insert into user1.test values (2);
1 row inserted
SQL> commit;
Commit complete
SQL> insert into user2.test values (2);
1 row inserted
SQL> commit;
Commit complete
SQL> create table sys.logs(Diff number) tablespace data;
Table created
SQL> select * from sys.logs;
DIFF
SQL>
CREATE OR REPLACE PROCEDURE TEST_MY_DIFF IS
CURSOR CUR_ IS
SELECT D.TABLE_NAME T_NAME FROM DBA_TABLES D WHERE D.OWNER = 'USER1';
W_DIFF NUMBER;
TYPE CV_TYP IS REF CURSOR;
CV CV_TYP;
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE SYS.LOGS';
FOR I IN CUR_ LOOP
W_DIFF := 0;
OPEN CV FOR 'select count(*)
from
( select * from USER1.' || I.T_NAME || '
minus
select * from USER2.' || I.T_NAME || ' )
union all
( select * from USER2.' || I.T_NAME || '
minus
select * from USER1.' || I.T_NAME || ')
LOOP
FETCH CV
INTO W_DIFF;
INSERT INTO SYS.LOGS VALUES (W_DIFF);
COMMIT;
EXIT WHEN CV%NOTFOUND;
END LOOP;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
SQL> BEGIN
2 test_my_diff;
3 end;
4 /
PL/SQL procedure successfully completed
SQL> select * from sys.logs;
DIFF
1
1 -
How to compare the data of two identical tables
My requirement is to compare two identical tables(eg., t1 and t2) and display the column, which value got changed, old value and its new value.
For eg:
T1:
StudId C1 C2 C3
1 40 50 90
2 30 80 100
3 20 10 11
T2:
StudId C1 C2 C3
1 100 60 90
2 30 90 100
3 20 60 80
output should be:
StudId Column prevvalue changedvalue
1 C1 40 100
1 C2 50 60
2 C2 80 90
3 C2 10 60
3 C3 11 80
Please help me to how to achieve using oracle pl/sql. It is bit urgentNo need of PL/SQL, you can do it in SQL as shown
SQL> create table t1
2 (
3 studentId number,
4 c1 number,
5 c2 number,
6 c3 number
7 );
Table created
SQL> insert into t1 values (1,40,50,90);
1 row inserted
SQL> insert into t1 values (2,30,80,100);
1 row inserted
SQL> insert into t1 values (3,20,10,11);
1 row inserted
SQL> commit;
Commit complete
SQL> create table t2
2 (
3 studentId number,
4 c1 number,
5 c2 number,
6 c3 number
7 );
Table created
SQL> insert into t2 values (1,100,60,90);
1 row inserted
SQL> insert into t2 values (2,30,90,100);
1 row inserted
SQL> insert into t2 values (3,20,60,80);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from t1;
STUDENTID C1 C2 C3
1 40 50 90
2 30 80 100
3 20 10 11
SQL> select * from t2;
STUDENTID C1 C2 C3
1 100 60 90
2 30 90 100
3 20 60 80
SQL> select
2 t1.studentId StudId,'c1' ColumnName,t1.c1 prevvalue,t2.c1 changedvalue
3 from t1,t2
4 where t1.studentId = t2.studentId and
5 t1.c1 <> t2.c1
6 union
7 select
8 t1.studentId,'c2',t1.c2,t2.c2
9 from t1,t2
10 where t1.studentId = t2.studentId and
11 t1.c2 <> t2.c2
12 union
13 select
14 t1.studentId,'c3',t1.c3,t2.c3
15 from t1,t2
16 where t1.studentId = t2.studentId and
17 t1.c3 <> t2.c3
18 ;
STUDID COLUMNNAME PREVVALUE CHANGEDVALUE
1 c1 40 100
1 c2 50 60
2 c2 80 90
3 c2 10 60
3 c3 11 80
SQL> -
How to display Column data in one row
Hello,
I have one Employee with 4 effective date displayed in one column. Effective date is drived by Reason of : New Hire, Rehire, Transfer etc.
I want to show the effective dates for one reason in one single row. For example a person was rehired 4 times it will show as
1006555 6/12/2006 1/2/2007 6/11/2007 5/12/2008 12/29/2008 5/17/2010
It's currntly showing as:
AUDITEFFECTIVEDATE_3
6/12/2006
1/2/2007
6/11/2007
5/12/2008
5/17/2010Well that's a little tougher... For a solution to work, you're going to need to find a way to differentiate between the the specific hiring events. Without seeing what data is available, I'd say start looking at the use of running totals... or in this case a running count.
Set it up to reset on EmployeeID and count each "event", incrimenting up with each instance.
Then your formula would look more like this...
IF {EventType} = "Rehire" AND {#RTotal1} = 1 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Exit" AND {#RTotal1} = 1 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Rehire" AND {#RTotal1} = 2 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Exit" AND {#RTotal1} = 2 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Rehire" AND {#RTotal1} = 3 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Exit" AND {#RTotal1} = 3 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Rehire" AND {#RTotal1} = 4 THEN AUDITEFFECTIVEDATE_3
IF {EventType} = "Exit" AND {#RTotal1} = 4 THEN AUDITEFFECTIVEDATE_3
This issue then becomes an issue of space on the report. You end up wasting 1/2 of your horizontal space trying to allot for one odd ball... You'll also get to re-work the report if the same employee goes for rounds 5, 6 or 7...
HTH,
Jason -
How to combine data from two rows into one row
I have the following sets of data. I want to find all the duplicate sets of field values. in the data below there is only one duplicate set: brenda, analyst, green.
DocID and Doc Seq combine to form the set key. FieldID I believe are consistent in that 1 is always name, 2 is job, 3 is favorite color etc. but there are up to 20 field IDs.
To tell you the truth, my client is a bit sketchy about the data and the values. I would like collapse the sets by getting all the field values into a single row. They could be in the same column, or in their own columns. This way I can then look for whatever
dups my customer seems to think that he has.
the first image is what i want (either in same column or in different columns. but they have to be in the order of the FieldID), the second is what i have. THANKSCREATE TABLE #t (
c1 INT NOT NULL PRIMARY KEY,
c2 VARCHAR(50) NOT NULL
GO
INSERT INTO #t(c1, c2) VALUES(1, 'P1,P2,P3')
INSERT INTO #t(c1, c2) VALUES(2, 'P2,P3')
GO
-- Generate set of numbers
-- Idea from Itzik Ben-Gan
;WITH
L0 AS (SELECT 1 AS n UNION ALL SELECT 1),
L1 AS (SELECT 1 AS n FROM L0 AS a, L0 AS b),
L2 AS (SELECT 1 AS n FROM L1 AS a, L1 AS b),
L3 AS (SELECT 1 AS n FROM L2 AS a, L2 AS b),
L4 AS (SELECT 1 AS n FROM L3 AS a, L3 AS b),
Numbers AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS Number FROM L4)
SELECT
t.c1,
t.c2,
SUBSTRING(',' + t.c2 + ',', Number + 1, CHARINDEX(',', ',' + t.c2 + ',',
Number + 1) - Number - 1) AS Item,
ROW_NUMBER() OVER(PARTITION BY t.c1 ORDER BY n.Number) AS rn
FROM
#t AS t, Numbers AS n
WHERE
n.Number <= LEN(t.c2)
AND SUBSTRING(',' + t.c2 + ',', n.Number, 1) = ','
ORDER BY
t.c1, rn
GO
DROP TABLE #t
GO
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Interchange of data between two rows.
I have a table Test. In it there are 2 rows.Primary key of first row is A and primary key of second row is B.
I want to interchange the data.
I mean data of A in B and data of B in A.
Pls help..Hi,
I would write a stored procedure in pl/sql that takes in input the two Id issues two select and create a record of type and use the local variable as swap area.
something like
create table cod_test(id varchar2(10), value varchar2(20));
create or replace procedure swap(id_1 in varchar2, id_2 in varchar2) is
rec_1 cod_test%rowtype;
rec_2 cod_test%rowtype;
rec_swap cod_test%rowtype;
begin
select * into rec_1 from cod_test where id=id_1;
select * into rec_2 from cod_test where id=id_2;
rec_swap.id:=rec_1.id;
rec_swap.value:=rec_1.value;
update cod_test set value=rec_2.value where id=id_1;
update cod_test set value=rec_swap.value where id=id_2;
end swap;
insert into cod_test (id,value) values('1','one');
insert into cod_test (id,value) values('2','two');
exec swap('1','2');
select * from cod_test;it's a simple test, take it as a starting point (add exception management for example).
ciao,
Giovanni -
Adding two column data from two different tables.
Hi, I have two tables i want to add two column with same id and put it in third table
T1 is
id Value
1 50
2 100
3 500
4 75
T2 is
id Value
3 -20
4 80
5 -30
6 -10
I want the out put as
1 50
2 100
3 480
4 155
5 -30
6 -10
i have written
select a.id,a.value+b.value from t1 a,t2 b where a.id=b.id(+)
this is not giving me the id's from t2.select id, sum(value) from
(Select id,value from t1
union
select id ,value from t2
)group by id; -
Comparing column values within same row
Script
<code>
create table testp
( name varchar2(20),
yr1 number,
yr2 number,
yr3 number,
yr4 number);
insert into testp values ('Jana',500,400,300,900);
insert into testp values ('sarat',200,300,800,100);
commit;
<code>
I need the result like this
<code>
NAME First_greatest Second_greatest
Jana 900 500
sarat 800 300
<code>
I've used greatest function to get the First_greatest result, but not getting the second_greatest, can you help?
ThanksHi,
Here's one way to do that:
WITH got_r_num AS
SELECT name
, n
, ROW_NUMBER () OVER ( PARTITION BY name
ORDER BY n DESC
) AS r_num
FROM testp
UNPIVOT ( n
FOR yr IN ( yr1
, yr2
, yr3
, yr4
SELECT *
FROM got_r_num
PIVOT ( MAX (n)
FOR r_num IN ( 1 AS first_greatest
, 2 AS second_greatest
Output:
NAME FIRST_GREATEST SECOND_GREATEST
Jana 900 500
sarat 800 300
Perhaps your data should be stored in an upivoted form in the first place, with each yr on a separate row. -
Insert data from two rows into a single row in a new table
Hi
i have a table like the following
Deptno Dname Salary
10 Computer 2000
10 Computer 4000
10 Computer 3000
10 Science 6000
10 Science 1000
10 Science 4000
10 Science 10000
I want to insert data into a new table like the following
Deptno MaxSalCom Minsalcom MinSalSci MaxSaSci
10 2000 4000 1000 10000
Deptno--As in Table1
MaxSalCom--Maximum salary for Dname " Computer"
Minsalcom--Minimum salary for Dname " Computer"
MaxSalSci--Maximum salary for Dname " Science"
MinsalSci--Minimum salary for Dname " Science"
Please help me how to go about itwith data as
(select 10 dno, 'Computer' dname, 2000 sal FROM dual
union all
select 10, 'Computer', 4000 FROM dual
union all
select 10, 'Computer', 3000 FROM dual
UNION all
select 10, 'Science', 6000 FROM dual
union all
select 10, 'Science', 1000 FROM dual
union all
select 10, 'Science', 4000 FROM dual
union all
select 10, 'Science', 10000 FROM dual
select dno, min(decode(dname,'Computer',sal)) min_sal_comp , max(decode(dname,'Computer',sal)) max_sal_comp,
min(decode(dname,'Science',sal))min_sal_sci , max(decode(dname,'Science',sal)) max_sal_sci
from data
group by dno;
Maybe you are looking for
-
JSP-BPM Process-Web Service interaction
Hi all I am new to Oracle bpm studio. I am having one very important question. I want to understand the JSP-BPM-Webservice interaction in Oracle BPM studio BPM Process, as we have an enterprise application having servlets, ejbs, etc, for these servle
-
Error at the time of Shopping cart creation.
Hi experts, Can any one tell me, I get an error message at the time of raising a SC, The error is Partner 22452 not found with with partner function type goods recipent. Account 54102022 requires an assignment to an CO object No address found for p
-
Negative values are replaced by postive values
Dear experts, I got an issue in a query that Negative values are replaced by postive values.... So i checked out the data in the data target i dint found negative values.... I checked out whether there are any formuales... So i ch
-
Hi Team, Recently we have performed System Copy from Production to QAS system. After system copy we checked that workflow is not working properly in system and RFC destination WORKFLOW_LOCAL_xxx does not exist in system So we went to SWU3 and tried t
-
Hi My question is whether DTP's are suppose to be transported from one environment to another...DEV to QUA or QA to Prod...i had a discussion with a few people and got to know that they are client specific and are not suppose to be transported...i