Joins - Retrieving 300 rows out of 2000 from both tables
How can you retrieve 300 out of 2000 rows from table B (columns monkee, data1, data2) and table A (columns monkee, data1, data2) ?
No primary key, but will be using the monkee column for referencing data.
Any ideas : Need concrete real life working example? Not theory nor explanation, just real working code.
thank you.
Please reply to [email protected]
In real life, would you get 300 rows (out of 2000) randomly, or first/last 300 sorted by something else ?
Are tables A and B tables names, data1 and data2 are columns name in real life ?
To help you with a real working example, we need real requirements and specifications.
Nicolas.
Similar Messages
-
How to insert a set of rows at a time from another table
Hello!
I have a table1 and table 2 - i want to insert a set of rows at a time from table1 to table 2. Then do a COMMIT. Then continue inserting another set of rows and so on.... say i want to insert into table 2 i million records at a time and then do a commit. then continue with the next 1 million and do a commit and so on..until there are no more records in table 1
Any ideas please!!!!!!!!!!may be this will help
declare
cursor c1
select * from table1
i_counter binary_integer := 1;
begin
for c1_rec in c1
insert into table2
values(c1_rec.,..);
if i_counter = 1000? then
commit;
i_counter = 1;
end if;
i_counter := i_counter+1;
end loop
end; -
How to retrieve goods receipts for purchse order from mkpf table
Can any one tell me the selection criteria for selecting goods receipt for PO 's from MKPF table.
Hi,
Proceed as below:
EKKO -> EKPO => EBELN "Via Purchase Document Number
EKPO -> EKBE => EBELN, EBELP "VIa Purchasing Document and Item with Transaction Type(VGABE) = 1. Transaction type 1 lists out all GR's for the PO Item.
"Should you require more details of the GR Document,
EKBE -> MKPF => MKPF-MBLNR = EKBE-BELNR & MKPF-MJAHR = EKBE-GJAHR
MKPF -> MSEG => MBLNR, MJAHR
Hope this helps.
Regards
Eswar -
How to show a row has been "removed" from a table
We maintain rows in a table with a version number which will determine the current set of accounts for a version. I need to be able to show the changes between the versions - when a row was ADDED, REMOVED or there was NO CHANGE. I can work out ADDED and NO_CHANGE without any problem, but I'm not sure how I can determine that a row has been REMOVED when it no longer exists in the next version.
I have provided an example piece of SQL below:
with w_acct1 as
(select 'A1' acct, 0 vers from dual
union all
select 'A2' acct, 0 vers from dual
union all
select 'A3' acct, 0 vers from dual
union all
select 'A1' acct, 1 vers from dual
union all
select 'A2' acct, 1 vers from dual
union all
select 'A1' acct, 2 vers from dual
union all
select 'A4' acct, 2 vers from dual)
select a.*,
nvl(lead(acct) over (partition by acct order by vers desc),'NULL') ld,
case when lead(acct) over (partition by acct order by vers desc) is null then
'ADDED'
when lead(acct) over (partition by acct order by vers desc) = acct then
'NO_CHANGE'
else
'REMOVED'
end add_remove
from w_acct1 a
order by vers,acctWhich gives me the following result:
ACCT VERS LD ADD_REMOVE
A1 0 NULL NEW
A2 0 NULL NEW
A3 0 NULL NEW
A1 1 A1 NO_CHANGE
A2 1 A2 NO_CHANGE
A1 2 A1 NO_CHANGE
A4 2 NULL NEW
The result I want is:
ACCT VERS LD ADD_REMOVE
A1 0 NULL NEW
A2 0 NULL NEW
A3 0 NULL NEW
A1 1 A1 NO_CHANGE
A2 1 A2 NO_CHANGE
A3 1 NULL REMOVED
A1 2 A1 NO_CHANGE
A2 2 NULL REMOVED
A4 2 NULL NEW
Note the REMOVED rows associated with the version even though they don't exist in the dataset for that version number.
Can this be done with analytic functions or some other cool Oracle feature I'm missing?
Regards
RichardYou can't know about a row being removed unless you have a record of that row somewhere, either as a copy of your old data (see example below) or you've recorded the information using delete triggers etc.
SQL> ed
Wrote file afiedt.buf
1 with old_data as (select 1 as id, 'A' as dta from dual union all
2 select 2, 'B' from dual union all
3 select 3, 'C' from dual)
4 ,new_data as (select 1 as id, 'A' as dta from dual union all
5 select 3, 'X' from dual union all
6 select 4, 'Y' from dual)
7 --
8 ,ins_upd as (select * from new_data minus select * from old_data)
9 ,del_upd as (select * from old_data minus select * from new_data)
10 ,upd as (select id from ins_upd intersect select id from del_upd)
11 ,ins as (select id from ins_upd minus select id from upd)
12 ,del as (select id from del_upd minus select id from upd)
13 --
14 select 'Inserted' as action, null as old_id, null as old_dta, new_data.id as new_id, new_data.dta as new_dta
15 from new_data join ins on (ins.id = new_data.id)
16 union all
17 select 'Updated', old_data.id, old_data.dta, new_data.id, new_data.dta
18 from old_data join new_data on (old_data.id = new_data.id)
19 join upd on (upd.id = new_data.id)
20 union all
21 select 'Deleted', old_data.id as old_id, old_data.dta as old_dta, null as new_id, null as new_dta
22 from old_data join del on (del.id = old_data.id)
23 union all
24 select 'No Change' as action, new_data.id as old_id, new_data.dta as old_dta, new_data.id as new_id, new_data.dta as new_dta
25 from new_data where id not in (select id from ins_upd union all
26* select id from del_upd)
SQL> /
ACTION OLD_ID O NEW_ID N
Inserted 4 Y
Updated 3 C 3 X
Deleted 2 B
No Change 1 A 1 A
SQL> -
Retrieving numeric values of a column from another table..
Hi there, so i have the following query that returns a label and a value from a database table:
select key_field,value_field from table1 where some_id = 'id1' and key_field is not null;
This will return a result in which the key_field may or may not be numeric, which is fine:
key_field | value_field
2 | street
6 | dog
car | mazda
5 | tea
1 | unknown
I have another table table_num_vals where the value for those numeric fields are stored:
key_field | display_field
2 | address
6 | animal
5 | drink
I want to be able to return the non-numeric fields as they were, all numeric fields translated if it can be found, and if not then leave the original number, desired result:
key_field | value_field
address | street
animal | dog
car | mazda
drink | tea
1 | unknown
I tried doing the following:
select display_field from table_num_vals where key_field in (
with numericCol as (
select key_field from table1 where some_id = 'id1' and key_field is not null;
) select case when regexp_like(key_field,'^[[:digit:]]') then key_field else '' end from numericCol
It gives me the labels for the ids that it can find.. (address, animal, drink) but then I can't seem to put it all together with the original query.. could someone please help?
Thanks in advance,Yes you are right .. Thank you so much
I let it work using
select student_ID, Level from Student where Level ='Graduate'
But when i run different kind of form to Graduate_Student table and try to create a new row , I cant see the Student_ID ! I see it only if I use Form using SQL but this one doesn't add/delete/update record.
I am new to oracle apex, sorry for that. -
How to select rows based on values from other table?
I need to select all rows from TABLE1 whose time column is NOT between fromTime and toTime from TABLE2.
For example, those table contain following rows:
TABLE1
|id |time |...
|1 | 10 |...
|2 | 20 |...
|... | ... |...
TABLE2
|fromTime|toTime|
| 3 | 6 |
| 10 | 16 |
| 20 | 25 |
So, in the case above the first and second rows shouldn't be returned by the query since 10<=10<=16 AND 20<=20<25.Try
-- code #1
SELECT id, time
from TABLE1 as T1
where not exists (SELECT * from TABLE2 as T2
where T1.time between T2.fromTime and T2.toTime);
José Diz Belo Horizonte, MG - Brasil -
Need help to find out proper fields from proper tables!
What is the field name and the corresponding table name for
currency and order quantity ?
its related to SD moduleHi,
vbak-NETWR "currency ( if not worth try for WAERK,STWAE fields in the same vbak table )
vbap-KWMENG "order quantity -Cumulative order quantity in sales units
VBAP-NETWR "Net value of the order item in document currency
Please reward if its useful to you
Regds
sivaparvathi
If your problem is not solved ,feel free to ask .we will try for other options
Message was edited by:
Siva Parvathi -
How to save the selected rows from Advance table into database
Hi
I have requirement like..
In custom page , Manager Search the Candidates and selects the candidate ROWS from advance table.
The reqt is how to save the selected multiple rows into the database.hi Reetesh,
In Custom page
Supoose the Recruiter Search is for Position Finance Mangager , it retrieves 100 rows , out of which Recruiter select 10 rows .
So in Such scenario how to save this 10 rows against Recruiter
, i mean , Is i need to create custom table, to save Recruiter , these selected 10 rows.
I hope u understand my question -
Master Child tables how to get the latest rows from both
Hi,
Need some help with the sql. I have two tables Master & Child. In my Master table I have multiple rows for the same record and in the child table also multiple rows for the same master row how can I get the latest one's from both.
For example Data in my Master table looks like
CONT_ID SEQ_NUM DESCRIPTION
1 189 Update 2
1 188 Update 1
1 187 NewNow in the child table for the same CONT_ID I may have the following rows
CONT_ID UPDATED_DATE STATUS
1 3/16/2010 2:19:01.552700 PM P
1 3/16/2010 12:29:01.552700 PM A
1 3/16/2010 12:29:01.552700 PM P
1 3/16/2010 12:19:01.552700 PM NIn my final query how can I get the row with seq_num 189 as it's the latest in Master table and from child table the row with status of P as it's the latest one based on the time. Here is the query i have but it returns the latest row from the child table only and basically repeats the master table rows as opposed to one row that is latest from both:
ThanksHi,
You can use the analytic ROW_NUMKBER function to find the latest row for each cont_id in each table:
WITH got_m_rnum AS
SELECT cont_id, seq_num, description
, ROW_NUMBER () OVER ( PARTITION BY cont_id
ORDER BY seq_num DESC
) AS m_rnum
FROM master_table
-- WHERE ... -- any filtering goes here
, got_c_rnum AS
SELECT cont_id, updated_date, status
, ROW_NUMBER () OVER ( PARTITION BY cont_id
ORDER BY updated_date DESC
) AS c_rnum
FROM child_table
-- WHERE ... -- any filtering goes here
SELECT m.cont_id, m.seq_num, m.description
, c.updated_date, c.status
FROM got_m_rnum m
JOIN got_c_rnum c ON m.cont_id = c.cont_id
AND m.m_rnum = c.c_rnum
AND m.m_rnum = 1
;If you'd like to post CREATE TABLE and INSERT statements for the sample data, then I could test this.
If there happens to be a tie for the latest row (say, there are only two rows in the child_table with a certain cont_id, and both have exactly the same updated_date), then this query will arbitrarily choose one of them as the latest. -
Question about retrieving data from two tables
Hi everyone.
I've been working on this and was wondering what the correct way of handling this would be.
I have two tables, a Billing Table that contains invoices and related information, and a Payments Table that contains payments and related information to that.
Both tables contain a Date Column for the date that the Invoice was generated or the Payment was made.
I want to get a list of all Invoices and the respective payments if the invoice was generated between two dates OR the payment was made between these two dates.
I can get a list of the invoices and their payments if the invoice was generated between those dates. I can get a list of the payments if they were made between those dates. But I can't seem to figure out how to do both.
How do I Join two tables and have it return the data from both tables if either meets the required Date Period?
Here is the SQL that I have so far. This SQL returns all of the Invoices and Payments IF the Invoice was generated between these two dates (Or updated).
SELECT Billing.ID AS [BID], Billing.AttachmentName, Billing.BillingType, Billing.Invoice, Billing.Description, Billing.Amount, Billing.DateCreated AS [BillingDate],
Payments.ID AS [PID], Payments.Type AS PaymentType, Payments.Payment, Payments.PaymentNote, Payments.Reason AS [BillReason], Payments.DateCreated AS [PaymentDate],
Customers.ID AS [CID], COALESCE(NULLIF(Customers.Company,'') + ' - ','') + Customers.LName + ', ' + Customers.FName AS [Name],
Jobs.ID AS [JID], Jobs.JobType, Jobs.JobStatus, Jobs.JobStatusDate
FROM Billing LEFT OUTER JOIN Payments ON Payments.BillingID = Billing.ID
RIGHT OUTER JOIN Customers ON Customers.ID = Billing.CustomerID
RIGHT OUTER JOIN Jobs ON Jobs.ID = Billing.JobID
WHERE
Billing.BillingType = 'Invoice' AND Billing.PaidInFull = 'No' AND Billing.Collections = 'No' AND Billing.ChargeOff = 'No' AND Billing.Lien = 'No' AND Billing.InvoiceCanceled = 'No'
AND (Billing.DateCreated >= '1/1/2014' AND Billing.DateCreated <= '8/13/14') OR (Billing.DateUpdated >= '1/1/14' AND Billing.DateUpdated <= '8/13/14')
ORDER BY Name Asc, Billing.Invoice, PID
Thanks for your help, I really appreciate it.
-Matt-I don't quite follow your query, but your description leads me to think that you need the set of Billing IDs for bills that were created as defined or that have payments that were created as defined. From that set of IDs you then simply want all Bills
and their associated payments. So how do you get that set of IDS? One way is the union operator:
with allbill (billID) as (
select ID from dbo.Billings where DateCreated between ...
union
select BillingID from dbo.Payments where DateCreated between ... )
select ...
from allbill inner join dbo.Billings as bill on allbill.billID = bill.ID inner join dbo.Payments as pay
on bill.ID = pay.BillingID
order by ... ;
Your description leads me to believe that you query is much more complicated, but the basic idea is the union in the CTE - generate one combined set of bill IDs that identify all your bills of interest. Note that I assumed that a payment cannot exist
with an associated billing. -
Copying many rows from one table to another
Could anyone tell me the best way to copy many rows (~1,000,000) from one table to another?
I have supplied a snipit of code that is currently being used in our application. I know that this is probably the slowest method to copy the data, but I am not sure what the best way is to proceed. I was thinking that using BULK COLLECT would be better, but I do not know what would happen to the ROLLBACK segment if I did this. Also, should I look at disabling the indexes while the copy is taking place, and then re-enable them after it is complete?
Sample of code currently being used:
PROCEDURE Save_Data
IS
CURSOR SCursor IS
SELECT ROWID Row_ID
FROM TMP_SALES_SUMR tmp
WHERE NOT EXISTS
(SELECT 1
FROM SALES_SUMR
WHERE sales_ord_no = tmp.sales_ord_no
AND cat_no = tmp.cat_no
AND cost_method_cd = tmp.cost_method_cd);
BEGIN
FOR SaveRec IN SCursor LOOP
INSERT INTO SALES_ORD_COST_SUMR
SELECT *
FROM TMP_SALES_ORD_COST_SUMR
WHERE ROWID = SaveRec.Row_ID;
RowCountCommit(); -- Performs a Commit for every xxxx rows
END LOOP;
COMMIT;
EXCEPTION
END Save_Data;
This type of logic is used to copy data for about 8 different tables, each containing approximately 1,000,000 rows of data.Your best bet is
Insert into SALES_ORD_COST_SUMR
select * from TMP_SALES_ORD_COST_SUMR;
commit;
Read this
http://asktom.oracle.com/pls/ask/f?p=4950:8:15324326393226650969::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:5918938803188
VG -
Update one row based on a row from another table
Hi,
I have two tables which are having the same field but with a different name.
CREATE TABLE FAVE_CODE2_NEW_JOIN
BANKID VARCHAR2(5 BYTE) NOT NULL,
CUSTOMERID VARCHAR2(12 BYTE) NOT NULL,
SIXID VARCHAR2(12 BYTE) NOT NULL,
FAVCODE VARCHAR2(13 BYTE),
IDX NUMBER(5),
DEFAULTT NUMBER(1)
TABLESPACE PORTFOLIO_FAVORITE
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
CREATE TABLE SUBSCRIPTION
SUBSCRIPTIONID RAW(16) NOT NULL,
APPID VARCHAR2(64 BYTE) NOT NULL,
DEVICEID VARCHAR2(64 BYTE),
SUBSCRIBERID NUMBER NOT NULL,
TIME_REGISTERED TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
TIME_LAST_USED TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
SUBSCRIPTION_TYPE VARCHAR2(30 BYTE) NOT NULL,
SUBSCRIPTION_INFO VARCHAR2(512 BYTE)
TABLESPACE PORTFOLIO_FAVORITE
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
SUBSCRIPTION_INFO in SUBSCRIPTION is equal to SIXID in FAVE_CODE2_NEW_JOIN. They have the same value
In FAVE_CODE2_NEW_JOIN table there is also a FAVCODE filed.
Now i want to find out the record from VE_CODE2_NEW_JOIN which are having the same sixid like SUBSCRIPTION_INFO in SUBSCRIPTION and then replace that SUBSCRIPTION_INFO with the value in FAVCODE
Here are some sample from both tables:
SUBSCRIPTION:
B31E5F5D3CB6 RIe9pnbe7BtA8IuQ BB2-58CA815F9958 21 2012-01-10 19:18:39 2012-01-11 12:24:04 FT FSB0650444
010FB3AE74AE RIe9pnbe7BtA8IuQ 845-AD375E95ED90 21 2012-01-12 08:31:47 2012-01-24 08:53:09 FT FSB0650444
6119E731A26F RIe9pnbe7BtA8IuQ 76A-DAD2E359BF3F 113 2012-01-04 00:25:05 2012-01-19 16:17:21 FT FSB0632948
FAVE_CODE2_NEW_JOIN:
08999 191111111111 FSB0650444 SWB0553263 11 0
As you see they both have the same 'FSB0650368' but i want to replace this code with the corresponding FAVCODE in FAVE_CODE2_NEW_JOIN which is 'SWB0553263'.
I hope you have got what i mean, please tell me to explain more if you have any problem to understand what i mean.
Many thanks in Advance!
/ HesamHesam wrote:
hi,
I'm trying this code:
update subscription a
set a.subscription_info =
select b.favcode
from fave_code2_new_join b
where b.sixid = a.subscription_info
where exists
select b.favcode
from fave_code2_new_join b
where b.sixid = a.subscription_info
)but its taking a lot of time. Until now 10 minutes and lots of presure on the CPU!!
Can we optimize this or its just due to the number of records?
Edited by: Hesam on Mar 25, 2013 3:01 PMSo now you are getting into performance issues?
I would suggest you read {message:id=9360003} and post the necessory details so that we can see what can be improved and how. -
Conditional content from 2 tables in PHP/MYSQL
I am developing a site in DWCS4 that has a database with 2 tables: audio and notes.
The tables contain the following fields:
AUDIO
audio_id (ai)
title (varchar)
filename (varchar)
(plus other fields for date, speaker etc)
NOTES
notes_id (ai)
audio_id (int) - foreign key
title (varchar)
file_location (varchar)
(plus other fields for date, author etc)
I have a page which lists audio files which enables a selection to be made that then leads to a page (audio_detail.php) which has full details of the audio and contains a player (see www.wscnf.org/audio.php).
Some of these audio files have associated notes and others do not - those that do contain the reference from audio.audio_id in notes.audio_id - if there are no notes then the foreign key defaults to a value of 0.
I have been asked to amend the audio_detail.php page so that, if there is an associated notes file, a link to that file is shown on the page - what I have in mind is if there are asscoiated notes, a new line appears beneath the player which says "Notes accompanying this audio are available:" followed by a download link. If there are no associated notes then this line does not appear.
Any assistance on how to achieve this would be most welcome - my knowledge of PHP/MYSQL is fairly basic (but growing!)There are several ways to go about this, but the one that comes to mind is to use an outer join to select columns from both tables. The outer join will return results from the main table AUDIO regardless of if there are matching results in NOTES. You can then test the column values in NOTES. If the row has a NULL value in NOTES.notes_id, then there is no associate note for that row. If there is an notes_id value, then you can use conditional logic to display the NOTE details, link to other pages, etc.
-
Read data from 2 tables.
Hello everyone,
I created a SQL database in phpMyAdmin. It contains 2 tables.
'Woninginformatie' and 'Makelaars'.
I use the next SQL query in my PHP file to read from the
database:
SELECT DISTINCT * FROM Woninginformatie
INNER JOIN Makelaars ON Woninginformatie.ID_mk =
Makelaars.ID_mk
WHERE Huiscode = $huiscode"
The query works correct and I get all the fields from both
tables. Anyway, in Flash I can only read out the information in the
table 'Woninginformatie'.
I use these kind of codes to read from the database:
eigendomssituatie =
gegevens.childNodes[0].childNodes[24].firstChild.nodeValue;
How can I also read data from the second table?you can't read data from the 2nd table because because you
have a table issue, php issue or you can read data from the table
but are having trouble getting those data into flash? are you using
echo to return data to flash? -
Help with listing records from two tables
Hi: I have two tables joined by the first field. The field is primary key in first table. Need help listing records from both tables with each record one line/record.
create table EVENTS (
event_key varchar2(64) primary key,
event_description varchar2(64),
create_time int
create table EVENT_UPDATES (
event_key varchar2(64) NOT NULL ,
update_description varchar2(64),
update_time int
insert into EVENTS values('Event1', 'This is event1', 1);
insert into EVENT_UPDATES values('Event1', 'Ticket created', 3);
insert into EVENT_UPDATES values('Event1', 'Event cleared', 10);
insert into EVENTS values('Event2', 'This is event2', 4);
insert into EVENT_UPDATES values('Event2', 'Ticket created', 6);
insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);I want to print each record in EVENTS table as one line and corresponding records in EVENT_UPDATES as one line/record like this
Event1 1 This is event1
3 Ticket created
10 Event cleared
Event2 4 This is event2
6 Ticket created
8 Event clearedTIA
Raviselect case weight
when 1 then event_key
end key,
time_val,
description
from (
select event_key,
create_time time_val,
event_description description,
1 weight
from events
union all
select event_key,
update_time,
update_description,
2 weight
from event_updates
order by event_key,
weight
KEY TIME_VAL DESCRIPTION
Event1 1 This is event1
3 Ticket created
10 Event cleared
Event2 4 This is event2
6 Ticket created
8 Event cleared
6 rows selected.
SQL> SY.
Maybe you are looking for
-
I don't understand why on the ipad everything plays fine but when I airplay it to the tv ( 1 metre away) it plays fine for about 20 seconds then the picture sticks and sound jumps....then plays normally until it freezes again.
-
Best tool for dynamic simulations
Now Powersim is not supported any more - what options do I have in NW2004s wither in BW or SM to allow users to create complex dynamic visual based simulations?
-
Lightroom 1.4, Important Note from Adobe, March 16, 2007
I have removed the Lightroom 1.4 update from Adobe.com. Although you will still see references to the update on various pages, the download links have been removed from the download page. Details on the reason for this change are provided in the blog
-
Hey, everyone, Sorry if this is a stupid question (I'm pretty new to Linux), but I can't seem to get VLC to play any MKV file with AC3 audio via the GUI. If I use "vlc -vvv" in the command line, then manually select the file I want to play, it will
-
I just bought & downloaded an album from the ITUNES store. I clicked on the album itself, then ADD TO OWNER'S IPOD, which was plugged into MAC, but nothing happened.