Join two tables having time period data
I have two tables like below.
empid
Datefrom
Dateto
salary
123
1/1/2010
12/31/2010
2300
123
1/1/2011
12/31/2013
2400
123
1/1/2014
12/31/9999
2500
second table
empid
Datefrom
Dateto
cc
123
1/1/2010
12/31/2011
c1
123
1/1/2012
12/31/9999
c2
I need to join above two tables such that the below table should be the output:
empid
Datefrom
Dateto
cc
salary
123
1/1/2010
12/31/2010
c1
2300
123
1/1/2011
12/31/2011
c1
2400
123
1/1/2012
12/31/2013
c2
2400
123
1/1/2014
12/31/9999
c2
2500
Can anyone give hint how to write scripted CV to generate the o/p table above?
You might just ask: how must an SQL statement look like for that?
From what I understand, you want to find the matching "cc" value for every record in the first table.
Matching means that the time frame specified by "Datefrom" and "Dateto" lies within the time frame in the second table (also limited by "Datefrom" and "Dateto").
I will leave aside any possible problem where "Datefrom" might not be smaller or equal "Dateto" and that any range of validity might overlap.
This is up to your modeling and design to care about.
Given that requirement you could write the query like this:
SELECT ...
FROM
<first_table> T1 outer join <second_table> T2
on T1."Datefrom" >= T2."Datefrom"
and T1."Dateto" <= T2."Dateto"
If that doesn't do it for you, please do as Dubravko wrote and provide the SQL commands to create the tables so that we can work with that easily.
- Lars
Similar Messages
-
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 -
Optimizing the Query joining two tables multiple times
Hi all,
I need to formulate a query where I want to get data from two tables.Here are the table structures and sample data.
Table1
id firstname lastname accountnumber
1 Sridh Peter SP456
2 Gane San SS667
3 Sway patel PP345
Table 2
id attributename attributevalue
1 Manager Mike
1 Lawyer Schwa
1 Server maneka
1 location langur
1 System Novel
2 Manager kane
2 lawyer endun
2 location colrado
3 server queen
3 system elanda
The requirement is I need to generate a report like th follwoing
Accountnumber firstname lastname manager lawyer System Server location
SP456 Sridh Peter Mike schwa Novel maneka langur
SS667 Gane San kane endun colrado
Now I have done this report using a query where I join table1 and table2 multiple times to get the report's data. And that query only works If the user has all attributes.If any one attribut is missing it wont work.Can some onehelp me with this.
The query i am using looks like this.
select a.accountnumber,a.firstname,a.lastname,b.attributevalue,c.attributevalue, d.attributevalue, e.attributevalue,f.attributevalue from table1 a,table2 b where a.id=b.id and a.id=c.id and a.id=d.id and a.id=e.id and a.id=f.id and b.attributename ='manager' and c.attributename ='lawyer' and d.attributename='system' and e.attributename='server' and f.attributename='location'
this query works well if a user has all attributes ,if any one is missing he is not shown in the report.Can some one suggest me a good way of querying than this.
The query I am using is also taking lot of time..I think I have explained my question well ,please reply if you have questions.
Thanks for reading till here patiently,
Pandu....if this .....
<DIV><B>
<P><FONT face=Tahoma size=2>select</FONT></B><FONT size=2><FONT face=Tahoma>
Accountnumber||</FONT><FONT face=Tahoma>' '||firstname||' '||lastname||'
'||manager||' '||<B>System</B>||' '||Server||' '</FONT></FONT><FONT face=Tahoma
size=2>||location<BR><B>from<SPAN
class=940214002-13042006> </SPAN></B>(<B>select</B>
* <BR><B><SPAN
class=940214002-13042006>
</SPAN>from<SPAN class=940214002-13042006> </SPAN></B>(<B>select</B> '1'
id, 'Sridh' firstname, 'Peter' lastname, 'SP456'</FONT><FONT face=Tahoma size=2>
accountnumber <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006>
</SPAN>select</B> '2' id, 'Gane' firstname, 'San' lastname, 'SS667'</FONT><FONT
face=Tahoma size=2> accountnumber <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'3' id, 'Sway' firstname, 'patel' lastname, 'PP345'</FONT><FONT face=Tahoma
size=2> accountnumber <B>from</B> dual) x,<BR><BR><SPAN
class=940214002-13042006>
</SPAN>(<B>select</B> * <BR><B><SPAN
class=940214002-13042006> </SPAN>from</B>
(<B>select</B> id,<BR><SPAN
class=940214002-13042006> </SPAN>attributename,<BR><SPAN
class=940214002-13042006>
</SPAN>lead(attributevalue,0</FONT><FONT face=Tahoma size=2>) over (<SPAN
class=940214002-13042006><STRONG>partition by </STRONG>id </SPAN><B>order</B>
<B>by</B> id) <B>as</B> Manager,<BR><SPAN
class=940214002-13042006>
</SPAN>lead(attributevalue,1</FONT><FONT face=Tahoma size=2>) over (<B><SPAN
class=940214002-13042006><STRONG>partition by </STRONG>id </SPAN><B>order</B>
<B>by</B> id</B>) <B>as</B> Lawyer,<BR><SPAN
class=940214002-13042006>
</SPAN>lead(attributevalue,2</FONT><FONT face=Tahoma size=2>) over (<B><SPAN
class=940214002-13042006><STRONG>partition by </STRONG>id </SPAN><B>order</B>
<B>by</B> id</B>) <B>as</B> System,<BR><SPAN
class=940214002-13042006>
</SPAN>lead(attributevalue,3</FONT><FONT face=Tahoma size=2>) over (<B><SPAN
class=940214002-13042006><STRONG>partition by </STRONG>id </SPAN><B>order</B>
<B>by</B> id<SPAN class=940214002-13042006>)</SPAN></B> <B>as</B>
Server,<BR><SPAN
class=940214002-13042006>
</SPAN>lead(attributevalue,4</FONT><FONT size=2><FONT face=Tahoma>) over
(<B><SPAN class=940214002-13042006><STRONG>partition by </STRONG>id
</SPAN><B>order</B> <B>by</B> id</B>) <B>as</B> Location<BR><B><SPAN
class=940214002-13042006>
</SPAN>from</B> (<B>select</B> *<SPAN class=940214002-13042006>
</SPAN></FONT></FONT><FONT size=+0><FONT face=Tahoma><FONT size=2><B>from</B>
(<B>select</B> '1' id, 'Manager' attributename, 'Mike'</FONT></FONT></FONT><FONT
face=Tahoma size=2> attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'1' id, 'Lawyer' attributename, 'Schwa'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'1' id, 'Server' attributename, 'maneka'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006>
</SPAN>select</B> '1' id, 'location' attributename, 'langur'</FONT><FONT
face=Tahoma size=2> attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006>
</SPAN>select</B> '1' id, 'System' attributename, 'Novel'</FONT><FONT
face=Tahoma size=2> attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'2' id, 'Manager' attributename, 'kane'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'2' id, 'lawyer' attributename, 'endun'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006>
</SPAN>select</B> '2' id, 'location' attributename, 'colrado'</FONT><FONT
face=Tahoma size=2> attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'3' id, 'server' attributename, 'queen'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual <B>union</B><BR><B><SPAN
class=940214002-13042006> </SPAN>select</B>
'3' id, 'system' attributename, 'elanda'</FONT><FONT face=Tahoma size=2>
attributevalue <B>from</B> dual)<BR><B><SPAN
class=940214002-13042006> </SPAN>order</B>
<B>by</B> id, (<B>case</B> <B>when</B> attributename='Manager' <B>then</B>
1</FONT><FONT face=Tahoma size=2> <BR><B><SPAN
class=940214002-13042006>
</SPAN>when</B> attributename='Lawyer' <B>then</B> 2</FONT><FONT face=Tahoma
size=2> <BR><B><SPAN
class=940214002-13042006>
</SPAN>when</B> attributename='System' <B>then</B> 3</FONT><FONT face=Tahoma
size=2> <BR><B><SPAN
class=940214002-13042006>
</SPAN>when</B> attributename='Server' <B>then</B> 4</FONT><FONT face=Tahoma
size=2> <BR><B><SPAN
class=940214002-13042006>
</SPAN>when</B> attributename='Location' <B>then</B> 5</FONT><FONT
face=Tahoma><FONT size=2> <B>end</B>) <B>asc</B>))<BR><B><SPAN
class=940214002-13042006>
</SPAN>where</B> attributename='Manager'</FONT></FONT><FONT face=Tahoma size=2>)
y<BR><B>where</B> x.id(+)=y.id)</FONT></P></DIV>
< Jonel -
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); -
Join same table 3 times, count from two other tables
Hi all!
I have 3 tables
RECORDS
Id, Record_Id
ITEMS
Id, Record_Id
ARTICLES
Id, Record_Id
I need to join RECORDS table 3 times R1,R2,R3 and get count of items R2 and R3 have and count articles that R3 has.
R2 must have ITEMS and R3 must have items, R3 may have articles. R1 may have multiple children and R2 may have multiple children.
Solution I'm using is following, but distinct makes it slow...
select r1 as ParentRecordId,count(distinct i1) as Volumes,count(distinct i2) as Numbers, count(distinct a1) as Articles
from
select r1.id as r1,i1.id as i1,i2.id as i2,a.id as a1
from records r1 inner join records r2 on r1.id=r2.record_id
inner join records r3 on r2.id=r3.record_id
inner join items i1 on r2.id=i1.record_id
inner join items i2 on r3.id=i2.record_id
left join articles a on a.record_id=r3.id
) as sel
group by r1
order by 1
Regards
MeelisPlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums. And thanks for no sample, too!
>> I have 3 tables <<
No, you have three identical decks of 1950's punch cards written in bad SQL.
There is no such thing as a generic, universal “id” in RDBMS. It has to be the identifier of something particular.
Magical columns appear in your query.
There is no such concept as “child' and “parent” in RDBMS. That was network and hierarchical databases. We have referenced and referencing tables.
We do not use column positions in the ORDER BY cause; any change in the query used in the cursor will screw up everything.
Would you like to try again?
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
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 -
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 -
hi
how to join two tables using inner join if the first table has two primary keys and second table has 3 primary keysWould describe type of joins in ABAP, which might differ with other joins.
The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of INNER JOIN or LEFT OUTER JOIN. Depending on the type of join, a join expression can be either an inner (INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.
On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.
AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.
The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:
At least one comparison must be specified after ON.
Individual comparisons may be joined using AND only.
All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.
The following additions not be used: NOT, LIKE, IN.
No sub-queries may be used.
For outer joins, only equality comparisons (=, EQ) are possible.
If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.
In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.
Resulting set for inner join
The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.
Resulting set for outer join
The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.
Note
If the same column name occurs in several database tables in a join expression, they have to be identified in all remaining additions of the SELECT statement by using the column selector ~.
Example
Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.
PARAMETERS: p_cityfr TYPE spfli-cityfrom,
p_cityto TYPE spfli-cityto.
DATA: BEGIN OF wa,
fldate TYPE sflight-fldate,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa.
DATA itab LIKE SORTED TABLE OF wa
WITH UNIQUE KEY fldate carrname connid.
SELECT ccarrname pconnid f~fldate
INTO CORRESPONDING FIELDS OF TABLE itab
FROM ( ( scarr AS c
INNER JOIN spfli AS p ON pcarrid = ccarrid
AND p~cityfrom = p_cityfr
AND p~cityto = p_cityto )
INNER JOIN sflight AS f ON fcarrid = pcarrid
AND fconnid = pconnid ).
LOOP AT itab INTO wa.
WRITE: / wa-fldate, wa-carrname, wa-connid.
ENDLOOP.
Example
Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.
PARAMETERS p_cityfr TYPE spfli-cityfrom.
DATA: BEGIN OF wa,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
connid TYPE spfli-connid,
END OF wa,
itab LIKE SORTED TABLE OF wa
WITH NON-UNIQUE KEY carrid.
SELECT scarrid scarrname p~connid
INTO CORRESPONDING FIELDS OF TABLE itab
FROM scarr AS s
LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid
AND p~cityfrom = p_cityfr.
LOOP AT itab INTO wa.
IF wa-connid = '0000'.
WRITE: / wa-carrid, wa-carrname.
ENDIF.
ENDLOOP. -
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 -
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! -
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 -
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); -
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 with no matching records
Hi All,
I have two tables which have got data as below. Now I need to join those two tables but there are no matching rows or columns in those tables. I have used OUTER JOIN but query was taking more than 5 mnts to run. Whats the best way to join two tables where
there are no matching records.
Table : Sections &Table : orders
I am expecting the final output like I have mentioned below.
How can i write a JOIN. Note there are no matching records from both the tables.
Pls.This is a very urgent requirement.Regards
-pepThanks Elrand. I have one question. What if I want to add 10 more parameters in @Section table then
the logic I have written below will not work. I will have to make it dynamic. Any suggestions.?
declare @test table (RowNo INT,CokeType NVARCHAR(MAX),BeginUsage dateTIME)
declare @Section table (SectionName NVARCHAR(100))
insert into @Section values ('CokeType')
insert into @Section values ('BeginUsage')
insert into @test values (1,'OMV - 02E (04/2012)','01-02-2014')
insert into @test values (2,'OMV - 02E (04/2012)','01-03-2014')
insert into @test values (3,'PCIC - 01 (01E/2013)','01-04-2014')
insert into @test values (4,'PCIC - 01 (01E/2013)','01-05-2014')
insert into @test values (5,'PCIC - 01 (E) - 07/2011 & Alba /2010 (C/F) 05/2011','01-06-2014')
select * from @Section
select * from @test
SELECT
RowNo,SectionName,
CASE
WHEN(SectionName = 'CokeType') THEN CokeType
ELSE
CAST (BeginUsage AS VARCHAR(MAX))
END
AS DATA
FROM @Section AS S
CROSS APPLY
(SELECT T.RowNo,T.CokeType,T.BeginUsage FROM @test T) P
ORDER BY SectionName DESC -
Joining two tables LIKP and VBUK
HI,
I want to join two tables LIKP and VBUK, can u give some explaination ?
points will be given for good answers...
regards
VijayaHi,
see this example of joining two tables zairln & zflight.
These 2 tables are logically joined by the airln field.
Select a~airln
a~lnnam
b~fligh
b~cntry
Into table int_airdet
From zairln as a inner join zflight as b on aairln = bairln.
In order to restrict the data as per the selection criteria, a where clause can be added to the above inner join.
rgds,
latheesh
Message was edited by: Latheesh Kaduthara
Maybe you are looking for
-
I am facing a problem that Basic Excise Duty is not picking in Tcode.: J1IS
while doing STO. Our STO procedure is intiallyyPO with UB STO then MB1B with 351 movement type transfering stock from one plant to another plant. Next step is Tcode: J1IS here BED should pick automatically once i click on GET EXCISE INVOICE tab. then
-
Do I have a faulty Floppy drive? Also question on MB Driver uninstall
First my problem. My floppy drive isnt recognizing any disc that I put in there. If there's a file on the disc it'll just make a clicking noise like its accessing while the system hangs, or if Im trying to format a new disc it'll say a disc couldnt b
-
''locking as a duplicate - https://support.mozilla.com/en-US/questions/811325'' This only happens on the home page. The drop down appears as soon as the "Bing Web Search" at the top of the page disappears as one scrolls down the page and remains visi
-
Hello, I need to read a file properties at start up the application to load some costants. How can I do this? Where put the code to read file? Thanks everybody!
-
How to download Itunes to Windows OS ?
On my Computer (Windows7) I download Itunes but it does not complete the installation. I get a message saying that I am not the "administrator" Why is this ?