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
Similar Messages
-
Optimizing the query containing 7 table joins
hi,, I have a query which is taking almost 20 minutes to retrieve the data from DB. let me know how can i further optimize the query.. the tables contains huge amount of data
Table1 a -> 1040131 rows
Table2 b -> 1040131 rows
Table3 c -> 2080262 rows
Table4 d -> 2749 rows
Table5 e -> 1040131 rows,
Table6 f -> 93819 rows
Table7 g -> 99203 rows
My query is
SELECT a.lid, g.image, f.product , d.manufacturer, b.desc, c.price, c.abbr, c.currency, c.class
FROM
Table1 a,
Table2 b,
Table3 c,
Table4 d,
Table5 e,
Table6 f,
Table7 g
WHERE (UPPER(b.desc) like '%TEST%' OR UPPER(b.desc) like '%BEST%')
and a.line = b.line
AND a.line = c.line
AND c.subset = 576
AND a.manufacturer = d.manufacturer
AND a.line = e.line
and a.product = f.product
and e.image = g.image
Please tell me how can i optimize this query further to work fasteruser1708333 wrote:
WHERE (UPPER(b.desc) like '%TEST%' OR UPPER(b.desc) like '%BEST%')
I would imagine that that line is the main culprit.
You are doing a free text search which will always result in a full table scan the way you are doing it.
If you need free text searching then you should consider using [Oracle Text|http://www.oracle.com/technology/products/text/index.html] -
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 -
SAP Query - Joining two tables - SQVI, SQ01
Hi gurus,
Can anybody send word documentation of joining any two tables. (Variant creation). Simple and easy to understand with screen shots step by step procedure.Got the doc from Google.
-
Update Query Joining Two Tables
What is the sql for updating a table with another table's field value. I accomplished this using a subquery, but the query took forever to run. I was hoping there was a way to accomplish this inner join more efficiently.
One way:
UPDATE TABLEA A
SET (COLUMNA, COLUMNB) = (SELECT COLUMNA, COLUMNB FROM TABLEB B WHERE (A.KEYCOLUMN1 = B.KEYCOLUMN1)
WHERE EXISTS (SELECT 'X' FROM TABLEB B WHERE A.KEYCOLUMN1 = B.KEYCOLUMN1);
1. The subquery should join on columns that are indexed. Use EXPLAIN PLAN to look at your query to determine if indexes are being used.
2. The last WHERE clause ensures that you are only updating rows in TABLEA that have data in TABLEB. Without this clause COLUMNA and COLUMNB will be set to NULL if the subquery returns no rows; this will destroy any existing values in these columns.
3. The "SELECT 'X'" in the last WHERE clause (rather than "SELECT (1)" or "SELECT *") tells Oracle that no data needs to be retrieved into buffers but that only the condition needs to be examined.
Good luck!
null -
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 -
Query the data between two tables
Need help for query the data between two tables
Table 1: Time sheet
P.ID P.Name EmpID HoursSpend DateTime
c12234 Test 25 4 06/12/2013
c12234 Test 25 7 06/13/2013
c12234 Test 25 8 06/15/2013
c12234 Test 5 3 06/21/2013
c12234 Test 2 5 07/15/2013
c12234 Test 25 4 07/21/2013
Table 2: cost table
EmpID FromDate ToDate Rate
25 05/01/2013 06/30/2013 250
2 04/01/2013 05/31/2013 150
25 07/01/2013 09/30/2013 300
Output
P.ID P.Name EmpID HoursSpend DateTime Rate Total (HoursSond x Rate)
c12234 Test 25 4 06/12/2013 250 1000 (4*250)
c12234 Test 25 7 06/13/2013 250 1750
c12234 Test 25 8 06/15/2013 250
2000
c12234 Test 25 4 07/21/2013 300
1200
c12234 Test 2 5 07/15/2013 150
750
===========================================
Total 28
6700
============================================
Here EmpID =2 don't have rate in the cost table on july month should be pick from last entry from cost table.Hi Gopal,
According to your description, it seems that the output needn’t include the row when EmpID=2. Because the DateTime for it in Table1 doesn’t included between FromDate column and ToDate column. After testing the issue in my environment, we can refer to the
query like below to achieve your requirement:
SELECT time.*,cost.EmpID,cost.Rate,(time.HoursSpend * cost.Rate)as [Total (HoursSond x Rate)]
FROM [Time sheet] as time
INNER JOIN
[cost table]as cost
ON time.EmpID = cost.EmpID
AND time.DateTime BETWEEN cost.FromDate AND cost.ToDate
If there are any other questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
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, 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. -
Outer join two tables with query search record attached to both tables
When I create a query with two tables that have query search records attached with outer join, PS seems to do a natural join (cartesian). We are on PT8.48.
Is there a workaround for this issue. I do not want to remove query search record on either of the tables.
I am trying to create an Emergency contact report. I am using two tables PS_EMPLOYEES and PS_EMERGENCY_CNTCT. Here is the sql PeopleSoft query generated when I did Left outer Join.
Query SQL:
SELECT A.EMPLID, A.NAME, A.ADDRESS1, A.CITY, B.PRIMARY_CONTACT, B.ADDRESS1, B.CITY, B.STATE, B.POSTAL, B.RELATIONSHIP, A.DEPTID, A.JOBCODE, A.COMPANY, A.EMPL_TYPE
FROM (PS_EMPLOYEES A LEFT OUTER JOIN PS_EMERGENCY_CNTCT B ON A.EMPLID = B.EMPLID ), PS_EMPLMT_SRCH_QRY A1, PS_PERS_SRCH_QRY B1
WHERE A.EMPLID = A1.EMPLID
AND A.EMPL_RCD = A1.EMPL_RCD
AND A1.OPRID = 'SREESR'
AND (B.EMPLID = B1.EMPLID OR B.EMPLID IS NULL )
AND B1.OPRID = 'PS'
Appreciate any help.I think there are fixes for this issue in later tools releases (Report ID 1544345000). I'm not sure about 8.48, but you might try the workaround documented in
E-QR: Left Outer Joins with Security Records are returning unexpected results [ID 651252.1]
on Oracle Support.
Regards,
Bob -
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 -
Insert the data into two tables at a time.
Hi ,
i have these two tables
create table [dbo].[test1](
[test1_id] [int] identity(1,1) primary key,
[test2_id] [int] not null
create table [dbo].[test2](
[test2_id] [int] identity(1,1) primary key,
[test1_id] [int] not null
alter table [dbo].[test1]
add constraint [fk_test1_test2_id] foreign key([test2_id])
references [dbo].[test2] ([test2_id])
alter table [dbo].[test2] add constraint [fk_test2_test2_id] foreign key([test1_id])
references [dbo].[test1] ([test1_id])
I want to insert the data into two tables in one insert statement. How can i do this using T-SQL ?
Thanks in advance.You can INSERT into both tables within one Transaction but not in one statement. By the way, you would need to alter your dbo.Test1 table to allow null for first INSERT test2_id column
See sample code below:
CREATE TABLE #test1(test1_ID INT IDENTITY(1,1),test2_id INT NULL)
CREATE TABLE #test2(test2_ID INT IDENTITY(1,1),test1_ID INT)
DECLARE @Test1dentity INT
DECLARE @Test2dentity INT
BEGIN TRAN
-- Insert NULL as test2_ID value is unknown
INSERT INTO #test1(test2_ID)
SELECT NULL;
-- get inserted identity value
SET @Test1dentity = SCOPE_IDENTITY();
INSERT INTO #test2(test1_ID)
SELECT @Test1dentity;
-- get inserted identity value
SET @Test2dentity = SCOPE_IDENTITY();
-- Update test1 table
UPDATE #test1
SET test2_ID = @Test2dentity
WHERE test1_ID = @Test1dentity;
COMMIT
SELECT * FROM #test1;
SELECT * FROM #test2;
-- Drop temp tables
IF OBJECT_ID('tempdb..#test1') IS NOT NULL
BEGIN
DROP TABLE #test1
END
IF OBJECT_ID('tempdb..#test2') IS NOT NULL
BEGIN
DROP TABLE #test2
END
web: www.ronnierahman.com -
Can't write right sql query by two tables
Hello
Everyone,
I am trying to get a sql query by two tables,
table:container
<pre class="jive-pre">
from_dest_id number
ship_from_desc varchar
to_dest_id number
</pre>
table: label_fromat (changeless)
<pre class="jive-pre">
SORT_ORDER number
PREFIX varchar2
VARIABLE_NAME varchar2
SUFFIX varchar2
LF_COMMENT varchar2
</pre>
the sql which i need is
a. table CONTAINER 's each column should have LABLE_FORMAT 's PREFIX before and SUFFIX back ,and these columns is connected
example : the query output should be like this :
<pre class="jive-pre">
PREFIX||from_dest_id||SUFFIX ||PREFIX||ship_from_desc||SUFFIX ||PREFIX|| to_dest_id||SUFFIX
</pre>
every PREFIX and SUFFIX are come from LABEL_FORMAT's column VARIABLE_NAME (they are different)
column SORT_ORDER decide the sequence, for the example above: Column from_dest_id order is 1, ship_from_desc is 2,to_dest_id is 3
b. table LABEL_FORMAT's column VARIABLE_NAME have values ('from_dest_id','ship_from_desc','to_dest_id')
If table CONTAINER only have one record i can do it myself,
But actually it is more than one record,I do not know how to do
May be this should be used PL/SQL,or a Function ,Cursor ,Procedure
I am not good at these
Any tips will be very helpful for me
Thanks
SavenHi, Saven,
Presenting data from multiple rows as a single string is called String Aggregation . This page:
http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
shows many ways to do it, suited to different requirements, and different versions of Oracle.
In Oracle 10 (and up) you can do this:
SELECT REPLACE ( SYS_CONNECT_BY_PATH ( f.prefix || ' '
|| CASE f.variable_name
WHEN 'FROM_DEST_ID'
THEN from_dest_id
WHEN 'SHIP_FROM_DESC'
THEN ship_from_desc
WHEN 'TO_DEST_ID'
THEN to_dest_id
END
|| ' '
|| f.suffix
, '~?'
, '~?'
) AS output_txt
FROM container c
CROSS JOIN label_format f
WHERE CONNECT_BY_ISLEAF = 1
START WITH f.sort_order = 1
CONNECT BY f.sort_order = PRIOR f.sort_order + 1
AND c.from_dest_id = PRIOR c.from_dest_id
saven wrote:If table CONTAINER only have one record i can do it myself,
But actually it is more than one record,I do not know how to do In that case, why did you post an example that only has one row in container?
The query above assumes
something in container (I used from_dest_id in the example above) is unique,
you don't mind having a space after prefix and before from_dest_id,
you want one row of output for every row in container, and
you can identify some string ('~?' in the example above) that never occurs in the concatenated data. -
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,
DirkHi,
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] -
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.
Maybe you are looking for
-
I've decided to buy 4s, but after talking with some people, I discouraged. I think it is not necessary to upgrade because it doesn't have any special feature for a 15 years old boy. Any one upgraded? Any Idea? Help me please? In case i want to buy it
-
Group By condition in result package
Hi Experts, I have a requirement, where I need to write a logic to populate few key figures of a Cube. Logic is based on Group by condition on 3 fields of the result package. (Need to implement in end routine) Is there any way that I can do a select
-
How to colour cells in table ui element
hi, i have to colour the cells in table ui element with diiferent colour according to the condition... kindly suggest the same . any help will be appreciated. thanks and regards, sahai.s
-
My iPhone 4 (CDMA) Wont Upgrade To iOS 5,
I Get Error (3002) When Upgrading I Get Error (3194) When Trying To Restore It Then Upgrading
-
Can't pinch to minimize apps anymore
When I try to pinch an app to minimize it nothing happens. I have to use the home button to navigate between apps. Why would this stop working?