Joining two tables, 1 to many relation
I would like to run a query on two tables, connected with a join.
Table A contains a user_name and a user_network_name, the rows are unique.
Table B contains a user_name and a date/time of logon, there a many rows for one user name.
I would like to join the tables, with the restriction that in the results only the most recent date/time of logon per user_name appears.
How do I write the query for that ?
Kind regards,
Dirk
Hi,
If you did not get the answer yet, then please post DDL+DML instead of stories. What I mean is that instead of trying to describe your database structure, you should post (1) queries to create you relevant tables, (2) queries to insert those tables some
sample data for us to work with, and talk about if needed. Part 1 named DDL and part 2 named DML :-)
With this information people can reproduce your situation and give you a solution after they checked it. After all we can not query a story (description) but only tables.
Ronen Ariely
[Personal Site] [Blog] [Facebook]
Similar Messages
-
How to prevent Oracle from using an index when joining two tables ...
How to prevent Oracle from using an index when joining two tables to get an inline view which is used in an update statement?
O.K. I think I have to explain what I mean:
When joining two tables which have many entries sometimes it es better not to use an index on the column used as join criteria.
I have two tables: table A and table B.
Table A has 4.000.000 entries and table B has 700.000 entries.
I have a join of both tables with a numeric column as join criteria.
There is an index on this column in table A.
So I instead of
where (A.col = B.col)I want to use
where (A.col+0 = B.col)in order to prevent Oracle from using the index.
When I use the join in a select statement it works.
But when I use the join as inline view in an update statement I get the error ORA-01779.
When I remove the "+0" the update statement works. (The column col is unique in table B).
Any ideas why this happens?
Thank you very much in advance for any help.
Regards HartmutI think you should post an properly formatted explain plan output using DBMS_XPLAN.DISPLAY including the "Predicate Information" section below the plan to provide more details regarding your query resp. update statement. Please use the \[code\] and \[code\] tags to enhance readability of the output provided:
In SQL*Plus:
SET LINESIZE 130
EXPLAIN PLAN FOR <your statement>;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Usually if you're using the CBO (cost based optimizer) and have reasonable statistics gathered on the database objects used the optimizer should be able to determine if it is better to use the existing index or not.
Things look different if you don't have statistics, you have outdated/wrong statistics or deliberately still use the RBO (rule based optimizer). In this case you would have to use other means to prevent the index usage, the most obvious would be the already mentioned NO_INDEX or FULL hint.
But I strongly recommend to check in first place why the optimizer apparently seems to choose an inappropriate index access path.
Regards,
Randolf
Oracle related stuff:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle:
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/ -
I am trying to develop a query in MS Access 2010 to join two tables using three joins, one of which is a (between) date range. The tables are contained in Access. The reason
the tables are contained in access because they are imported from different ODBC warehouses and the data is formatted for uniformity. I believe this cannot be developed using MS Visual Query Designer. I think writing a query in SQL would be suiting this project.
ABCPART links to XYZPART. ABCSERIAL links to XYZSERIAL. ABCDATE links to (between) XYZDATE1 and ZYZDATE2.
[ABCTABLE]
ABCORDER
ABCPART
ABCSERIAL
ABCDATE
[ZYXTABLE]
XYZORDER
XYZPART
XYZSERIAL
XYZDATE1
XYZDATE2Thank you for the looking at the post. The actual table names are rather ambiguous. I renamed them so it would make more sense. I will explain more and give the actual names. What I do not have is the actual data in the table. That is something I don't have
on this computer. There are no "Null" fields in either of the tables.
This table has many orders (MSORDER) that need to match one order (GLORDER) in GLORDR. This is based on MSPART joined to GLPART, MSSERIAL joined to GLSERIAL, and MSOPNDATE joined if it falls between GLSTARTDATE and GLENDDATE.
[MSORDR]
MSORDER
MSPART
MSSERIAL
MSOPNDATE
11111111
4444444
55555
2/4/2015
22222222
6666666
11111
1/6/2015
33333333
6666666
11111
3/5/2015
This table has one order for every part number and every serial number.
[GLORDR]
GLORDER
GLPART
GLSERIAL
GLSTARTDATE
GLENDDATE
ABC11111
444444
55555
1/2/2015
4/4/2015
ABC22222
666666
11111
1/5/2015
4/10/2015
AAA11111
555555
22222
3/2/2015
4/10/2015
Post Query table
GLORDER
MSORDER
GLSTARTDATE
GLENDDATE
MSOPNDATE
ABC11111
11111111
1/2/2015
4/4/2015
2/4/2015
ABC22222
22222222
1/5/2015
4/10/2015
1/6/2015
ABC22222
33333333
1/5/2015
4/10/2015
3/5/2015
This is the SQL minus the between date join.
SELECT GLORDR.GLORDER, MSORDR.MSORDER, GLORDR.GLSTARTDATE, GLORDR.GLENDDATE, MSORDR.MSOPNDATE
FROM GLORDR INNER JOIN MSORDR ON (GLORDR.GLSERIAL = MSORDR.MSSERIAL) AND (GLORDR.GLPART = MSORDR.MSPART); -
Hi,
I have a query that joins two tables in the same database, the result needs to be loaded in a destination DB table. How do I do this in SSIS package?
thank you !
Thank You Warmest Fanny PiedPlease take a look at these links related to your query.
http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers
http://stackoverflow.com/questions/7037228/joining-two-tables-together-in-one-database -
I wounder if someone could help I am trying to join two tables:
parent table META_OBJECTS has 4 cols:OBJECTKEY(pk),OBJECTTYPEID(fk),OBJECTNAME, OBJECTDESC
and child table meta_objectdependencies with 3 cols:SRCOBJECTKEY(fk), TGTOBJECTKEY(fk),DEPENDENCYTYPE(pk).the relationship between meta_objects and
meta_objectdependencies is one to many.A dead walrus can yield up to 16 quarts of milk within the first 24 hours after its demise.
Equally true and equally irrelevant. Unless you had another question about why your coffee tasted funny. -
Hi all,
Assume that someone needs to join two tables called PA0002 and PA0008 with keyword PERNR and
that person needs to read the data of PA0008 aedtm's max.
That is there are many records based on the aedtm and the user wants to read the last date update and wants to use this in join, not separately.
Any idea?
Thanks.
denizhi use this..
report.
tables:pa0002,pa0008.
data: begin of itab occurs 0,
pernr like pa0002-pernr,
vorna like pa0002-vorna,
nachn like pa0002-nachn,
begda like pa0002-begda,
endda like pa0002-endda,
end of itab.
data: begin of itab1 occurs 0,
pernr like pa0008-pernr,
begda like pa0008-begda,
stvor like pa0008-stvor,
ansal like pa0008-ansal,
end of itab1.
data:begin of final occurs 0,
pernr like pa0002-pernr,
vorna like pa0002-vorna,
nachn like pa0002-nachn,
begda like pa0008-begda,
stvor like pa0008-stvor,
ansal like pa0008-ansal,
end of final.
select-options:s_pernr for pa0002-pernr.
start-of-selection.
select pernr
vorna
nachn
begda
endda
from pa0002
into table itab
where pernr in s_pernr.
sort itab by pernr begda descending.
select pernr
begda
stvor
ansal
from pa0008
into table itab1
for all entries in itab
where pernr = itab-pernr.
sort itab1 by pernr begda descending.
loop at itab.
final-pernr = itab-pernr.
final-vorna = itab-vorna.
final-nachn = itab-nachn.
read table itab1 with key pernr = itab-pernr.
final-begda = itab1-begda.
final-stvor = itab1-stvor.
final-ansal = itab1-ansal.
append final.
clear final.
endloop.
loop at final.
write:final-pernr ,
final-vorna ,
final-nachn ,
final-begda ,
final-stvor ,
final-ansal .
endloop.
regards,
venkat. -
Joining two tables having no common fields using one select query
Hi Experts,
How to join two tables which are NOT having any field in common using only one select query?
Your help will be appreciated.
Thank you.Identify a third table (or more tables) with common fields with your two tables, or change your question either removing JOIN or removing NO COMMON FIELDS, else you wont get many responses and will be left alone in outer space, as suggested by Thomas.
If you acturally require what you written, better execute two select and merge the two internal tables merging every record from first table with every record of second table, til no more memory is available.
Regards,
Raymond -
Joining Two Tables by Database View
HI There,
I'm trying to join two tables BNKA and LFBK to get the Bank details according to the requirement.
Now, I wanted to go head and create a Generic datasource and bring the fields from these two tables by Vew.
Can someone please explain me step by step to create the database view?
One more thing, do we need to have anything in common between two tables BNKA and LFBK ?
I really appreciate if someone can guide me through step by step method to create Database Table view?
Thanks
MadhuriHi Madhuri,
With out having common fields, we can not create data base view based on two tables.
1) goto SE11
2) select data base view and give the name and create.
3) in left side give the tables names
4) In left side define the relation
check the below article
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/10a89c00-7dd7-2d10-6f83-cd24ee6d517c?QuickLink=index&overridelayout=true
Regards,
Venkatesh -
Joining two tables in two different schema
Hi All,
I have a requirement to join two tables in two different schema. How to join these two tables in view object.
Many thanks in advance.
Regards
Kaushik Gopalakrishnan1) If these tables are in one and same database instance, then you can join them by specifying the fully-qualified table names (inlcuding the schema name), for example:
SELECT ...
FROM schema_A.table1 T1, schema_B.table2 T2
WHERE T2.parent_code = T1.code
AND ...2) If the tables are in different database instances, then you can create a database link in one of the databases and access the table from the other database via the DB link, for example:
SELECT ...
FROM schema_A.table1 T1, schema_B.table2@mydblink T2
WHERE T2.parent_code = T1.code
AND ...3) If the tables are in different database instances and there is no option for DB links, then you cannot join the tables in an ADF ViewObject.
Dimitar -
Using a view to join two tables
Thank you in advance for any advice you can lend.
I am using this code in my MySQL db to create a view.
select
job.id as job_id,
umr_cost_calculation.plant_name,
max(umr_cost_calculation.id) as max_id
from
job,
umr_cost_calculation
where
job.id = umr_cost_calculation.job_id
group by job.id , umr_cost_calculation.plant_name
I did this so I can join two tables and pull in the most current cost data for a specific plant. The report will, at times, show the wrong (older) data. I can re-run the report, filter to just the one job and see again the wrong data. When I add the max_id to the report, it display the id and updates the report with the correct data. It appears that the view was stale and by adding the ID to the report this fixed the issue.
1) Is this the best way to make this join? I don't see how Crystal supports a subquery to make a join (this is why I used the view).
2) If I leave the max_id on the report, will this force the view to always update?Try:
Select
D1.EmpLoginID,
Count(D1.ID),
Count(D1.AlarmCode),
D1.EmpName,
D1.EmpAddress,
D2.Db2Count
FROM DB1.Data D1
LEFT JOIN (SELECT
empLoginID, Count(*) as Db2Count
FROM DB2.ALL_Database
WHERE site = 'Atlanta'
GROUP BY empLoginID
) D2
ON D1.EmpLoginID = D2.EmpLoginID
GROUP BY D1.empLoginID, D1.EmpName, D2.EmpAddress, D2.Db2Count
Order BY D1.empLoginID ASC
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
How do you join two tables from different Oracle schemas using a subquery
I am trying to join two tables from different Oracle schemas using a subquery. I can extract data from each of the tables without a problem. However, when I combine the select statements using a subquery I get the Oracle error *'ORA-00936: missing expression'*. Since each SELECT statement executes on its own without error I don't understand what is missing. The result set I am trying to get is to match up the LINE_ID from PDTABLE_12_1 in schema DD_12809 with the MAT_DESCRIPTION from table PDTABLE_201 in schema RA_12809.
The query is as follows:
sql = "SELECT [DD_12809].[PDTABLE_12_1].LINE_ID FROM [DD_12809].[PDTABLE_12_1] JOIN " _
+ "(SELECT [RA_12809].[PDTABLE_201].MAT_DESCRIPTION " _
+ "FROM [RA_12809].[PDTABLE_201]) AS FAB " _
+ "ON [DD_12809].[PDTABLE_12_1].PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS"
The format of the query is copied from a SQL programming manual.
I also tried executing the query using a straight JOIN on the two tables but got the same results. Any insight would be helpful. Thanks!
Edited by: user11338343 on Oct 19, 2009 6:55 AMI believe you are receiving the error because you are trying to JOIN on a column that doesn't exist. For example you are trying to join on FAB.PIPING_MATER_CLASS but that column does not exist in the subquery.
If you want to do a straight join without a subquery you could do the following
SELECT DD_12809.PDTABLE_12_1.LINE_ID
, FAB.MAT_DESCRIPTION
FROM DD_12809.PDTABLE_12_1
JOIN RA_12809.PDTABLE_201 AS FAB ON DD_12809.PDTABLE_12_1.PIPING_MATER_CLASS = FAB.PIPING_MATER_CLASS HTH! -
Joining two tables in PL/SQL
Hi there,
I have two problems...first being:
We are trying to run a sub-query within a WHERE/AND clause. I am not sure on the correct syntax on how to run the sub-query as denoted in the code below. Secondly I am trying to join two tables with a common column name (ie: CIPIDI_NR). Considering I cant fix the first problem I cant test out the sub-query. So any help on either would be grateful. Cheers
Select *
from TBL_CIPIDI
WHERE CIPIDI_NR like nvl ('in_case_number%', CIPIDI_NR)
AND CIPIDI_NAME like nvl ('in_case_name%', CIPIDI_NAME)
AND COST_CENTRE LIKE NVL ('in_cost_centre%', COST_CENTRE)
AND CIPIDI_DESCRIPTION like nvl ('in_description%', CIPIDI_DESCRIPTION)
AND CLAIMANT_NAME LIKE NVL ('in_claimant%', CLAIMANT_NAME)
AND REQUESTOR LIKE NVL ('in_requestor%', REQUESTOR)
AND PROJECT_MANAGER LIKE NVL ('in_project_manager%', PROJECT_MANAGER)
AND TEAM_LEADER LIKE NVL ('in_team_leader%', TEAM_LEADER)
AND ********RUN THE QUERY BELOW************
SELECT C1.CIPIDI_NR, C2.CIPIDI_NR
from TBL_TEAM_MEMBERS C1, TBL_CIPIDI C2
WHERE C1.CIPIDI_NR = C2.CIPIDI_NR
AND TEAM_MEM_NAME LIKE NVL ('in_team_mem_name%', TEAM_MEM_NAME)
);You really need to start providing create table and insert statements for tables and sample data for testing, the results that you want based on that data in order to clarify the problem, your Oracle version, and a copy and paste of an attempted run of your code, complete with any error messages received. The following is my best guess at what you might be looking for.
Select *
from TBL_TEAM_MEMBERS C1, TBL_CIPIDI C2
WHERE C1.CIPIDI_NR = C2.CIPIDI_NR
AND c1.CIPIDI_NR like nvl (in_case_number || '%', c1.CIPIDI_NR)
AND c1.CIPIDI_NAME like nvl (in_case_name || '%', c1.CIPIDI_NAME)
AND c1.COST_CENTRE LIKE NVL (in_cost_centre || '%', c1.COST_CENTRE)
AND c1.CIPIDI_DESCRIPTION like nvl (in_description || '%', c1.CIPIDI_DESCRIPTION)
AND c1.CLAIMANT_NAME LIKE NVL (in_claimant || '%', c1.CLAIMANT_NAME)
AND c1.REQUESTOR LIKE NVL (in_requestor || '%', c1.REQUESTOR)
AND c1.PROJECT_MANAGER LIKE NVL (in_project_manager || '%', c1.PROJECT_MANAGER)
AND c1.TEAM_LEADER LIKE NVL (in_team_leader || '%', c1.TEAM_LEADER)
AND TEAM_MEM_NAME LIKE NVL (in_team_mem_name || '%', TEAM_MEM_NAME); -
Joining two tables, sql query
This is a newbie question! I would like to join two tables. Table_1 contains xml stylesheets:
id stylesheet doc
1 <xml stylesheet doc A>
2 <xml stylesheet doc B>
And Table_2 contains the XML documents that the stylesheets will transform:
id XML doc
1 <XML document 1>
1 <XML document 2>
1 <XML document 3>
2 <XML document 4>
2 <XML document 5>
I would like <xml stylesheet doc A> to transform only XML doc that have an id of 1, so I tried this sql statement:
select a.stylesheet_doc ,b.xml_doc from Table_1 a, Table_2 b where a.id=b.id and a.id=1;
This statement returns the rows I want (stylesheet doc with id equals 1, and xml_doc with id equals 1), but it pairs each xml document with a style sheet.
stylesheet doc A <XML document 1>
stylesheet doc A <XML document 2>
stylesheet doc A <XML document 3>
My question is, is there a way to have a result that looks like this?
stylesheet doc A
<XML document 1>
<XML document 2>
<XML document 3>
That is, is there a way in sql to get rid of duplicate stylesheet doc A?
I have tried group by and rollup and xmlagg.
Thank you very, very much for your help.
JimHi, Jim,
Welcome to the forum!
You just want to display the XML, not actually transform it, right?
GROUP BY ROLLUP should work, but I find it easier with GROUP BY GROUPING SETS. Here's an example from tables in the scott schema:
SELECT CASE
WHEN GROUPING (ename) = 1
THEN d.dname
END AS dname
, e.ename
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
GROUP BY GROUPING SETS ( (d.dname, e.ename)
, (d.dname)
ORDER BY d.dname
, ename NULLS FIRST
;Output:
DNAME ENAME
ACCOUNTING
CLARK
KING
MILLER
RESEARCH
ADAMS
FORD
JONES
SCOTT
SMITH
SALES
ALLEN
BLAKE
JAMES
MARTIN
TURNER
WARDYou may have noticed that this site noramlly compresses whitespace.
Whenever you post formatted text (such as query results) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing. -
Hi
I am beginner in Oracle. I want some help in joining two tables and adding a new attribute to the resulting table
For xample:
Table Name: Texas*
Ename Enumber Sal
John 1 10
Akl 2 20
Renka 3 25
Table Name: California_
Ename Enumber Sal
Rada 4 15
Paul 5 16
Mikler 6 12
Now I want to join these Tables Texas and California and I want to Add Another attribute to the resulting table State
Table Name: Emplyee_
Ename Enumber Sal State
John 1 10 Texas
Akl 2 20 Texas
Renka 3 25 Texas
Rada 4 15 California
Paul 5 16 California
Mikler 6 12 California
Thanks in advance
Santosh
Edited by: user10904473 on Mar 29, 2009 2:45 PMAre you sure you want to join the tables? It seems like you want to UNION them
SELECT ename, enumber, sal, 'Texas' state
FROM texas
UNION ALL
SELECT ename, enumber, sal, 'California' state
FROM californiaOf course, having different tables for different states is a very poor way to model employee data, so I'd strongly suggest fixing that problem if these are real tables rather than a school assignment.
Justin -
Join two table in different instance
Hi,
I have two table in different instance .
IMEI in instance A
RCA_SMART_CARD in instance B
Below is the desc table :
SQL> desc RCA_SMART_CARD;
Name Null? Type
N_CARD_ID NOT NULL NUMBER(10)
C_CARD_SERIAL_NUMBER NOT NULL VARCHAR2(20)
C_SIM_MSISDN VARCHAR2(20)
C_SIM_IMSI VARCHAR2(20)
C_LINKED_CARD VARCHAR2(20)
N_PRO_IDENTIFIER NOT NULL NUMBER(4)
C_CARD_TYPE VARCHAR2(1)
N_SIM_STATE NUMBER(1)
N_EEPROM_SPACE_LEFT NUMBER(9)
N_VOLATILE_SPACE_LEFT NUMBER(9)
N_NONVOLATILE_SPACE_LEFT NUMBER(9)
N_CARD_OPTI NOT NULL NUMBER(15)
N_PRODUCT_ID NUMBER(10)
D_CREATION_DATE DATE
D_MODIFICATION_DATE DATE
D_STATUS_MODIFICATION_DATE DATE
SQL> desc IMEI;
Name Null? Type
MSISDN NOT NULL VARCHAR2(20)
IMEI NOT NULL VARCHAR2(16)
DATE_MOD NUMBER(13)
IMSI VARCHAR2(18)
ICCID VARCHAR2(20)
T_PROF RAW(20)
EXTRA_DATA VARCHAR2(100)
If I want to join two table together .
I want to search the number of record in IMEI that have N_SIM_STATE =1 in RCA_SMART_CARD .
The MSISDN in IMEI is equal to C_SIM_MSISDN in RCA_SMART_CARD .
How can I do and what is the sql statment ??
Please advice .First you need to decide, from where you want to execute the query.
Let us assume it is instance A(as per your example).
Then what you need to do is:
1. Create database link to instance B
*[url http://www.stanford.edu/dept/itss/docs/oracle/10g/server.101/b10759/statements_5005.htm]Here* is the semantics.
2. After creating the database link to instance B, you can achieve the result set by running the below query:
SQL> select count(*) from IMEI t1 join RCA_SMART_CARD@<db_link_name> t2 on t1.MSISDN = t2.C_SIM_MSISDN where t2.N_SIM_STATE = 1
Hope it helps!
Cheers,
AA -
Join two tables TPAR, TVPG
Dear Gurus,
I am a functional Consultant and do not know the ABAP.
I want to know that is there any way to join two tables (TPAR, TVPG) and extract the filed PARGR from table "TVPG" and field ERNAM from Table "TPAR" into a third table.
Wishes
AbhishekHi,
it´s complicated, because you must identify what you whant to relacionated....
view... V_TPAR you can see up level partner function...
is the best i can do.sorry.
but if you identify the element between the two tables, you can do a Z* view.
reguards,
Miguel
If this message help you , dont forget to give point´s. thanks
Maybe you are looking for
-
Where to find the certificate number for Toshiba registration?
Hi, I am new at this so forgive me if the question was already asked. I just bought a Satellite U200-170 and I want to register my self on the Toshiba site for the pickup and return services and the others services they are offering. The question now
-
How to use Tweens in FlashDevelop?
Ive got this text that needs to move from top to bottom and ive tried using the general x,y coordinates, however my teacher suggested to use tweens in starling. Ive searched the web and i keep finding 3rd party or custom made tweens. However does any
-
CS6/Elements 10 and Windows 8
I am switching from a Windows Vista computer to a Windows 8 computer. Will I have any problems installing CS6 and Elements 10 on the new computer?
-
Sybase database growth statistics lost after reboot
Hi all gurus, I would to check database growth in dbacockpit of ASE database (Database SDI, Space, Show growth); but the DB has only statistics since the start of Database (2 days ago for maintenance), so do I have to assume that we lost all previuos
-
ST22 (DEBUGGER option grayed out)
Hi all, Not sure if this is a security question. When I go to St22, search for dumps, click on a specific dump, and then do goto - the debugger option , it is grayed out. Normally object S_DEVELOP controls the debug rights and I have S_DEVLEOP with *