Convert Oracle SQL query to single column output
Hello All,
I need to build the query to have multiple columns in a single column with multiple rows.
select a.customer_trx_id,a.previous_customer_trx_id
from ra_customer_trx_all a
where a.customer_trx_id = :customer_trx_id
here, a.customer_trx_id and a.previous_customer_trx_id are in two columns. I need to bring them into a single column.
Say: the above output is
a.customer_trx_id a.previous_customer_trx_id
123456 87654
Need to have single column
As
123456
87654
Please do the needful.
Thanks,
Abdul
Hi,
Post your question in [SQL and PL/SQL|http://forums.oracle.com/forums/forum.jspa?forumID=75] forum, you would probably get a better/faster response.
Regards,
Hussein
Similar Messages
-
Convert Oracle SQL Query to SQLite Query
Hi,
I am new to SQLite and have requirement to write quries in SQLite format. As table structure is same in both the Oracle and SQLite database i have created queries in Oracle SQL. Now wanted to know is there any tool or package available which will convert my simple Oracle queries into SQLite format???
Please guide..
Regards,
Priyankawith tree as (
select
level lev
,id
,parent_id
,sys_connect_by_path(id, '-') p
from test_temp
connect by
parent_id = prior id
start with parent_id is null
select
from tree t1
where exists (
select 1
from tree t2
where substr(t2.p, 1, length(t1.p)) = t1.p
and regexp_count(t2.p, '-') = regexp_count(t1.p, '-') + 2
-- if regexp_count is not available in your version or performs not well, below is an alternative
-- and length(t2.p) - length(replace(t2.p, '-')) = length(t1.p) - length(replace(t1.p, '-')) + 2
LEV
ID
PARENT_ID
P
1
1
-1
2
3
1
-1-3
2
5
1
-1-5
1
2
-2
2
6
2
-2-6
3
14
6
-2-6-14 -
Converting Oracle XML Query Result in Java String by using XSU
Hi,
I have a problem by converting Oracle XML Query Result in Java
String by using XSU. I use XSU for Java.
For example:
String datum=new OracleXMLQuery(conn,"Select max(ps.datum) from
preise ps where match='"+args[0]+"'");
String datum1=datum;
I become the following error:
Prototyp.java:47: Incompatible type for declaration. Can't
convert oracle.xml.sql.query.OracleXMLQuery to java.lang.String.
Can somebody tell me a method() for converting to solve my
problem??????
ThanksHmmm.. Pretty basic just look at the example:
OracleXMLQuery qry = new OracleXMLQuery(conn,"Select max(ps.datum) from preise ps where match='"+args[0]+"'");
String xmlString = qry.getXMLString();
Hi,
I have a problem by converting Oracle XML Query Result in Java
String by using XSU. I use XSU for Java.
For example:
String datum=new OracleXMLQuery(conn,"Select max(ps.datum) from
preise ps where match='"+args[0]+"'");
String datum1=datum;
I become the following error:
Prototyp.java:47: Incompatible type for declaration. Can't
convert oracle.xml.sql.query.OracleXMLQuery to java.lang.String.
Can somebody tell me a method() for converting to solve my
problem??????
Thanks -
Result of an SQL query as a Column name of another query
Hi Friends,
Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
For eg :
I have a table where is store numbers;
select col1 from table1 where col1='5';
and i have another table where .. this value of col is a column name..
select ( select col1 from table1 where col1='5') from table2;
Thanks in advance.Hi,
ORAFLEX wrote:
Hi Friends,
Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
For eg :
I have a table where is store numbers;
select col1 from table1 where col1='5';
and i have another table where .. this value of col is a column name..
select ( select col1 from table1 where col1='5') from table2;
Thanks in advance.Do you really mean that?
select col1 from table1 where col1='5';That query will always return either '5' or nothing. Whatever you're trying to accomplish with that, you can do with an EXISTS query.
Perhaps you meant to reference two different columns in that query:
select col1 from table1 where col2='5';In that case, sorry, no, you can't do that without resorting to dynamic SQL.
If the same column is used throughout the query (but could change every time you run the query), then the dynamic SQL might be pretty easy. In SQL*Plus, for example, you could use substitution variables, defined in another query at run-time.
If there are only a few possible values that the sub-query could possibly return, and you know what they all are, then you can fake a dynamic query like this:
SELECT CASE ( SELECT col1
FROM table1
WHERE col2 = '5'
WHEN 'BONUS' THEN bonus
WHEN 'COMM' THEN comm
WHEN 'SAL' THEN sal
END AS col1
FROM table2
;Sorry to give such a vague answer, but it's the best I can do with the information I have.
It would help if you posted a little sample data (CREATE TABLE and INSERT statments for both tables), and the results you want to get from that data. If you want to pass a parameter to the query, give the results you want for a couple of different parameters. -
Hi
On a report there is a function "Convert to SQL Query", I have tryed the function and get the SQL statment for the report, is it the only thing to get in "text" instead of the more "graphic" way to show it?
Best Regrads
Jesper VelsJesper,
is it the only thing to get in "text" This is correct.The "Convert to SQL Query" link is available for report that are built using the Wizard. Selecting this will give you the underlying SQL Query that you can edit/modify if required.
more "graphic" way to show itWhat do you mean by this? Please elaborate?
-Amitabh -
SQL Query to convert multiple rows to a single row output
I have the following sql query;
SELECT B.ClientID,
B.BillNo, B.[BillDate],
B.CurrencyCode,
B.BAmount
FROM tblCompanyDetails CD
CROSS
APPLY (
SELECT TOP 5 B1.ClientID,
B1.[BillNumber],
B1.[BillDate], B1.[CurrencyCode] AS CCY,
B1.[BillAmount]
FROM tblBills B1
WHERE ROUND(B1.[BillAmount],2)<>0
AND B1.ClientID=CD.CompanyID
AS B
WHERE CD.ContactId=235405
ORDER
BY B.ClientID,B.BillNo
DESC
This gives me the following row output;
ClientID
BillNo
BillDate
CurrencyCode
BAmount
94466
425616
2015-04-07
GBP
5000
94466
424517
2015-01-15
GBP
6000
94466
424455
2014-10-15
GBP
4000
However, I would like the ouput to appear like this;
ClientID
BillNo1
BillDate
CCY
BAmount
BillNo2
BillDate
CCY
BAmount
BillNo3
BillDate
CCY
BAmount
94466
425616
2015-04-07
GBP
5000
424517
2015-01-15
GBP
6000
424455
2014-10-15
GBP
4000I have tried to create a sample based on your question
CREATE TABLE MyTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT
INSERT INTO MyTable
VALUES (94466,425616,'2015-04-07','GBP',5000),
(94466,424517,'2015-01-15','GBP',6000),
(94466,424455,'2014-10-15','GBP',4000)
Then I have used another table to put ranking in that, you can actually tweak your statement to include rank also
CREATE TABLE RowTable
ClientID INT ,
BillNo INT,
BillDate DATE,
CurrencyCode varchar(5),
BAmount INT,
Ranking INT
INSERT INTO RowTable
SELECT * , ROW_NUMBER() OVER (ORDER BY ClientID) as row
FROM MyTable
Once your data is ready then execute below dynamic query
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(col+cast(Ranking as varchar(10)))
from RowTable
cross apply
select 'BillNo', 1 union all
select 'BillDate', 2 union all
select 'CurrencyCode', 3 union all
select 'BAmount' , 4
) c (col, so)
group by col, so, Ranking
order by Ranking, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ClientId,' + @cols + '
from
select clientid, col+cast(Ranking as varchar(10)) col, value
from RowTable
cross apply
SELECT ''BillNo'', cast(BillNo as varchar(20)) union all
SELECT ''BillDate'', cast(BillDate as varchar(20)) union all
SELECT ''CurrencyCode'', CurrencyCode union all
SELECT ''BAmount'', CAST(BAmount as varchar(10))
) c (col, value)
) x
pivot
max(value)
for col in (' + @cols + ')
) p '
execute sp_executesql @query;
Output:
Hope this will help. -
Oracle SQL query for getting specific special characters from a table
Hi all,
This is my table
Table Name- Table1
S.no Name
1 aaaaaaaa
2 a1234sgjghb
3 a@3$%jkhkjn
4 abcd-dfghjik
5 bbvxzckvbzxcv&^%#
6 ashgweqfg/gfjwgefj////
7 sdsaf$([]:'
8 <-fdsjgbdfsg
9 dfgfdgfd"uodf
10 aaaa bbbbz#$
11 cccc dddd-/mnm
The output has to be
S.no Name
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
It has to return "Name" column which is having special characters,whereas some special chars like -, / ," and space are acceptable.
The Oracle query has to print columns having special characters excluding -,/," and space
Can anyone help me to get a SQL query for the above.
Thanks in advance.You can achieve it in multiple ways. Here are few.
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where regexp_like(translate(name,'a-/" ','a'), '[^[:alnum:]]');
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where translate
19 (
20 lower(translate(name,'a-/" ','a'))
21 , '.0123456789abcdefghijklmnopqrstuvwxyz'
22 , '.'
23 ) is not null;
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> -
Handling hierarchy in oracle sql query
I have two tables:
The first contains COMPANY_ID and CATEGORY_ID
The second contains CATEGORY_ID and PARENT_CATEGORY_ID
I need to return a list containing COMPANY_ID and CATEGORY_ID. The entries, however, must come not only from the first table, but also include all the descendants listed in the second table.
So for example (and please read the example carefully) - let’s say in the first table I have one row:
COMPANY_ID | CATEGORY_ID
1000 | 1
In the second table, I have three rows:
CATEGORY_ID | PARENT_CATEGORY_ID
1 | null
2 | 1
3 | 2
4 | null
I want to have the following rows as a result:
COMPANY_ID | CATEGORY_ID
1000 | 1
1000 | 2
1000 | 3
How can I do this in oracle 10 in a single SQL statement ?Hi,
Welcome to the forum!
Whenver you have a question, post your sample data in a form that people can use.
CREATE TABLE and INSERT statements are great:
CREATE TABLE first
( company_id NUMBER (4)
, category_id NUMBER (4)
INSERT INTO first (company_id, category_id) VALUES (1000, 1);CREATE TABLE AS is good, too:
CREATE TABLE second
AS
SELECT 1 AS category_id, NULL AS parent_category_id FROM dual UNION ALL
SELECT 2, 1 FROM dual UNION ALL
SELECT 3, 2 FROM dual UNION ALL
SELECT 4, NULL FROM dual;Without that, people can't test their ideas, and are often unsure about your data.
You should also say what version of Oracle you're using. That's especially important with CONNECT BY queries. Every version since Oracle 7 has had significant improvements in how to do CONNECT BY queries. The query below works in Oracle 10 (and up).
For hierarchies and trees, use CONNECT BY
When you have to join tables and use CONNECT BY, it's usually more efficient to do one in a sub-query, and the other in another query.
In the example below, we're doing the CONNECT BY in sub-query cbq, and the join in the main query.
WITH cbq AS
SELECT category_id
, CONNECT_BY_ROOT category_id AS root_category_id
FROM second
START WITH parent_category_id IS NULL
AND category_id IN ( SELECT category_id -- Maybe; see note below
FROM first
CONNECT BY parent_category_id = PRIOR category_id
SELECT first.company_id
, cbq.category_id
FROM first
JOIN cbq ON first.category_id = cbq.root_category_id
;The 2nd condition in the START WITH clause won't change the results any; any rows that are excluded by that condition would also be excluded by the join condition. There can be a big differenece in performance, however. Try the query both ways, and use the 2nd condition if it helps.
Edited by: Frank Kulash on Feb 3, 2010 3:46 PM -
How to tune a oracle sql query to make it run faster ...
Hi Guys,
I am very new to this tuning of sql queries. It is taking a lot of time to run a query and give the output. I have a tool called TOAD[ Tool for oracle application development ] and i ran a query in that tool. Its showing that the query is taking 7sec to get its output. I donno the exact procedure what to follow in order to tune that query. What are these actually like : explain plan, sqltrace, tkprof and how to use these to tune the query. I want my query to run faster like in msec.
Help Appreciated.
Thankshttp://download-east.oracle.com/docs/cd/B10501_01/server.920/a96533/toc.htm
http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96533/sqltrace.htm#1018 -
Performance of update query for single column vs multiple column
Hi All,
I could not find any answer for this, does it ever matter in terms of performance updating single column versus multiple column in a single update query.
For eg. table consisting of 15 columns, what would be the difference in performance when one column is update in the single query against another update query where 15 columns are updated.
Please do keep in mind my table columns in actually could be around 150+.
Thanks for any information provided.If updated columns aren´t on where clause , then the only impact of 15 columns will be a increase on redo generation, and a possible chainning row.
So since the redo is one of the things that have a large impact, the answer is yes.
The performance will be slower.
Regards
Helio Dias.
http://heliodias.com
OCE SQL, OCP 9i -
Convert T-SQL query to DB2 query
I am trying to convert a T-SQL query to DB2 (v.9.7) query but have no luck.
I have tested following T-SQL query using query tool of SQL Server Management Studio and it works fine as expected.
SELECT CASE WHEN (EXISTS (SELECT NULL FROM "CUSTOMERS" t0) ) THEN 1 ELSE 0 END AS "value"
The query is originally from following C# code:
TestQuery(() => db.Customers.Any());
I try to run the query using query tool of IBM Data Studio 4.1.0.1 Client and get error: SQLCODE=-104, SQLSTATE=42601, DRIVER=4.17.30
How to convert it to DB2 correctly?
Any help would be greatly appreciated! Thanks in advance.
MagnusSo this is not the right place to ask about DB2. What I can say is that if you had been converting to Oracle, you would have needed to add "FROM dual" to your query.
That is, on Oracle you cannot leave out the FROM clause, why they have this funky "dual". Whether you can leave out FROM on DB2, I have no idea, but I raise this is a potential problem.
Erland Sommarskog, SQL Server MVP, [email protected] -
Convert Oracle sql to TimesTen sql please?
Hello,
I have the following statement that is puking in TimesTen. Can anyone help me convert it from Oracle sql to TimesTen sql?
select
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60)),
max(metric),avg(metric),min(metric)
from fc_data
where creation_date>sysdate-1 and suite_id='web-pac-hk-symantecstore'
group by
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60))
order by
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60))Chris,
Thanks for looking into this. The version of TT is 7.0.1. I'm pretty sure the sysdate - 1 is a problem but there also seems to be another issue. Here are the results. The creation_date column is a date type column.
Table FC_ADMIN.FC_DATA:
Columns:
*DATA_ID NUMBER (38) NOT NULL
SUITE_ID VARCHAR2 (64) INLINE NOT NULL
STATUS VARCHAR2 (16) INLINE
INFO VARCHAR2 (2000) NOT INLINE
DETAILS VARCHAR2 (4000) NOT INLINE
METRIC NUMBER (20,10)
METRIC_UNITS VARCHAR2 (16) INLINE
DURATION_MILLIS NUMBER (38)
CREATION_DATE DATE
CREATOR VARCHAR2 (64) INLINE
*MONTH_OF_YEAR NUMBER (38) NOT NULL
TIMELINE VARCHAR2 (4000) NOT INLINE
select
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60)),
max(metric),avg(metric),min(metric)
from fc_data
where creation_date>sysdate-1 and suite_id='web-pac-hk-symantecstore'
group by
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60))
order by
(trunc(creation_date,'HH24')+(round(to_number(to_char(creation_date,'mi'))/10.,0)*10.)/(24*60))
;1001: Syntax error in SQL statement before or at: "(", character position: 44
select (trunc(creation_date,'HH24')+(round(to_number(to_char(creation...
^
The command failed.
How does one do a sysdate - 1 in TT?
Thanks,
Brian -
Clarification on using function in where clause of oracle sql query
I have an issue in regarding function using where clause of sql query..
We are facing performance issue while executing query, so in what ways to improve the performance of the query which i have posted below.
select col ,case when my_function(parameter)
from tab1 a ,tab2 b,tabl3 c
where a.column1=b.column2
and b.column3 =c.column6
and my_function(parameter)>0
Regards
Dinesh
Edited by: wild fire on May 18, 2012 4:15 PMDinesh,
remind that when you use a function in the where clause it normally will get started for each record in the table.
So your answer is two-fold:
1. make the function only start when needed by adding a function based index on the table (this will make inserts and updates slower)
2. make the function faster by adding the DETERMINISTIC clause if possible. this will make Oracle "cache" the result of the function.
Regards,
Richard
blog: http://blog.warp11.nl
twitter: @rhjmartens
If this question is answered, please mark the thread as closed and assign points where earned.. -
Oracle Sql Query issue Running on Different DB Version
Hello All,
I have come into situation where we are pruning sql queries on different DB version of Oracle and have performance issue. Let me tell you in brief and i really appreciate for your prompt response as its very imperative stuff.
I have a query which is running on a DB of version 7.3.4 and it takes around 30 mins where as the same query when run on 8i it takes 15sec., its a huge difference. I have run the statistics to analyze on 7.3 and its comparatively very high. Question here is, the sql query trys to select data from same schema table and 2 tables from another DB using DB link and 2 other tables from another DB using DB link.So,how can we optimize this stuff and achieve this run as same time as 8i DB in 7.3. Hope i am clear about my question, Eagerly waiting for your replies.
Thanks in Advance.
Message was edited by:
Ram8Difficult to be sure without any more detailed information, but I suspect that O7 is in effect copying the remote tables to local temp space, then joining; 8i is factoring out a better query to send to the remote DBs, which does as much work as possible on the remote DB before shipping remaining rows back to local.
You should be able to use EXPLAIN PLAN to identify what SQL is being shipped to the remote DB, If you can't (and it's been quite a while since I tried DB links or O7) then get the remote DBs to yourself, and set SQL_TRACE on for the remote instances. Execute the query and then examine the remote trace files, And don't forget to turn off the tracing when you're done.
Of course it could just be that the CBO got better,,,
HTH - if not, post your query and plans for the local db, and the remote queries.
Regards Nigel -
Oracle SQL Query from EXCEL 2007 with prompt
Hello,
I have many excel reports where I am pulling information from our Oracle 9 db through Excel using the following method:
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
http://blog.mclaughlinsoftware.com/2009/11/30/sql-query-in-excel-2007/
However, I am having trouble when I try the following query due to the prompt:
SELECT
IM.ITEM_GROUP,
IM.ITEM,
IM.DESCRIPTION
FROM
LAWSON.ITEMMAST IM
WHERE
IM.ITEM = '&ITEM';
Does any one know how I can connect a prompt to an excel cell and then pass the query on to Oracle, or have a PL SQL prompt work from Excel?
Thanks,
BenThe 'prompt' as you call it is a sqlplus feature, so does not belong to the SQL language
You would need to write a stored procedure returning a resultset.
create or replace procedure foo(rc in out sys_refcursor, p_item) as
begin
open rc for
'SELECT
IM.ITEM_GROUP,
IM.ITEM,
IM.DESCRIPTION
FROM
LAWSON.ITEMMAST IM
WHERE
IM.ITEM = '||p_item;
end;
and call that using ODBC or asp.net
Obviously this is profusely documented.
Sybrand Bakker
Senior Oracle DBA
Maybe you are looking for
-
Is it possible to preorder apple watch in the UK?
wondering if it is possible to preorder the apple watch in the uk already?
-
Could I run a jar application use a external class such as db2java.zip (ib
Could I run a jar application use a external resource in a zip format such as db2java.zip (ibmdb2 driver) not a jar format ?
-
Hello, can some body help me to knowe how can I get the table name form a Field. If I Cklick on the Field and then press F2 I can not see the table name but i can see wich UI-Component and view and Attribute used, but I need to knowe the tablename wh
-
I recently bought a new laptop because my old one crashed. The only backup I had for my music was on my ipod. My new computer will not recognize the ipods music and I can't transfer anything. It keeps asking me to sync the ipod to this computer, b
-
I just want to burn some files to disc....
Idealy I would like to put in the compact fash card and have my macbook just go burn it for me, but I would be happy to just right click on a folder (any folder), and hit burn... any way to do this? Thanks!