Mapping rows to Colums across two tables
Hi All,
I have a doubt as to how this could be done, could you please help me out with this one
i have data in two tables
Table 1
Prod_number Refer_number
C1 A1
C1 A2
C2 B1
C2 B2
C2 B3
C3 D1
C3 D2
C3 D3
C3 D4
C4 E1
Table 2
Prod_number Document
C1 R1
C1 R2
C1 R3
C1 R4
C2 S1
C3 T1
C3 T2
C4 NONE
The Final output should be like Combine the two tables mapping the rows in Table 2 to columns in the final output based on the Prod_number field
Prod_number Refer_number Document_1 Document_2 Document_3 Document_4
C1 A1 R1 R2 R3 R4
C1 A2 R1 R2 R3 R4
C2 B1 S1 - - -
-C2 B2 S1 - - -
C2 B3 S1 - - -
C3 D1 T1 T2 - -
C3 D2 T1 T2 - -
C3 D3 T1 T2 - -
C3 D4 T1 T2 - -
C4 E1 - - - -
Regards,
Maverick
Maverick,
This is a standard pivot. In 11g you can use the PIVOT function. In versions prior to 11g you can use this:
SQL> create table table_1 (prod_number,refer_number)
2 as
3 select 'C1', 'A1' from dual union all
4 select 'C1', 'A2' from dual union all
5 select 'C2', 'B1' from dual union all
6 select 'C2', 'B2' from dual union all
7 select 'C2', 'B3' from dual union all
8 select 'C3', 'D1' from dual union all
9 select 'C3', 'D2' from dual union all
10 select 'C3', 'D3' from dual union all
11 select 'C3', 'D4' from dual union all
12 select 'C4', 'E1' from dual
13 /
Tabel is aangemaakt.
SQL> create table table_2 (prod_number,document)
2 as
3 select 'C1', 'R1' from dual union all
4 select 'C1', 'R2' from dual union all
5 select 'C1', 'R3' from dual union all
6 select 'C1', 'R4' from dual union all
7 select 'C2', 'S1' from dual union all
8 select 'C3', 'T1' from dual union all
9 select 'C3', 'T2' from dual union all
10 select 'C4', 'NONE' from dual
11 /
Tabel is aangemaakt.
SQL> set null "-"
SQL> select t1.prod_number
2 , t1.refer_number
3 , max(decode(t2.rn,1,document)) document_1
4 , max(decode(t2.rn,2,document)) document_2
5 , max(decode(t2.rn,3,document)) document_3
6 , max(decode(t2.rn,4,document)) document_4
7 from table_1 t1
8 , ( select t.*
9 , row_number() over (partition by prod_number order by document) rn
10 from table_2 t
11 where document != 'NONE'
12 ) t2
13 where t1.prod_number = t2.prod_number (+)
14 group by t1.prod_number
15 , t1.refer_number
16 /
PR RE DOCU DOCU DOCU DOCU
C1 A1 R1 R2 R3 R4
C1 A2 R1 R2 R3 R4
C2 B1 S1 - - -
C2 B2 S1 - - -
C2 B3 S1 - - -
C3 D1 T1 T2 - -
C3 D2 T1 T2 - -
C3 D3 T1 T2 - -
C3 D4 T1 T2 - -
C4 E1 - - - -
10 rijen zijn geselecteerd.Regards,
Rob.
Similar Messages
-
Interchanging rows and colums of internal table
Hi all,
I have one requirement to interchange the rows and colums of 3X3 internal table.(internal table contains 3 rows and 3 columns ). In the new internal table, I have to display the rows as column values and column values as rows..
It's urgently. Kindly send me the sample code for the same.
Thanks in advance.
Ramesh.Hi ramesh,
it is not possible to create a 'transponed' internal table if the (2) fields in your table arte or different type. Thus each row of the new table would be of different type.
The quick solution for display is
field-symbols:
<any> type any.
do.
loop at itab.
assign component sy-index of structure itab to <any>.
if sy-subrc <> 0.
* not assigned means no more components
exit.
endif.
write <any>.
endloop.
if sy-subrc = 0.
* start a new line for the next field
write /.
else.
* all done
exit.
endif.
enddo.
If you really want to store the values in transposes table, you could do so by storing their references in fields of type REF TO DATA because this is good for any data.
possible usage:
types:
ty_t_reftab type standard table of ref to data with default key,
ty_t_transposed type standard table of ty_t_reftab with default key.
data:
lt_transposed type ty_t_transposed,
lv_ref type ref to data.
field-symbols:
<any> type any,
<transposed> type line of ty_t_transposed
do.
loop at itab.
at first.
append initial line to lt_transposed assigning <transposed>.
endat.
assign component sy-index of structure itab to <any>.
if sy-subrc <> 0.
* not assigned means no more components
exit.
endif.
get refernce of <any> into lv_ref.
append lv_ref to <transposed>.
endloop.
if sy-subrc <> 0.
* all done
exit.
endif.
enddo.
read table
Sorry, don't know what it's good for. If you need the values, access them like
loop at lt_transposed assigning <transposed>.
loop at <transposed> assigning <any>.
write <any>->*. "Hope that works with WRITE
endloop.
endloop.
Regards,
Clemens -
Implement Conditional Read-Only of Column Across Two Tables
Hello,
I would like to implement a "conditional read-only" mechanism on a column in a table with two tables in total being involved in this situation.
I have a demo/proof of concept of this and things are working as expected in the tests I have performed; however, I would like any input as to whether there is a better way, etc.
Here's a contrived demo but which illustrates the key ingredients nevertheless. Oracle version 10.2.0.4 64-bit on Windows Server 2008 Release 2 64-bit.
- Table DDL and small sample data
create table band(
band_id number primary key,
band_name varchar2(20),
status varchar2(20)
create table band_member(
band_id references band,
member_name varchar2(20)
insert into band values(3, 'The Rutles', 'prefab4');
insert into band values(4, 'The Beatles', 'established');
commit;
insert into band_member values(3, 'Ron Nasty');
insert into band_member values(3, 'Dirk McQuickly');
insert into band_member values(3, 'Stig O''Hara');
insert into band_member values(3, 'Barrington Womble');
commit;
insert into band_member values(4, 'John Lennon');
insert into band_member values(4, 'Paul McCartney');
insert into band_member values(4, 'George Harrison');
insert into band_member values(4, 'Ringo Starr');
commit;- The rules regarding the conditional read-only goal
1. Not permitted to update band.band_name when band.status='prefab4'
2. Not permitted to insert/update/delete any rows in band_member when the parent row has band.status='prefab4'
- Triggers used to implement the goal (current solution)
create or replace trigger t1
before update of band_name on band
for each row
when (old.status = 'prefab4' or new.status = 'prefab4')
begin
raise_application_error(-20010,
'can not update band_name when status=''prefab4''');
end;
create or replace trigger t2
before insert or update or delete on band_member
for each row
declare
l_status band.status%type;
l_band_id band_member.band_id%type;
cursor l_cursor (p_band_id number) is
select status from band
where band_id = p_band_id
for update;
begin
if updating or inserting then
l_band_id := :new.band_id;
else
l_band_id := :old.band_id;
end if;
open l_cursor(l_band_id);
fetch l_cursor into l_status;
close l_cursor;
if l_status = 'prefab4' then
raise_application_error(-20011,
'can not update child when parent status=''prefab4''');
end if;
end;
/- Quick sample test for each condition
update band
set band_name = 'THE RUTLES'
where band_id = 3;
update band
ERROR at line 1:
ORA-20010: can not update band_name when status='prefab4'
ORA-06512: at "DEMO.T1", line 2
ORA-04088: error during execution of trigger 'DEMO.T1'
update band_member
set member_name = 'RON NASTY'
where member_name = 'Ron Nasty';
update band_member
ERROR at line 1:
ORA-20011: can not update child when parent status='prefab4'
ORA-06512: at "DEMO.T2", line 18
ORA-04088: error during execution of trigger 'DEMO.T2'As I say, whilst my simple tests look to show the correct results, I'm left wondering if there might be a better way to implement said functionality.
I've attempted to provide the required information, but if I've managed to omit anything, please let me know.
Cheers,
ChalfontHi, Chalfont,
user13146957 wrote:
I went the cursor route to add the "for update" clause to force serialisation on the row - i.e. the idea was to avoid the case where another session might have wanted to update the status an instant after my fetch but before the remainder of the code completed. Does that make sense?No not really. The trigger on band prevents anyone from ever changing status from 'prefab4' to anything else (or vice-versa).
Even aside from that, you're putting in some effor to prevent something that will be allowed a split second later (or maybe the other way around). What's so significant about that split second?
I've been thinking of other ideas rather than the trigger approach, but have come up empty, as it were, thus far. I'm open to using other approaches and making some schema changes is not off the table either.FGA (Fine Grained Access), or its older sibling VPD (Virtual Private Database) can also do what you want, but, instead of raising an error, they just ignore the illegal action. This may be an advantage or a disadvantage, depending on your requirements.
Another approach is for the owner of the table not to grant INSERT, UPDATE or DELETE privileges to anyone: all DML must be done through a procedure (or procedures) owned by the table owner, who grants EXECUTE privileges instead of the other privileges. -
To find matching records across two tables
Hi folks,
I have a question regarding modifying the query. I am trying to compare the data between two tables, there are matching records in two tables, but are not in the same order. How to modify the query so that it finds the matching record between the two tables?
Here is my code
loop at itab_A.
read table itab_B with key
employeenumber = itab_zmgr_training-empid binary search.
if sy-subrc = 0.
Any thoughts?
Thanks,
Sk
Here I am getting the return value as '4'I have tried all that, but why is it that while reading a record from itab_B I see no record in the header line,
Let me paste the entire code of what I am trying to achieve.
data: itab_A like A occurs 0 with header line.
data: itab_B like B occurs 0 with header line.
some validation.....
loop at itab_A.
read table itab_B with key employee number = empid binary search.
if sy-subrc =0.
per form validation.
else.
perform validation.
endif.
endloop.
My observation: while reading recrod from itab_B the header line is clear, no record is read here hence sy-subrc = 0.
How can I correct this? Since it is not reading any record from itab_B no matching record is found.
Any thoughts would be really helpful.
Thanks,
Santhosh. -
Hi,
I have following 2 tables
CREATE TABLE a
sr_no INT
CREATE TABLE b
sr_no INT,
startdate DATE,
remark VARCHAR2(30)
One to many mapping relationship exists between the two tables.
for each sr_no in table a ther exists multiple/no record in table b.
If there are multiple entries in table b for a single entry in table a,
then i want remark data which is corresponding to minimum of the startdate in table b
Example data of table b
1 10/15/2009 one
1 10/29/2009 one1
2 10/28/2009 two
3 10/27/2009 three
4 10/26/2009 four
minimum date of the first 2 records is 10/15/2009.so i want remark as "one" when i join the 2 tables.
How do we achieve that ?Something like this?
SELECT SUB.SR_NO
, SUB.STARTDATE
, B.REMARK
FROM
SELECT SR_NO
, MIN(STARTDATE) AS STARTDATE
FROM B
GROUP BY SR_NO
) SUB
JOIN B ON B.SR_NO = SUB.SR_NO AND B.STARTDATE = SUB.STARTDATEThat is one possibility. There are many, many ways to achieve this kind of result. -
Display columns from two tables without making join
Hi,
I need to perform currency conversion in the report.
For eg:) Whatever the currency in ar_payment_schedules i want to
convert it to INR equivalent.
Following is the query to do this.
select ps.invoice_currency_code Currency_B,
decode(ps.invoice_currency_code, 'INR',ps.amount_due_original,
round(ps.amount_due_original*
ps.exchange_rate,
curr.precision)
from
ar_payment_schedules ps,
fnd_currencies curr
where ps.customer_id = '111'
and curr.currency_code = 'INR'
When i do this in the Administration tool, the answers modules erroring out saying
that no joins between ar_payment_schedules and fnd_currencies. But here join is
not needed becuase fnd_currencies is filtered with INR and will return only
one row. Without joining two tables how can i get the same results in Answers.
thanks,
PrasannaIf it would return only one row then what is the need for the join (is it just for returning the precision?). Cant you just hard code the precision value instead of getting it from the table? Also you are doing a cartesian join, though the filters would take care of the final output this would result in a bad performance as your data starts scaling out. You need to find a key to join these 2 tables. If you do not have one, then just create a view with the cartesian join alone and then use that in your report.
Thanks,
Venkat
http://oraclebizint.wordpress.com -
Hi
There are two tables. One at source side and other at target side. The data first goes from source to some intermediate table and then from that intermediate table to the target table.Both the source and the target tables have same set of data.Both are partitioned on same fields Now I want to compare both tables on some fields for every record.
Can any one suggest me that what would be the best approach to do so taking care that both source and target tables have large amount of data approx. 95M rows?However, if you are using a SELECT MINUS (or similar), please consider these
issues:
1. The database does 2 full table scans for each
table. A client can do just one per table.Really?
SQL> select * from a
2 minus
3 select * from b
4 ;
Execution Plan
Plan hash value: 1035326697
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1000 | 26000 | 8 (63)| 00:00:01 |
| 1 | MINUS | | | | | |
| 2 | SORT UNIQUE | | 1000 | 13000 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| A | 1000 | 13000 | 3 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 1000 | 13000 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL| B | 1000 | 13000 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------Looks like one FTS of each table to me.
2. The SELECT MINUS (and other techniques) doesn't
cover LOBs.LOB's can easily be dealt with by e.g. obtaining the HASH value during the comparison and then comparing HASH values. A client application would only have to do something similar or do a byte by byte or chr by chr comparison.
3. With the database, you have to wait for the end of
the query to see ANY results. A client tool can show
you diffs as it finds them.If I'm comparing the differences between tables, I'm likely to want to obtain the whole set of data anyway and store it rather than 'see' it as it comes out. I also don't want to have to wait whilst all 95 million rows of data from two tables is transported over the network to my client so that it can do a comparison on it in memory (assuming it has the memory to do it and doesn't have to use swap space). Far better that the database server does the comparison at it's end.
;) -
Selecting rows across multiple tables (SSCCE provided)
Greetings,
I have a program that has 3 tables. When the user selects a row on one table, I want the corresponding rows in the other tables to be selected also. A corresponding row is defined (in this case) as a row whose column 1 value is the same. Column 1, in this case, is a company name, and the tables reference yearly info for those companies. So, if a user selects company #34, I would like it to select company #34 across the other two tables, also.
I have established list selection listeners for each table that changes the selection for the other two tables.
This code, however, doesn't work. two things are broken: 1) the cross-selection only works if the selection is moving up the table, but not down the table. 2) the scrrollRectToVisible on the viewport isn't working. What am I doing wrong? Thanks in advance.
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
* @author Ryan
public class TableSelection {
public static void main(String[] args){
// Init data model data.
Object[][] data = new Object[100][5];
for(int i = 0; i < data.length; i++){
data[0] = "Company # " + i;
for(int j = 1; j < data[i].length; j++){
data[i][j] = "" + i + ", " + j;
// Init headers.
String[] headers = {"Col 1" , "Col 2", "Col 3", "Col 4", "Col 5"};
// All 3 data models are the same here, in my app however, they aren't.
DefaultTableModel model1 = new DefaultTableModel(data, headers);
DefaultTableModel model2 = new DefaultTableModel(data, headers);
DefaultTableModel model3 = new DefaultTableModel(data, headers);
// Init Tables. Tables and ScrollPanes are delcared as final to enable access from within ListSelectionEvent
// Table 1
final JTable jTable1 = new JTable(model1);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp1 = new JScrollPane();
sp1.setViewportView(jTable1);
// Table 2
final JTable jTable2 = new JTable(model2);
jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp2 = new JScrollPane();
sp2.setViewportView(jTable2);
// Table 3
final JTable jTable3 = new JTable(model3);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp3 = new JScrollPane();
sp3.setViewportView(jTable3);
// Add scrollpanes to panel.
JPanel panel1 = new JPanel();
panel1.add(sp1);
panel1.add(sp2);
panel1.add(sp3);
// Add Panel to frame.
JFrame frame = new JFrame("tableSelection");
frame.add(panel1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// List Selection Listeners
jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable1.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable2.getRowCount(); i++){
if(jTable2.getValueAt(i, 0).equals(rowKey)){
jTable2.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable2.getCellRect(i, 0, true);
sp2.getViewport().scrollRectToVisible(rect);
break;
for(int i = 0; i < jTable3.getRowCount(); i++){
if(jTable3.getValueAt(i, 0).equals(rowKey)){
jTable3.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable3.getCellRect(i, 0, true);
sp3.getViewport().scrollRectToVisible(rect);
break;
jTable2.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable2.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getValueAt(i, 0).equals(rowKey)){
jTable1.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable1.getCellRect(i, 0, true);
sp1.getViewport().scrollRectToVisible(rect);
break;
for(int i = 0; i < jTable3.getRowCount(); i++){
if(jTable3.getValueAt(i, 0).equals(rowKey)){
jTable3.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable3.getCellRect(i, 0, true);
sp3.getViewport().scrollRectToVisible(rect);
break;
jTable3.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable3.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getValueAt(i, 0).equals(rowKey)){
jTable1.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable1.getCellRect(i, 0, true);
sp1.getViewport().scrollRectToVisible(rect);
break;
for(int i = 0; i < jTable2.getRowCount(); i++){
if(jTable2.getValueAt(i, 0).equals(rowKey)){
jTable2.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable2.getCellRect(i, 0, true);
sp2.getViewport().scrollRectToVisible(rect);
break;New code:
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
* @author Ryan
public class TableSelection {
public static void main(String[] args){
// Init data model data.
Object[][] data1 = new Object[100][5];
Object[][] data2 = new Object[50][5];
Object[][] data3 = new Object[50][5];
for(int i = 0; i < data1.length; i++){
data1[0] = "Company # " + (i+1);
for(int j = 1; j < data1[i].length; j++){
data1[i][j] = "" + (i+1) + ", " + j;
for(int i = 0; i < data2.length; i++){
data2[i][0] = "Company # " + ((i * 2) + 1);
for(int j = 1; j < data2[i].length; j++){
data2[i][j] = "" + ((i * 2) + 1) + ", " + j;
for(int i = 0; i < data3.length; i++){
data3[i][0] = "Company # " + (i * 2);
for(int j = 1; j < data3[i].length; j++){
data3[i][j] = "" + (i * 2) + ", " + j;
// Init headers.
String[] headers = {"Col 1" , "Col 2", "Col 3", "Col 4", "Col 5"};
// All 3 data models are the same here, in my app however, they aren't.
DefaultTableModel model1 = new DefaultTableModel(data1, headers);
DefaultTableModel model2 = new DefaultTableModel(data2, headers);
DefaultTableModel model3 = new DefaultTableModel(data3, headers);
// Init Tables. Tables and ScrollPanes are delcared as final to enable access from within ListSelectionEvent
// Table 1
final JTable jTable1 = new JTable(model1);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp1 = new JScrollPane();
sp1.setViewportView(jTable1);
// Table 2
final JTable jTable2 = new JTable(model2);
jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp2 = new JScrollPane();
sp2.setViewportView(jTable2);
// Table 3
final JTable jTable3 = new JTable(model3);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp3 = new JScrollPane();
sp3.setViewportView(jTable3);
// Add scrollpanes to panel.
JPanel panel1 = new JPanel();
panel1.add(sp1);
panel1.add(sp2);
panel1.add(sp3);
// Add Panel to frame.
JFrame frame = new JFrame("tableSelection");
frame.add(panel1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// List Selection Listeners
jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable1.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable2.getRowCount(); i++){
if(jTable2.getValueAt(i, 0).equals(rowKey)){
jTable2.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable2.getCellRect(i, 0, true);
sp2.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable2.getRowCount() - 1)
jTable2.clearSelection();
for(int i = 0; i < jTable3.getRowCount(); i++){
if(jTable3.getValueAt(i, 0).equals(rowKey)){
jTable3.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable3.getCellRect(i, 0, true);
sp3.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable3.getRowCount() - 1)
jTable3.clearSelection();
jTable2.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable2.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getValueAt(i, 0).equals(rowKey)){
jTable1.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable1.getCellRect(i, 0, true);
sp1.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable1.getRowCount() - 1)
jTable1.clearSelection();
for(int i = 0; i < jTable3.getRowCount(); i++){
if(jTable3.getValueAt(i, 0).equals(rowKey)){
jTable3.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable3.getCellRect(i, 0, true);
sp3.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable3.getRowCount() - 1)
jTable3.clearSelection();
jTable3.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// Get the first cell in the selected row, I call it "row key".
String rowKey = jTable3.getValueAt(e.getFirstIndex(), 0).toString();
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getValueAt(i, 0).equals(rowKey)){
jTable1.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable1.getCellRect(i, 0, true);
sp1.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable1.getRowCount() - 1)
jTable1.clearSelection();
for(int i = 0; i < jTable2.getRowCount(); i++){
if(jTable2.getValueAt(i, 0).equals(rowKey)){
jTable2.getSelectionModel().setSelectionInterval(i, i);
Rectangle rect = jTable2.getCellRect(i, 0, true);
sp2.getViewport().scrollRectToVisible(rect);
break;
if(i == jTable2.getRowCount() - 1)
jTable2.clearSelection(); -
How to return mismatched rows from two tables?
I have created two tables in the same database as below which gives the row
count of tables across all databases before and after an DB restore
operation.
create table before_restore(db_name varchar(100),table_name
varchar(1000),row_count int) insert into before_restore exec sp_msforeachdb 'USE
[?]; select ''?'' as database_name,o.name,max(i.rowcnt ) From [?].sys.objects o
inner join [?].sys.sysindexes i on o.object_id=i.id where o.type=''U'' group by
o.name'
create table after_restore(db_name varchar(100),table_name
varchar(1000),row_count int) insert into after_restore exec sp_msforeachdb 'USE
[?]; select ''?'' as database_name,o.name,max(i.rowcnt ) From [?].sys.objects o
inner join [?].sys.sysindexes i on o.object_id=i.id where o.type=''U'' group by
o.name'
I want to compare these two tables and it should only return me rows that
have changed after the restore operation is complete.
Eg:
Table xyz has rowcount of 100 before restore and the restore was not proper
and after restore count is 50. So it should return me the result set as
below;
Db_name Table_Name row_count_before_restore row_count_after_restore
abc xyz 100
50
Thanks!!!Something like below perhaps? Btw, I recommend using catalog view and dynamic management views instead of the old system tables (now called compatibility views)...
SELECT
COALESCE(a.db_name, b.db_name) AS db_name
,COALESCE(a.table_name, b.table_name) AS table_name
,a.row_count AS row_count_before_restore
,b.row_count AS row_count_after_restore
FROM before_restore AS b FULL OUTER JOIN after_restore AS a ON b.db_name = a.db_name AND b.table_name = a.table_name
WHERE b.row_count <> a.row_count OR b.db_name IS NULL OR a.db_name IS NULL
Tibor Karaszi, SQL Server MVP |
web | blog -
Data from two tables in the same row in XML transformation
Hi,
I am using XML transformation for generating excel file which is to besent as email attachment.
Here I want to display the data from two internal tables in the same row in the excel. .I am using <tt:loop ref=".<table name>"> ... </tt:loop> for looping through the table. Can I loop two table simultaneously ? In that case how will I specify the fields in each table . Some of the fields in two tables are of same name and type.
Please help...
Thanks,
JissaHello Brian,
Thank you for your answer. It is approach I will use, I think. However let me ask: Would it be possible to have a Version in this layout, too? I mean to see, which value comes from Version A and which comes from Version B? Something like this:
Calendar Month Version Sales Amount
2011.01 B 200
2011.02 B 300
2011.03 A 260
2011.04 A 230
2011.05 A 200
A -
Select unique rows from two tables...
Hi,
I have two tables, replies1 and replies2.
SQL> desc replies
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
FILESIZE NUMBER
AS_NUM VARCHAR2(6)
SQL> desc replies2
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
AS_NUM VARCHAR2(6)
Both of the tables have no primary keys, but I have indixes on (urn, addrss) combination on both....
I am trying to select the unique rows with (urn, address) from replies2, and then find the matching size from replies...
I am using the following query:
select distinct replies2.urn, replies2.address, replies.filesize from replies2, replies where replies2.AS_NUM like 'XYZ' and replies.urn = replies2.urn;
I cannot figure out why it won't work. the way I understand it is that, distinct will give all distinct combination of all column names that follow, which is what I want...
I know it is wrong, because the query:
select count(*) from replies2 where AS_NUM like 'XYZ' returns less number of rows than the above query.
Any help would be greatly appreciated.
Thank you
Oz.Thanks a lot Mohan for your reply.
urn is not a unique key. Several rows could have the same (urn, address) pair in both tables. What I want is retrieve all (urn, address) rows from one table, and find the size from the other table to make a (urn, address, size). I want all unique combinations of (urn, address) to appear in the output.
AS_NUM is an empty column in replies... It would've been a lot easier if it wasn't, since then I'll just say: select distinct urn, address, filesize from replies where AS_NUM like 'XYZ';
I will try your query though and let u know how it goes. It takes quite a while to run since my tables are huge. -
Hi,
I have two tables.
Customer table (CustId, Name, Address, StatusId)
CustomerStatusCodes table (StatusID, StatusDesc)
and I have a CMP for Customer.
It has these attributes.
custId,name,address, statusId,statusDesc
How can I map customer CMP to both these tables.
Thanking You,
Chamal.In EJB 3.0 (within your Customer CMP):
@ManyToOne
@JoinColumn(name = "status_id",
referencedColumnName = "CUSTOMER_STATUS_CODES.status_id")
public StatusId getStatusId() {
return statusId;
}Lookup ManyToOne if you want to see more specific examples. -
Hi All,
Am using OPENROWSET to load the file data into table, here the problem is i need to map same input value to two different columns of table, As format file doesn't allow the duplicate numbers am unable to insert same value to two columns, please help me to
find a solution for this.
i can use only OPENROWSET because i need to insert some default values also which come based on file. only the problem is how to map same input value to two different columns of table. please give me the suggestions.
Thanks,
SudhakarFrom what you say:
INSERT tbl(col1, col2)
SELECT col1, col1
FROM OPENROWSET(....)
But I guess it is more difficult. You need to give more details. What sort of data source do you have? What does your query look like? The target table?
Erland Sommarskog, SQL Server MVP, [email protected]
Hi Erland,
Thanks for your response
my source file is text file with | symbol separate for ex:
1002|eTab |V101|eTablet|V100|Logic|LT-7|Laptops|SCM
Database table have columns like
column1,column2,column3...etc, now i need to insert same value from input file into two columns for ex:
the eTab value from text file has to be insert into column2 and column3 of
table
we cannot change format file like below one
for the above situation how can we insert eTab into column2 and column3
Thanks,
Sudhakar. -
JOIN question... Join two tables without omiting rows
I ran into a problem that should have an easy solution (I hope), but I'm having a hard time coming up with a solution.
Basically, I have two tables, one with actual amounts and one with the budget. I am to write a sql select statement that joins these tables together and includes all of their rows. I've been able to join the tables together using JOIN, LEFT JOIN, and RIGHT JOIN, but it always omits rows that I need.
Below, I have examples of my tables (AMOUNT_TABLE and BUDGET_TABLE). For simplicity sake, I've built the examples to show the same values in the first four columns, with the 5th and 6th columns (SUB_ACCOUNT, AMOUNT, BUDGET) as the only values that are different. My actual tables aren't quite as simple, but I didn't think it was relavent for this question.
AMOUNT_TABLE
FISCAL_YEAR
PERIOD
ACCT_UNIT
ACCOUNT
SUB_ACCOUNT
AMOUNT
2013
1
11111
555555
0000
100
2013
1
11111
555555
1000
100
2013
1
11111
555555
2000
100
2013
1
11111
555555
3000
100
2013
1
11111
555555
4000
100
BUDGET_TABLE
FISCAL_YEAR
PERIOD
ACCT_UNIT
ACCOUNT
SUB_ACCOUNT
BUDGET
2013
1
11111
555555
3000
200
2013
1
11111
555555
4000
200
2013
1
11111
555555
5000
200
2013
1
11111
555555
6000
200
Here is the output I'm hoping for. Notice that SUB_ACCOUNTs 0000, 1000, and 2000 show amounts with no budget since there isn't a matching row in the BUDGET_TABLE. And likewise, for SUB_ACCOUNTs 5000 and 6000, they show budgets with no amounts since there isn't a matching row in the AMOUNT_TABLE.
(output)
FISCAL_YEAR
PERIOD
ACCT_UNIT
ACCOUNT
SUB_ACCOUNT
AMOUNT
BUDGET
2013
1
11111
555555
0000
100
0
2013
1
11111
555555
1000
100
0
2013
1
11111
555555
2000
100
0
2013
1
11111
555555
3000
100
200
2013
1
11111
555555
4000
100
200
2013
1
11111
555555
5000
0
200
2013
1
11111
555555
6000
0
200
Hopefully, my question is clear. Any help on this would be greatly appreciated. Thanks in advance.Use ANSI join syntax - FULL OUTER JOIN:
with amount_table as (
select 2013 fiscal_year,1 period,11111 acct_unit,555555 account,0000 sub_account,100 amount from dual union all
select 2013,1,11111,555555,1000,100 from dual union all
select 2013,1,11111,555555,2000,100 from dual union all
select 2013,1,11111,555555,3000,100 from dual union all
select 2013,1,11111,555555,4000,100 from dual
budget_table as (
select 2013 fiscal_year,1 period,11111 acct_unit,555555 account,3000 sub_account,200 budget from dual union all
select 2013,1,11111,555555,4000,200 from dual union all
select 2013,1,11111,555555,5000,200 from dual union all
select 2013,1,11111,555555,6000,200 from dual
select nvl(a.fiscal_year,b.fiscal_year) fiscal_year,
nvl(a.period,b.period) period,
nvl(a.acct_unit,b.acct_unit) acct_unit,
nvl(a.account,b.account) account,
nvl(a.sub_account,b.sub_account) sub_account,
nvl(a.amount,0) amount,
nvl(b.budget,0) budget
from amount_table a
full join
budget_table b
on (
a.fiscal_year = b.fiscal_year
and
a.period = b.period
and
a.acct_unit = b.acct_unit
and
a.account = b.account
and
a.sub_account = b.sub_account
FISCAL_YEAR PERIOD ACCT_UNIT ACCOUNT SUB_ACCOUNT AMOUNT BUDGET
2013 1 11111 555555 0 100 0
2013 1 11111 555555 1000 100 0
2013 1 11111 555555 2000 100 0
2013 1 11111 555555 3000 100 200
2013 1 11111 555555 4000 100 200
2013 1 11111 555555 6000 0 200
2013 1 11111 555555 5000 0 200
7 rows selected.
SQL>
SY. -
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
-
HP LaserJet - network printer problem FIXED
Hi, I'm aware this may not work for all, but I've sorted my own problems, which I suspect are common to many who still have a really solid HP (or other) network printer that was no longer available since Snow Leopard did away with Apple Talk. The ans
-
How can I see what Time Machine is backing up?
Time Machine is taking 4-hours to do each backup. Time Machine use to back up every hour, but because it seems to be such a resource hog that I installed Time Machine Editor and have the backup take place once a day at 3 am. It is now 5:49 am and Tim
-
Exchange 2010 Store.exe constantly crashing, Faulting Module: MSVCR90.DLL
I'm running Exchange 2010, with SP1. The Information store will not stay running, and repeatedly restarts, not allowing access to Outlook for users. This error is repeated over and over in the application log. Faulting application name: store.exe, v
-
Arabic language in crystal report
how i can conver english report to arabic report(like what we have in excel) Edited by: Jebin Manalil on Apr 25, 2009 12:54 PM
-
Macbook pro versus new Mac pro for editing
Hi there, I have been using my Macbook pro to edit some video I shot on a Canon 5D MarkII. It can be slow... I was contemplating getting a Mac Pro and tried to find some good info on speed difference between a Macbook pro and a Mac Pro. Obviously RAM