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
Saven
Hi, 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.
Similar Messages
-
How can I write a SQL statement which checks if a table exists?
How can I write a SQL statement which tells me whether a table exists?
execute an sql query: select * from <tablename>
catch the exception n check whether the erroe code
matches the one that occurs for table doesn't exist
that's itHow is your answer any different from the one given in the first reply?
It isn't.
As WorkForFood says DatabaseMetaData has a bunch of methods for getting information about tables but this is more useful when you don't know the names of any of the tables.. it sounds like you do so I would concur SELECT from table is probably the quickest way to go. If it helps the Xopen error should be either S1000 or 42S01 (I think) but I would try and see if there is a specific vendor code for table not found/not exists error and check for that. -
Help me write a SQL query; urgent
Hi, can somebody please help me write a SQL query.
I have 3 tables each with the same column names (Col1, Col2, Col3). Col1 is PK with Unique Constraint.
I wanted to add values of Col2 and Col3 (from all 3 tables) and put it in a separate table (i.e aggregated) of all values found in Col1.
Does anybody help me please ?
thanks alot.Please don't mark your question as urgent. You've been around here long enough that you should know that it will not get your question anwered any faster, and may just irritate people into not answering at all.
I'm not sure exactly what you want.
Are you saying you want t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3 for the rows that have the same c1 in all three tables?
If so, it would be like this, I think: insert into t4
select t1.c1
, t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3
from t1, t2, t3
where t2.c1 = t1.c1
and t3.c1 = t1.c1If that's not what you want, please clarify.
And next time maybe you should post your SQL question in a SQL forum. -
How to write a SQL query in SAP B1 2007 B with input parameters?
How to write a SQL query in SAP B1 2007 B with input parameters, on execution of which will ask for some input value from the user and the values will be selected from a list such as item list?
The syntax like
SELECT * FROM OITM T0 WHERE T0.ItemCode = '[%0\]'
Thanks,
Gordon -
Can I put a SQL query into a bind variable and then use it to output report
Hi,
Can I put a SQL query into a bind variable and then use it to output report?
I want to create a report and an item "text area" (say P1_TEXT) which can let user to input a SQL query(they are all technical users and knows SQL very well). Then, I use a bind variable (that text area) to store the SQL statement. Then, I add a submit button and I want to use the following to output the report:
select * from (:P1_TEXT);
Do you think it is possible to do that? Any known limitations for APEX in this area?
Thanks a lot,
AngelaYou can, but make sure it's what you really want to do. Make sure you are VERY familiar with SQL Injection. Most people who know what it is, go out of their way to prevent SQL Injection. You're going out of your way to allow it.
You can try using &P1_TEXT. instead of bind variable syntax. Bind variables are one of the best ways to prevent SQL Injection, which is why it's not working for you.
Once again, I strongly urge you to consider the implications of your app, but this suggestion should get it working.
Tyler -
Can I write a SELECT query on maintenance view?
Hi,
Can we write a select query on maint. view? If not what are the views on which we can write the select queries?hi,
you can write select query for maintenance view, database view and projection view but not helpview as it is only intended for f4 help.
reward if helpful
prasanth -
Can anybody provide the SQL query to find the files uploaded in a particular folder?
Hi All,
Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
Any help is greatly appreciated.
With best regards,
NevinNevin,
Be great if we could know the version of Portal. For Rel. 1, here's the query
select id,masterthingid from wwv_things
where siteid = &site
and cornerid = &corner
&site - Content Area id
&corner - Folder id
if you don't know the folder id, use
select id from wwv_corners where siteid = &site
and name = &folder
Hope this helps. I have run into this situation before. Usually, the culprits were
one of the following:
1. Junk Characters in description of item (caused due to Copy-Paste)
2. Special Characters in the File name
Hi All,
Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
Any help is greatly appreciated.
With best regards,
Nevin -
Can u write the following query without using group by clause
select sp.sid, p.pid, p.name from product p, supp_prod sp
where sp.pid= p.pid and
sp.sid = ( select sid from supp_prod group by sid
having count(*) =(select count(*) from product));
thru this, we retrieving all the products delivered by the supplier.
Can you write the following query without using the group by clauseselect sp.sid, p.pid, p.name
from product p, supp_prod sp
where sp.pid= p.pid the above query will still retrieve all the products supplied by the supplier. sub-query is not necessary.
maybe if you can post some sample data and output will help us understand what you want to achieve. -
Q: how can i write PL/SQL block to check prerequisite?
Hello...
I designed an application using sql*plus statment for creating database and developer for creating forms, for on-line registration system for universties,
so I need to teach me how can I write pl/sql block to check the prerequisite, taken courses, and complete hours for the students who wants register the courses via Internet.
thanks alot in advance
kindly send the answers a.s.a.pplease repost this in the SQL & PL/SQL forum
thanks - OTN -
Write the sql query to find largest value in row wise without using great
write the sql query to find largest value in row wise without using
greatest fuction?Another not so good way, considering you want greatest of 4 fields from a single record:
SQL> ed
Wrote file afiedt.buf
1 with t as (Select 100 col1,200 col2,300 col3,400 col4 from dual
2 union select 500,600,700,800 from dual
3 union select 900,1000,1100,1200 from dual
4 union select 1300,1400,1500,1600 from dual
5 union select 1700,1800,1900,2000 from dual
6 union select 2100,2200,2300,2400 from dual
7 union select 2800,2700,2600,2500 from dual
8 union select 2900,3000,3100,3200 from dual)
9 SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) Max_value
10 FROM
11 (SELECT (CASE WHEN col1_col2 > col2_col3 THEN col1_col2 ELSE col2_col3 END) col1,
12 (CASE WHEN col2_col3 > col3_col4 THEN col2_col3 ELSE col3_col4 END) col2,
13 (CASE WHEN col3_col4 > col4_col1 THEN col3_col4 ELSE col4_col1 END) col3
14 FROM
15 (SELECT (CASE WHEN col1 > col2 THEN col1 ELSE col2 END) col1_col2,
16 (CASE WHEN col2 > col3 THEN col2 ELSE col3 END) col2_col3,
17 (CASE WHEN col3 > col4 THEN col3 ELSE col4 END) col3_col4,
18 (CASE WHEN col4 > col1 THEN col4 ELSE col1 END) col4_col1
19* FROM t))
SQL> /
MAX_VALUE
400
800
1200
1600
2000
2400
2800
3200
8 rows selected.
SQL> Edited by: AP on Sep 21, 2010 6:29 AM -
Hi,
I have this problem, How can query a two table?
Table A -> Table B
id table-a_id
name table_b_name
the relationship is one-to-many
How can I get the result?
Hope my question make sense
cheers.
thanks a lot.I bet you have more luck looking for an answer in a SQL forum.
-
Dear Expert,
If I want to make queries for two tables, for example I want to make a query for the list or amount of invoices for a certain group of clients with same master records such as geographic location, then how can I make the queries which involves two tables.
Thank you.
RaymondHi,
Use territories to segregate your Business Partners geographycally and then use following query to view doc total of invoices for business partners belongs to a particular territoy,
SELECT T0.DocTotal FROM OINV T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode INNER JOIN OTER T2 ON T1.Territory = T2.territryID WHERE T2.descript =[%0]
Regards,
/Siddiq -
Finding the Text of SQL Query causing Full Table Scans
Hi,
does anyone have a sql script, that shows the complete sql text of queries that have caused a full table scan?
Please also let me know as to how soon this script needs to be run, in the sense does it work only while the query is running or would it work once it completes (if so is there a valid duration, such as until next restart, etc.)
Your help is appreciated.
Thx,
MayuranFinding the Text of SQL Query Causing Full Table Scan
-
How can i write the trigger for Global Temporary Table
Hi Grus,
How can i write the trigger for Global Temporary Table.
I was created the GTT with trigger using the below script .
CREATE GLOBAL TEMPORARY TABLE GLOBAL_TEMP
EMP_C_NAME VARCHAR2(20 BYTE)
ON COMMIT PRESERVE ROWS;
CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
BEFORE DELETE OR UPDATE OR INSERT
ON GLOBAL_TEMP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
INSERT INTO EMPNAME VALUES (:OLD.EMP_C_NAME);
END;
trigger was create successfully, but the wouldn't insert into to EMPNAME Table..
Please guide whether am correct or not? if not kindly give a correct syntax with example
Thanks in Advance,
Arun M MBEGIN
INSERT INTO EMPNAME VALUES (:OLD.EMP_C_NAME);
END;
you are referencing old value in insert stmt.
BEGIN
INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
END;then run ur application it works fine...
CREATE GLOBAL TEMPORARY TABLE GLOBAL_TEMP
EMP_C_NAME VARCHAR2(20 BYTE)
ON COMMIT PRESERVE ROWS;
CREATE OR REPLACE TRIGGER TRI_GLOBAL_TEMP
BEFORE DELETE OR UPDATE OR INSERT
ON GLOBAL_TEMP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
dbms_output.put_line(:OLD.EMP_C_NAME||'yahoo');
INSERT INTO EMPNAME VALUES (:new.EMP_C_NAME);
dbms_output.put_line(:OLD.EMP_C_NAME);
END;
create table EMPNAME as select * from GLOBAL_TEMP where 1=2
insert into GLOBAL_TEMP values('fgfdgd');
commit;
select * from GLOBAL_TEMP;
select * from EMPNAME;
output:
1 rows inserted
commit succeeded.
EMP_C_NAME
fgfdgd
1 rows selected
EMP_C_NAME
fgfdgd
1 rows selectedgot it Arun
Edited by: OraclePLSQL on Dec 28, 2010 6:07 PM -
How to write a sql query to retrieve data entered in the past 2 weeks
Hi,
I have file names and last accessed date(java.sql.Date format) stored in my database table, I would like to know how I can write a query to get the name of files accessed in the past 2 weeks,I use open sql server at the back end.
Thanks in advance.This has essentially nothing to do with JDBC. JDBC is just an API to execute the SQL language using Java and thus interact with the databases.
Your problem is related to the SQL language, you don't know how to write the SQL language. I suggest you to go through a SQL tutorial (there is one at w3schools.com) and to read the SQL documentation which come along with the database in question. A decent database manfacturer has a website and probably also a discussion forum / mailinglist as well.
I'll give you a hint: you can just use equality operators in SQL like everywhere. For example: "WHERE date < somedate".
Maybe you are looking for
-
Please add "resize image before uploading" and "do...
SOME of us do NOT want to send bothersome, huge images over mobile devices just to share a pointless moment with a friend - or an enemy. Wechat already has this AWESOME feature, so maybe Skype should have one too - as it is so cumbersome to go into a
-
I purchased an iPad 1 (in Australia where I used to live) and 5 months after the warrenty had run out it ceased to turn on. This I hasten to add, was only 1yr 5 months after purchasing it, and I think we all expect a premium product, which we pay a p
-
Nvidia GF 9500M shows up as two cards. How to manage?
My Nvidia GF 9500M shows up as two cards: 9200M at PCI:0:2:0:0 and 9400M at PCI:0:3:0:0. Currently, I only include the 9400M in my xorg.conf (Currently the same as the example on the Dell XPS 13 entry in the wiki) However, the 9200M continues to gen
-
Doing some animation work with a lot of vector elements that I am arranging in photoshop as smart objects. I like to comp my AE stuff in Photoshop, even though I do the detail vector work in Illustrator. Now, when working in AE...the PS composition
-
Call original VO query from entended VO
I have create a extended VO which is having modified query. I want to call the original VO query from extended by using if else condition. if(condition=true) call the extended vo else call the original vo Please suggest how to implement this. Thanks,