return results as a query of single line sup.
HelloIt's my current query
WITH car as (SELECT 'FORD' make FROM DUAL UNION SELECT 'TOYOTA' make FROM DUAL UNION SELECT'BMW' make FROM DUAL UNION SELECT 'FIAT' make FROM DUAL)
SELECT * FROM CAR
Which is: -.BMW
FIAT
FORD
TOYOTA
However, the actual output required: -.'BMW, FIAT, FORD, TOYOTA'
that is, all turned around on a single line.Any ideas on how to do this?
Thanks in advance!
David
Or
SQL> ed
Wrote file afiedt.buf
1 WITH car as (SELECT 'FORD' make FROM DUAL UNION
2 SELECT 'TOYOTA' make FROM DUAL UNION
3 SELECT'BMW' make FROM DUAL UNION
4 SELECT 'FIAT' make FROM DUAL)
5 SELECT ltrim(sys_connect_by_path(make,','),',') as makes
6 FROM (select make, row_number() over (order by make) rn from CAR)
7 where connect_by_isleaf = 1
8 connect by rn = prior rn + 1
9* start with rn = 1
SQL> /
MAKES
------------------------------------------------------------------------
BMW,FIAT,FORD,TOYOTA
SQL>
Tags: Database
Similar Questions
-
How do I rotate my result of a query from a line to a SQL column?
Hey, guys:
Is it possible that I can rotate my result of a query from a line to a SQL column?
It's a certain type of pivot example
Instead of
DEPTNO DNAME
-------- ---------------
10 accounting
It would be
DEPTNO: 10
DNOM: ACCOUNTINGHello
When you have N columns to rank 1 and you want to display in the form of 1 column on several lines, it's called Unpivoting . Here's a way to do it:
WITH cntr AS ( SELECT LEVEL AS n FROM dual CONNECT BY LEVEL <= 2 -- number of columns to be unpivoted ) SELECT CASE c.n WHEN 1 THEN 'DEPTNO' WHEN 2 THEN 'DNAME' END || ':' AS label , CASE c.n WHEN 1 THEN TO_CHAR (d.deptno) WHEN 2 THEN d.dname END || ':' AS val , d.deptno -- PK, if needed FROM cntr c CROSS JOIN scott.dept d WHERE deptno = 10 -- or whatever ;
The query above works in Oracle 9.1 or more.
From Oracle 11.1, you can also use the SELECT... Function of the UNPIVOT operator. -
replace the carriage return, displaying the output on a single line
[oracle@ccoshs02xvdbs01 ~] $ sqlplus deploy_ctl/deploy_ctl
SQL * more: Production version 11.2.0.1.0 on Mon Mar 28 10:21:05 2011
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production
With partitioning, OLAP, Data Mining and Real Application Testing options
Modified session.
10:21:05 SQL > insert into test (b) values ("this is stop
10:21:38 2 space are now ');
1 line of creation.
10:21:47 SQL > commit;
Validation complete.
10:21:49 SQL > select test b where b as "this % ';
B
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It is stop
space wear now
10:22:07 SQL > select replace (b, chr (13)) of the test where b as "this % ';
REPLACE (B, CHR (13))
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It is stop
space wear now
10:22:56 SQL > select replace (b, chr (13), chr (32)) test fix where b as "this % ';
DIFFICULTY
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It is stop
space wear now
10:24:19 SQL >
I would lilke to display on the same line.
Thank you.Hello
SQL> select translate(b,chr(13)||chr(10),chr(32)) fix from test where b like 'this%'; FIX ---------------------------------------------------------------------------------------------------- this is stopspace now carry on SQL> select translate(b,chr(13)||chr(10),' ') fix from test where b like 'this%'; FIX ---------------------------------------------------------------------------------------------------- this is stopspace now carry on
-Pavan Kumar N
-
convert the column values to a single line...
I have to return the column values to a single line separated by commas.
If the nulls in the column just ignore without a comma.
Here is one for example. There are three values and two NULL values in the table
Help, pleaseSQL> select ID from temp_fa; ID ----- 1 2 3 5 6 rows selected. I am expecting an output as 1,2,3,5
There is always more than one title in the Oracle world ;)
You can use the TRIM, for example (same configuration as your example):hoek&XE> create table t as select level col from dual connect by level <= 6; Tabel is aangemaakt. hoek&XE> update t set col = null where col in (1,3,5); 3 rijen zijn bijgewerkt. hoek&XE> select * from t; COL ---------- 2 4 6 6 rijen zijn geselecteerd. hoek&XE> select ltrim(sys_connect_by_path(col, ','), ',') output 2 from ( select col 3 , row_number() over (order by col) rn 4 from t 5 ) 6 where connect_by_isleaf=1 7 start with rn=1 8 connect by rn = prior rn+1; OUTPUT ------------------------------------------------------------------------------------------------------------- 2,4,6,,, 1 rij is geselecteerd. hoek&XE> select trim ( both ',' from sys_connect_by_path(col, ',')) output 2 from ( select col 3 , row_number() over (order by col) rn 4 from t 5 ) 6 where connect_by_isleaf=1 7 start with rn=1 8 connect by rn = prior rn+1; OUTPUT ------------------------------------------------------------------------------------------------------------- 2,4,6 1 rij is geselecteerd.
-
join in the query with the result of a single line
Hi all
I have two tables
I insert the following recordscreate table item(item_id number primary key, item_desc varchar2(200)); create table item_properties(item_id number references item(item_id), property_name varchar2(20), property_value varchar2(100));
now I want a query that produces the following resultsinsert into items values(1,'CPU'); insert into item_properties values(1,'RAM','2gb'); insert into item_properties values(1,'PROCESSOR','2ghz'); insert into item_properties values(1,'HARDDISK','2ghz'); commit;
How to generate this result?item_id RAM PROCESSOR HARDDISK 1 2gb 2ghz 2TB
I create a query, but it generates several lines, instead I need a single line like above.
Kind thanks.select i.item_id,p.property_value from items i , item_properties p where i.item_id=p.item_id and i.item_id=1;
Published by: Maahjoor on May 7, 2013 12:22 AMselect i.item_id, max(decode(p.property_name,'RAM',p.property_value)) ram, max(decode(p.property_name,'PROCESSOR',p.property_value)) processor, max(decode(p.property_name,'HARDDISK',p.property_value)) hd from items i , item_properties p where i.item_id=p.item_id and i.item_id=1 group by i.item_id;
Or pivot to 11g
with details as ( select i.item_id,p.property_name,p.property_value from item i , item_properties p where i.item_id=p.item_id and i.item_id=1 ) select * from details pivot ( max(property_value) for property_name in ('RAM','PROCESSOR','HARDISK') );
Published by: JAC on May 7, 2013 13:04
-
Hello!
I have a simple object type and a proecdure in which I am trying to use it to insert into another table
-object
CREATE ORREPLACETYPEmt_mtg ASOBJECT
(
ACOL NUMBER ,
BCOL NVARCHAR2 (100)
);
CREATE ORREPLACETYPEREF_MTG ASTABLEOFMt_MTG ;
-same structure as the use of sampletbl target table in the cursor query
create table tbl_MT_MTG
(
ACOL NUMBER ,
BCOL NVARCHAR2 (100)
);
-procedure
CREATE ORREPLACEINTERIORTEST_PROCEDURE1
AS
ref_cur sys_refcursor ;
REFR ref_mtg ;
BEGIN
OPEN ref_cur FOR
Select acol,
BCOL
DE sampletbl où rownum<10;
Fetch ref_cur in bulk collectintorefr;
Insert intotbl_MT_MTG(acol,bcol)selectacol,bcol fromtable(refr);
commit;
CLOSE Ref_cur;
END;
/
When I run this procedure fails with
ORA-06504: PL/SQL: return variables of the game results or the query types do not match
ORA-06512: at "TEST_PROCEDURE1", line 10
ORA-06512: at line 2
Any help on this please...
Thanks to an OLD POST below
so perfect helped me! Thank you
Tubby
After 5 years of more :-)
How to store refcursor in collection How to store refcursor in collection
-
create table ab (a number, b varchar2 (20));
Insert into ab
Select rownum, rownum. "" sample "
of the double
connect by level < = 10
create table bc (a number, b varchar2 (20));
Insert into BC.
Select rownum + 1, rownum + 1 | "" sample "
of the double
connect by level < = 10
Select * AB
where b in (select b BC where b = "2sample")
This query will return me 1 row, but there are cases where the value of the parameter b is null
and that it should return all rows in the table
as
Select * AB
where b in (select b BC where b = "2sample")
which return specific values, but I want to change in a way when the inner query returns nothing then outer query should return all the lines and works as
Select * AB;
Is it possible to put in a single query
Hello
You seem to ask for different things. You want all the lines AB when
- The parameter ("2sample' in the example) is NULL, or when
- There is no corresponding row in the 2 tables (which could happen even if the parameter is not NULL)
?
Assuming you want the option 2, here's one way:
WITH got_rnk AS
(
SELECT ab.*
DENSE_RANK () (ORDER IN CASE
WHEN b (IN)
SELECT b
BC.
WHERE b = "2sample" - parameter
)
THEN "A".
OF ANOTHER 'B '.
END
) AS rnk
AB
)
SELECT a, b
OF got_rnk
WHERE rnk = 1
;
This does not assume b is unique in each table.
Thanks for posting the CREATE TABLE and INSERT statements; It is very useful.
-
Search for value in the subset of data and returns a single line.
Hello
I wonder if you can help me with a query.
I want to return a single line of data below
where the Rno = 1 but also include a column to indicate a code of T555 tran was found in the subset (based on customer no)
I used an analytic function (row_number) to enter the data in the following order and wonder if there is something similar for helping me understand the transaction code.
Customer without Tran Code TranDate NWR
1 T345 01/01/2001 1
1 B455 01/01/1999 2
1 T555 01/01/1998 3
1 R433 01/01/1997 4
1 "T543" 01/01/1996 5
Thank you
SQL> with t 2 as 3 ( 4 select 1 client_no, 'T345' tran_code, to_date('01/01/2001', 'dd/mm/yyyy') trandate, 1 rno 5 from dual union all 6 select 1 client_no, 'B455' tran_code, to_date('01/01/1999', 'dd/mm/yyyy') trandate, 2 rno 7 from dual union all 8 select 1 client_no, 'T555' tran_code, to_date('01/01/1998', 'dd/mm/yyyy') trandate, 3 rno 9 from dual union all 10 select 1 client_no, 'R433' tran_code, to_date('01/01/1997', 'dd/mm/yyyy') trandate, 4 rno 11 from dual union all 12 select 1 client_no, 'T543' tran_code, to_date('01/01/1996', 'dd/mm/yyyy') trandate, 5 rno 13 from dual 14 ) 15 select client_no 16 , tran_code 17 , trandate 18 , rno 19 , case when t555_cnt > 0 then 'YES' else 'NO' end t555 20 from ( 21 select t.* 22 , count(decode(tran_code, 'T555', 1)) over() t555_cnt 23 from t 24 where client_no = 1 25 ) 26 where rno = 1; CLIENT_NO TRAN TRANDATE RNO T55 ---------- ---- --------- ---------- --- 1 T345 01-JAN-01 1 YES SQL>
-
single-line subquery returns moret one line han
UPDATE T74
SET LORRYOWNERNAME = 'BELLA. "
OWNERFATHERNAME = "GHANIMIYA"
AND
(SELECT T74 INVOICEDATE, T74A WHERE T74.) REFERENCENO = T74A. REFERENCENO AND T74. "LORRYNO ="KA05AA4916"AND INVOICEDATE > = February 25, 2011")= February 25, 2011 "
my inner query returns 5 rows ihave to update only the data that are greater than 25 February 2011"
not below when I use > = any it updates all rows, if I removed everything he is in error:
single-line subquery returns moret one line han
could you please correct the query
Published by: 848525 on Sep 7, 2011 12:18 AMTry
UPDATE T74 SET LORRYOWNERNAME='BASHA', OWNERFATHERNAME='GHANIMIYA', PANNO='AGNPD4113G' WHERE LORRYNO='KA05AA4916' AND exists (SELECT INVOICEDATE FROM T74A WHERE T74.REFERENCENO=T74A.REFERENCENO AND INVOICEDATE>='25-FEB-2011')
do not duplicate the post please
Published by: Alexandr Sep 6, 2011 23:52
-
Is there a way to combine their results of the query in a single function
I have a bunch of functions that return a query:
< cffunction = access "getDeals" name = "remote" returntype = "query" >
< cffunction = access "getDeals2" name = "remote" returntype = "query" >
is there a way to combine their results of the query in a single function that returns a structure of different results?
Uh, it is probably more suited to a table - effectively turning it into a list of queries. Then you loop the array and each output.
I use a similar approach in the creation of the system of the user messages. Most of my functions that are called do their part, then at the end call another function, I called 'messages '. This function takes all what my function sends and adds it to a message board.
Keeps me out of a long list of messages - because I could call several functions at the same time. If I have just the output of a message of each function, each would crush the other. So, I'd get one message at a time.
Immediately after that he was released, I will destroy it then the message table so that messages are not kept inside.
Mikey.
-
Compare the result of a query with a number and return a message
Hello
I have the following query in oracle 9i:
SELECT COUNT (*)
OF hourly_files
WHERE date_received = TO_DATE ((SELECT TO_CHAR (SYSDATE - INTERVAL '1' DAY, 'DDMMYYYY')
(THE DOUBLE), 'DDMMYYYY');
This will produce a number of lines required
I need to compare the number of output with another number hardcoded (threshold) and print an appropriate example message
If the result of the query above is 18000 and the number of threshold is fixed at 20000, then output a meesage:
Number of files received less than 2000
Any help will be very appreciated!
Thank you.SQL> ed Wrote file afiedt.buf 1 SELECT CASE WHEN COUNT(*) >5 THEN 'Number is > than 5' 2 WHEN COUNT(*) <1 THEN 'Its less than 1' 3 ELSE 'Its in between' 4 END 5 FROM emp 6* WHERE deptno=20 SQL> / CASEWHENCOUNT(*)>5 ------------------ Its in between SQL> SELECT COUNT(*) FROM emp 2 WHERE deptno=10; COUNT(*) ---------- 3
-
I need to return the result of a query on a stored procedure
I need to return the result of a query to a stored procedure, I mean when I run a stored procedure it returns a result set in a select statement.
Best regards...Hello.
Do you really want a stored procedure for this?
Why not just a script that contains the query?Assuming that you don't really want a stored procedure, you'll have to decide what to do with the results.
An option is a slider.For example, you can write a procedure of this type to hold the query:
CREATE OR REPLACE PROCEDURE USP_TEST ( out_cursor OUT SYS_REFCURSOR ) IS BEGIN OPEN out_cursor FOR SELECT * FROM scott.emp; END USP_TEST; / SHOW ERRORS
You can move the cursor to another procedure for handling.
You could test this in SQL * more by creating a variable blond:
VARIABLE usp_test_cursor REFCURSOR; EXEC usp_test (:usp_test_cursor); PRINT :usp_test_cursor
-
Help returns the array of query results
Hi all... Pretty new to this so please forgive my ignorance. Running Oracle 11 g, trying to return results as in the example I give below. I don't know how the Sport field will have unique values, and I don't know how many unique values the year field is seen in the data. Does anyone have recommendations/suggestions?
The structure of the table is:
Year of sport
Baseball 10-11
Football 10-11
Football 11-12
Football 11-12
Football 11-12
Baseball 11-12
The result should be sports in each year and should be grouped by year but shown as columns so:
(GROUP OF the year)
1, baseball 1, Football
2, 1, Football Soccer 1, Baseball
I hope that makes sense. Thanks for any help!
... KaelonDoes anyone have recommendations/suggestions?
Maybe something like:
SQL> with t as ( select 'Baseball' sport, '10-11' year from dual union all select 'Football', '10-11' from dual union all select 'Football', '11-12' from dual union all select 'Football', '11-12' from dual union all select 'Soccer', '11-12' from dual union all select 'Baseball', '11-12' from dual ) -- -- select year, listagg(cnt || ', ' || sport, ' ') within group (order by cnt desc) sports from ( select count(*) cnt, sport, year from t group by sport, year) group by year / YEAR SPORTS ------- ---------------------------------------- 10-11 1, Baseball 1, Football 11-12 2, Football 1, Baseball 1, Soccer 2 rows selected.
?
-
Multi lines convert to a single line
Dear Group members
I want to convert the queries that return multiple lines in a single line. Scanario is provided for in
This query gives multi lines, but I would like to convert it into a line like thisselect * from inout v where v.empcode = '00047' and v. dated = '27-dec-09' EMPCODE DATED IN_TIME NET_TIME ATT_STATUS VAL 00047 27/12/2009 0 0 G 1 00047 27/12/2009 0 0 R 1 00047 27/12/2009 7.44 483 P 1
If the employee came the day that has three traffic status ('G', 'R', 'P'), then it should show in_time, net_time situation and their participation should be 'R '.EMPCODE DATED IN_TIME NET_TIME ATT_STATUS VAL 00047 27/12/2009 7.44 483 R 1
In this example there are two rows. I need the line that has in_time > 0, which is the 2nd bet replaces att_status 'P' for 'R '.
Suppose that there are the 25/12/2009 and employee came that day then it shows the following result.EMPCODE DATED IN_TIME NET_TIME ATT_STATUS VAL 00047 25/12/2009 0 0 R 1 00047 25/12/2009 7.59 476 P 1
It presents 'R' Att_status coloums instead of 'P '.EMPCODE DATED IN_TIME NET_TIME ATT_STATUS VAL 00047 25/12/2009 7.59 476 R 1
>
Note that
'G' = blew in gusts
'R' = rest
"P" = present
>
Test case is provided for in
Thanks and greetingsCREATE TABLE INOUT ( EMPCODE VARCHAR2(10), DATED DATE, IN_TIME NUMBER(10,2), NET_TIME NUMBER(10,2), ATT_STATUS VARCHAR2(5), VAL NUMBER(3) ) insert into inout(EMPCODE, DATED ,IN_TIME , NET_TIME, ATT_STATUS, VAL ) values ('00047','25-DEC-2009', 0 , 0 , 'R' , 1); insert into inout(EMPCODE, DATED ,IN_TIME , NET_TIME, ATT_STATUS, VAL ) values ('00047','25-DEC-2009', 7.59 , 476 , 'P' , 1); insert into inout(EMPCODE, DATED ,IN_TIME , NET_TIME, ATT_STATUS, VAL ) values ('00047','27-DEC-2009', 0 , 0 , 'G' , 1); insert into inout(EMPCODE, DATED ,IN_TIME , NET_TIME, ATT_STATUS, VAL ) values ('00047','27-DEC-2009', 0 , 0 , 'R' , 1); insert into inout(EMPCODE, DATED ,IN_TIME , NET_TIME, ATT_STATUS, VAL ) values ('00047','27-DEC-2009', 7.44 , 483 , 'P' , 1);
select distinct empcode,dated, case when cnt>0 then max(in_time) over (partition by empcode,dated) else in_time end in_time, case when cnt>0 then max(net_time) over (partition by empcode,dated) else net_time end net_time, case when cnt >0 then case when status in ('G','P','R') then 'R' when status in ('HCP','1/2') THEN 'HCP' end else status end status from ( select v.empcode,v.dated,v.in_time,v.net_time,(count(*) over (partition by v.empcode,v.dated)) cnt,att_status status from inout v where v.empcode = '00047')
Published by: Khaled Aradhye, January 20, 2010 02:23
-
Reading single line of the text file
I use the following code to read a .txt file
private String readTextFile (String fName) {}
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {}
fconn = (FileConnection) Connector.open (fName, Connector.READ);
is = fconn.openDataInputStream ();
Byte [] = IOUtilities.streamToBytes (is) data;
result = new String (data);
} catch (IOException e) {}
System.out.println (e.getMessage ());
} {Finally
try {}
If (null! = is)is. Close();
If (null! = fconn)
fconn. Close();
} catch (IOException e) {}
System.out.println (e.getMessage ());
}
}
return the result;
}I want to print a single line instead of the file around so I read that I need to store the contents of the file into an array and then get the index of the line. Problem is that I couldn't find any information on how to do it. Can someone give me a shot. Thanks in advance.
This isn't a problem with encoding. You just need to do a little extra accounting and adjust start and end indices to remove these characters. To treat the "\r\n" of line termination style, you can maintain a flag (initially false) to indicate that the previous line ended with a \r; If a line begins with \n, it should be treated as the end of a line (empty) if the flag is true (in which case it should be ignored).
If you want to extract all lines (not just the first), then it may be easier to do this accounting while avoiding a separate function. Something like this (untested):
// read the file into a byte array 'data'; then: Vector lines = new Vector(); boolean endCR = false; int start = 0; for (i = start; i < data.length; ++i) { if (data[i] = 0xD) { lines.addElement(new String(data, start, i - start)); endCR = true; start = i + 1; } else if (data[i] = 0xA) { if (endCR && i == start) { start++; endCR = false; } else { lines.addElement(new String(data, start, i - start)); start = i + 1; } endCR = false; } } String[] results = new String[lines.size()]; lines.copyInto(results);
Maybe you are looking for
-
Access the 4th generation Time Capsule data remotely in Mac and PC
Hi all I intend to log in case Apple support, but my Service and Support coverage is already expired > _ <. Apple PC does not support mobile devices like IOS or android, so in this top, I'll focus on Mac and PC. Appreciate if you guys could give me a
-
Make a right-click opens menu long, I tried a lot of bugs found in the forums
Under Firefox 33.0 puppy 5.7.1 Linux on a desktop PC. Make a right-click opens menu long, exactly as described in support.mozilla.org/en-US/questions/956675 I have disabled all extensions and plugins, I tried safe mode, reset Firefox. I uninstalled F
-
33.1 incompatible Firefox with Symantec Endpoint Firewall
This morning, I received a message from Firefox indicating that a new version (33.1) Firefox is available - so I took the auto-update feature. Since the update, I found that Firefox can no longer connect to the internet. Suspect a firewall issue, I d
-
Firefox does not add the file on the file extension registration
When I ask to save a file, the Save dialog for Firefox removes the file extension (for example, 'doc', 'pdf', "ofx") of file name, and I have to manually add it back. Earlier versions don't behave this way.
-
When I do .net applications or install heavy softwares such as .net or oracle 9i my becomes base system overheating and shutdown.Please tell me the solution