SQL - Multiple Fetch in a single column with a comma separator
Hello Experts,Good day to all...
I need your help on the following scenarios. The following query returns all channels titleID. Rather than print them one under the other as a result of the query, I want the output to be in the batch of 25 values.i.e than each line must have 25 values separated by commas. IE if there are 100 titles satisfying the output, then there should be only four lines with and each line with 25 titles in comma separated way.
SELECT DISTINCT title_id
FROM pack_relation
WHERE package_id IN ( SELECT DISTINCT fa.package_id
FROM annotation fa
GROUP BY fa.package_id
HAVING COUNT
(fa.package_id) <100);
I tried with the PL/SQL block; Whereas it is printing all the values permanently :(I have to stop with 25 values and display.
If its possible with SQL block alone. then it would be a great help
DECLARE
v_str VARCHAR2 (32767) := NULL;
CURSOR c1
IS
SELECT DISTINCT title_id
FROM pack_relation
WHERE package_id IN ( SELECT DISTINCT fa.package_id
FROM annotation fa
GROUP BY fa.package_id
HAVING COUNT
(fa.package_id) <100);
BEGIN
FOR i IN c1
LOOP
v_str := v_str || ',' || i.title_id;
END LOOP;
v_str := SUBSTR (v_str, 2);
DBMS_OUTPUT.put_line (v_str);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error-->' || SQLERRM);
END;
Thank you...
You can use CEIL
Code example
SELECT
nt,
LTRIM(MAX(SYS_CONNECT_BY_PATH(val,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concat_val
FROM
(
SELECT
val,
nt,
ROW_NUMBER() OVER (PARTITION BY nt ORDER BY val) AS curr,
ROW_NUMBER() OVER (PARTITION BY nt ORDER BY val) -1 AS prev
FROM
(
SELECT
level AS val,
ceil(rownum/3) as nt /* Grouped in batches of 3 */
FROM
dual
CONNECT BY level <= 10
)
)
GROUP BY
nt
CONNECT BY prev = PRIOR curr
AND nt = PRIOR nt
START WITH curr = 1;
NT CONCAT_VAL
---------- --------------------------------------------------------------------------------
1 1,2,3
2 4,5,6
3 7,8,9
4 10
Your code
SELECT
nt,
LTRIM(MAX(SYS_CONNECT_BY_PATH(title_id,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') AS concat_val
FROM
(
SELECT
title_id,
nt,
ROW_NUMBER () OVER (PARTITion BY nt ORDER BY title_id) AS curr,
ROW_NUMBER() OVER (PARTITION BY nt ORDER BY title_id) -1 AS prev
FROM
(
SELECT
title_id,
ceil(rownum/25) AS nt /* Grouped in batches of 25 */
FROM
pack_relation tdpr
JOIN annotation fa
ON
tdpr.package_id = fa.package_id
GROUP BY
title_id,
fa.package_id
HAVING
COUNT (fa.package_id) < 500
)
)
GROUP BY
nt
CONNECT BY prev = PRIOR curr
AND nt = PRIOR nt
START WITH curr = 1;
Tags: Database
Similar Questions
-
Hi all
I have a requirement where I need to get all the records, for each record in double, I need to get a single column with null or 0.
create table a1
(
Identification number,
VARCHAR2 (100), the point
part varchar2 (100));
Insert into a1
values (1, 'ABC', 'A1');
Insert into a1
values (2, 'DEF', 'A2');
TABLE A
PART ITEM ID
1 ABC A1
1 ABC A1
1 ABC A1
DEF 2 A2
DEF 2 A2
3 DEF A2
O/P
PART ITEM ID
1 ABC A1
1 ABC 0
1 ABC 0
DEF 2 A2
2 DEF 0
3 DEF 0
Thanks in advance.
Thanks for your help FrankKalush...
This one will work.
WITH got_r_num AS
(
SELECT NVL (a1.id, a1.id) as id
NVL (a1.item, a1.item) AS element
NVL (a1.part, a1.part) IN the framework
a1.id AS a_id
ROW_NUMBER () OVER (PARTITION BY a1.id
ORDER BY NULL
) AS r_num
BY the a1
)
SELECT id
element
CASE
WHEN a_id IS NOT NULL
AND r_num = 1
THEN part
ELSE ' 0'
END in the framework
OF got_r_num
;
-
I have a column with two values, separated by a space, in each line. How do I create 2 new columns with the first value in one column and the second value in another column?
Add two new columns after than the original with space separated values column.
Select cell B1 and type (or copy and paste it here) the formula:
= IF (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')
shortcut for this is:
B1 = if (Len (a1) > 0, LEFT (A1, FIND ("", A1) −1), ' ')
C1 = if (Len (a1) > 0, Member SUBSTITUTE (A1, B1 & "", ""), "")
or
the formula of the C1 could also be:
= IF (Len (a1) > 0, RIGHT (A1, LEN (A1) −FIND ("", A1)), "")
Select cells B1 and C1, copy
Select cells B1 at the end of the C column, paste
-
Multiple lines in a single column in an SQL statement
Can someone provide me with a simple sql that runs on the underside of the table (USERROLE table)
ID ROLEUSER ROLENAME
1 user1 GL
User2 OBI_AP 2
User1 3 OBI_AP
User2 4 GL
User1 5 OBI_AR
User2 6 AR
7 the GL util_3
and give the result form
ROLENAMES ROLEUSER
User1 GL; OBI_AP; OBI_AR
User2 OBI_AP; GL; AR
the GL util_3
Thank you
VikramHello
There is a similar thread, you can watch: concatenate the values of column in a row
Kind regards
-
Create views of data from multiple lines in a single column shows
Hi all - it's probably posted in the wrong forum, but I couldn't find that was right.
I'm almost a perfect beginner in sql, but I have a need to create a view that can be expanded to 10g (which effectively runs the volumes are likely to be high) who will do the following.
Authentic table with columns Parent_code, Child_code
Parent_Code Child_Code
1000-2000
1000-3000
1000-4000
2000 3000
2000-5000
(note that Parents may have several children and a child can have multiple parents!)
What I have to finish with in my opinion is the following
Child_Code Parent_List
' 2000 ' 1000 (3).
3000 "1000 (3), 2000 (2)"
' 4000 ' 1000 (3).
"5000 ' 2000 (2)"
Note the number in parentheses is the number of children whose parent's - IE in the original parent a 1000, 3 table lines (one for each child)
This point of view should be used as a quick glance upward (on the children's code) for a report of business objects.
Is there someone who could you PLEASE, PLEASE help me quickly on what I have very little time to find a solution?Hello
You can test these:
select child_code , ltrim(sys_connect_by_path(parent_info,', '), ', ') as parent_list from ( select child_code , to_char(parent_code) || ' (' || count(*) over(partition by parent_code) || ')' as parent_info , row_number() over(partition by child_code order by parent_code) rn from your_table ) where connect_by_isleaf = 1 connect by prior rn = rn-1 and prior child_code = child_code start with rn = 1 ;
select child_code, rtrim( extract( xmlagg(xmlelement("e",parent_info||', ') order by parent_info) , '//text()' ) , ', ' ) as parent_list from ( select child_code, to_char(parent_code) || ' (' || count(*) over(partition by parent_code) || ')' as parent_info from your_table ) group by child_code ;
What you need is called 'chain aggregation '.
See here for the various techniques, including the two above: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php -
How to use SQL * Loader to load the XMLType column with other columns?
Hello
I try to use sqlldr to load an XML file into a table with an XMLType column. I have found many examples where the entire table is an xmltype, but I'd like to load a lot of XML objects in a generic table for treatment with a single XMLType column and other columns to identify the load. A simple example, I have a constant column which I want to put during the download.
The following example does not work: (.) No error either.
create the table xml_upload
(the varchar2 (10) of upload_type not null,)
donnees_xml XMLType)
/
my control file:
DOWNLOAD THE DATA
INFILE * add
IN THE TABLE xml_upload
XMLType (xml_data)
FIELDS ENDED BY ',' POSSIBLY FRAMED BY "" "
(
constant upload_type MARKET,
donnees_xml
)
BEGINDATA
< SALE_ORDER >
TIM < CUST_CODE > < / CUST_CODE >
< ORDER_NUM > 1234 > < / ORDER_NUM >
< / SALE_ORDER >
$ sqlldr my.ctl
SQL * Loader: Release 10.2.0.4.0 - Production on Sun Sep 27 22:51:52 2009
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Commit the point reached - the number of logical records 4
SQL > select * from xml_upload;
no selected line
SQL >
Any ideas on how I can do this? Did I miss something...
I also played with the clause sqlldr lobfile, also without success.
Current database version is 10g R2.
Thank you
Tim.The following works in 11.1.0.6
LOAD DATA INFILE * INTO TABLE xml_upload TRUNCATE ( upload_type constant 'MARKET' ,file_name filler char(100) ,xml_data LOBFILE (file_name) TERMINATED BY EOF ) BEGINDATA test.xml
SQL> select upload_type,xmlserialize(document xml_data no indent) from xml_upload; UPLOAD_TYP XMLSERIALIZE(DOCUMENTXML_DATANOINDENT) ---------- ---------------------------------------------------------------------- MARKET 1 SQL>
-
Hierarchy in a single column, with a hole more
Hello
I have the following query, which built a hierarchy:with it_dept (orga3) as
(
Select '0305' across double Union
Select "03050202" of any double union
Select "03050105" of any double union
Select "03050001" of any double union
Select "03050106" of any double union
Select "03050203" of any double union
Select "03050107" of any double union
Select "0305000101" of any double union
Select "0305000102" of any double union
Select "0305000103" of any double union
Select "0305020201" of any double union
Select "0305020202" of any double union
Select "0305020203" of any double union
Select "0305020301" of any double union
Select "0305020302" of any double union
Select "03050204" of any double union
Select "0305020401" of any double union
Select "0305020402" of any double union
Select "0305010501" of any double union
Select "0305010502" of any double union
Select "0305010601" of any double union
Select "0305010602" of any double union
Select "0305010701" of any double union
Select "0305010702" of any double union
Select "030501" of any double union
Select '030502' double
)
Select parent_org,
child_org,
level lvl
Of
(
Select parent_org null,
child_tbl.orga3 child_org
of it_dept child_tbl
where length (child_tbl.orga3) = (select min (length (orga3))
of it_dept
)
the Union / * cover the hole * /.
Select '0305' parent_org,
'030500' child_org
of the double
Union
Select parent_tbl.orga3 parent_org,
child_tbl.orga3 child_org
of it_dept parent_tbl
outer join left it_dept child_tbl
on parent_tbl.orga3 = substr (child_tbl.orga3, 1, length (parent_tbl.orga3))
and (child_tbl.orga3) length = length + 2 (parent_tbl.orga3)
)
Connect prior child_org = parent_org
Start with parent_org set to null.
1. the data is organized in such a way that they build a hierarchy. Each level of
This has the same number of digits.
2. the data of the hierarchy is on a specific data column (i.e.: orga3).
3. There's a hole in the hierarchy, which means that a member is missing (namely: the 2nd member of '030500' level).Issues related to the:
(1) is there a simpler way to achieve the same result?
(2) is it possible do not write the missing member, in order to cover the hole?
(3) is it possible do not write + 2 (as in the line "and length = length + 2 (parent_tbl.orga3) (child_tbl.orga3)") for
in terms of getting the members of next level?
Note: I'm using OracleDb 11 g v.2Thank you
SIMWhen I see a problem that really needs a better model of data, I wonder: ' how would I migrate data to a better model?
Once I wrote the SQL code to migrate the data, the SQL to meet the requirement is simple.
If you cannot migrate the code, just run the two SQL bits, one after the other.
For example, the test data:
create table it_dept(orga3) nologging as select '0305' from dual union all select '03050202' from dual union all select '03050105' from dual union all select '03050001' from dual union all select '03050106' from dual union all select '03050203' from dual union all select '03050107' from dual union all select '0305000101' from dual union all select '0305000102' from dual union all select '0305000103' from dual union all select '0305020201' from dual union all select '0305020202' from dual union all select '0305020203' from dual union all select '0305020301' from dual union all select '0305020302' from dual union all select '03050204' from dual union all select '0305020401' from dual union all select '0305020402' from dual union all select '0305010501' from dual union all select '0305010502' from dual union all select '0305010601' from dual union all select '0305010602' from dual union all select '0305010701' from dual union all select '0305010702' from dual union all select '030501' from dual union all select '030502' from dual;
To migrate the data, I have to assume that there is difference of 2 bytes between child and parent. If you introduce a difference of 1 or 3 bytes, we're all lost.
create or replace view new_it_dept as with minlen as ( select min(length(orga3)) minlen from it_dept ) , filled_hierarchy as ( select distinct substr(orga3,1,minlen+(level-1)*2) orga3, minlen from it_dept, minlen connect by orga3 = prior orga3 and prior sys_guid() is not null and length(orga3) >= minlen+(level-1)*2 ) select case when length(orga3) > minlen then substr(orga3,1,length(orga3)-2) end dad, orga3 son from filled_hierarchy;
If you create a table with that point of view, you have migrated data. If you cannot migrate, then simply use the view in your query.
select dad, son, level from new_it_dept start with dad is null connect by dad = prior son;
DAD SON LEVEL 0305 1 0305 030500 2 030500 03050001 3 03050001 0305000101 4 03050001 0305000102 4 03050001 0305000103 4 0305 030501 2 030501 03050105 3 03050105 0305010501 4 03050105 0305010502 4 030501 03050106 3 03050106 0305010601 4 03050106 0305010602 4 030501 03050107 3 03050107 0305010701 4 03050107 0305010702 4 0305 030502 2 030502 03050202 3 03050202 0305020201 4 03050202 0305020202 4 03050202 0305020203 4 030502 03050203 3 03050203 0305020301 4 03050203 0305020302 4 030502 03050204 3 03050204 0305020401 4 03050204 0305020402 4 -
Question-DAQmx: using multiple channels on a single device with a relaxation
The purpose of the attached VI (Switching_Controller.vi) is to wait for a triggering of the input signal and an output pulse whenever it occurs. However, at the same time I want to output and read a sample of another entry and exit of the pair of channels (Main_Controller.vi behavior). I was counting on this operation in two parallel Subvi but I am running in the commune-50103 error 'the specified resource is reserved. I understand that in order to solve this problem, I need to compress all output channels and all channels of entry into just two tasks. However, I cannot address the issue of the trigger, because I want the second set of inputs and outputs to occur continuously and relaxation force the task to a certain repetition rate. Is it possible to run a multichannel task in two parallel Subvi?
Thank you for the insight.
Hello!
Please post on the Forums OR! 'Reserved resources' are a common mistake and it seems that you are aware of its source. With the help of two tasks of the same type at the same time without having anything between the two that uncommits resources will not work. Your best option here would be to combine all your HAVE AO in another task in a task and every one of you.
What you could do is to use an analog line available that you can analyze and implement a logic with something as a structure case to insert a value in a table, display it, open a session, or all you want to do with it, when this analog channel crosses a value you're looking for.
You can include your other I / AO in the tasks and have just their acquisition / output as usual.
Hope that this gets you going in the right direction. Have a great day!
-
How to scan multiple pages of a single document with the help of a 5530 want?
Under 'Advanced settings', tab 'File', the "Create a separate file for each scanned page" checkbox is NOT checked.
I tried to check the box and do a scan and then uncheck the box. no luck
Thanks in advance
I thought about it - apparently this software/mode particaulr does not believe allowing PDF files with high resolution (600 dpi or more) of multi page.
Thanks anyway
-
Splitting of multiple lines on a single line with different fields
Hi all
I have a table. A Course_Code have section_codes several other words ('NUM' column is not sectype is just an order!)
Code---num---Sec_code---Sectype---DESC---WEIGHT
1603 1 - C 7427 - Coursework - 50
1603--------2-------7428-----------E------------ Exam---------------- 50
I query this table I want to be able to see the course work, review divided on the same line. i.e.
Code - Coursework - review
1603 C = 50 - E = 50
any help would be greatHello
'Splitting' means take an element (such s as a string 'C = 50') and diviiding in smal; LER objects (such as the chains under "C" and "50"). Are you really trying to divide something, or are you trying to do exactly the opposite (for example, combine smaller chains in a larger string)?
I think that what you are looking for is a Pivot . Like so many other things, exactly how do depends on your version of Oracle and your needs.
Here's one way:SELECT code , MIN (CASE WHEN descr = 'Coursework' THEN sectype END) || '=' MIN (CASE WHEN descr = 'Coursework' THEN weight END) AS coursework , MIN (CASE WHEN descr = 'Exam' THEN sectype END) || '=' MIN (CASE WHEN descr = 'Exam' THEN weight END) AS exam FROM table_x GROUP BY code ;
It will work in Oracle 8.1 and higher. From Oracle 11.1, you can also use the SELECT... Function PIVOT.
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables and also post the results desired from these data.
Explain, using specific examples, how you get these results from these data.
Always tell what version of Oracle you are using. -
two rows of back with a single column with just one place
Hello all;
Published by: user13328581 on March 22, 2011 11:52I have a query that returns two rows similar to this below ID PLACE PROGRAM A NEWYORK PROGRAM A A NEWYORK PROGRAM B I would like this instead ID PLACE PROGRAM A NEWYORK PROGRAM A PROGRAM B All help is appreciated. Thank you.
user13328581 wrote:
WOW... Thanks a lot for Salomon, I never used the partitions and row_number in such a way... can you please explain your logic.Ensure that:
ROW_NUMBER() over (partition by order of identification by location, program)
This operation can take all the rows returned by the query andsplit them in compartments (sheet music) by id. Inside each bucket it order lines by program and place and assign line numbers. So for each row of the ID number 1 will be stored with (in alphabetical order) first place the first programt. And this is the line where we want to ID is displayed. That's why we wrap the analytical function that precedes in the CASE statement that will do just that. Now:
ROW_NUMBER() over (partition by id, order by program)
pretty much the same just bucket is a combination ID / PLACE. So for each combination of it, we want to show that for the first (in alphabetical order) programt.
Now notice in the ORDER BY clause I prefix ID and PLACE with the table alias. You do it because otherwise the query alias ID and PLACE will have priority resulting in the incorrect sort order (remember that we cancelled all ID and PLACE with the exception of the number of line 1).
SY.
-
Scan multiple documents in a single file with HP advantage 3525
I want to scan several documents and save them in a file using the printer HP advantage 3525. It scans only one document and it must be savrd to a new file every time. How do I overcome this? Thank you
Hello
There are few options but please try first the builtin option. There is a small (+) sign after a click just + page to add another page... and at the end to record the entire session to a pdf file:
Kind regards.
-
How to scan multiple pages into a single file with a craving for hp 110?
I am running Win 7. I don't see an icon "+" in the screen of 110 envy of hp to be able to scan additional pages (such as the State of instructions online).
Thank you.
Hello
After the Scan, a Document dialog box or Photo reduce the resolution to 300 DPI or lower, the sign should appear in the preview screen.
Shlomi
-
Use of multiple sequence on a single column of odi
My requirement is-
I have the source table
S
1
1
1
2
2
3
1 and so on
I want that in the target
SEQ S
1 1
1 2
1 3
2 1
2 2
1 s
1 4
It should maintain the sequence for each SDC of the source table.
Published by: SSZ on 3 Sep 2012 06:55If you have any problem to create simple interface then do not use the user function.
How to create the interface
1. run ODI Studio
2. choose the tab "designer".
3. in the section models : model to create and import target and source of db
4. in the sections of projects :
Create interface
Go to Quick-editing (bottom). The target and Source value. Expression of mapping of the group as row_number() over (partition S)
Go to the stream. Choose the stage space, and in the Properties window, set IKM.
Run the interface.Read manual (press F1 in ODI Studio). ODI is not easy to understand software.
-
Name of the table: employee
Employee_id First name Last_name Salary Joining_date Department 1 John Abraham 1000000 1ST JANUARY 13 12.00.00 AM Banking services 2 Michael Clarke 800000 1ST JANUARY 13 12.00.00 AM Insurance 3 Roy Thomas 700000 1ST FEBRUARY 13 12.00.00 AM Banking services 4 Tom Jose 600000 1ST FEBRUARY 13 12.00.00 AM Insurance 5 Jerry Pinto 650000 1ST FEBRUARY 13 12.00.00 AM Insurance 6 Philippe Mathew 750000 1ST JANUARY 13 12.00.00 AM Services 7 TestName1 123 650000 1ST JANUARY 13 12.00.00 AM Services 8 TestName2 Lname % 600000 1ST FEBRUARY 13 12.00.00 AM Insurance Table name: incentives
Employee_ref_id Incentive_date Incentive_amount 1 FEBRUARY 1, 13 5000 2 FEBRUARY 1, 13 3000 3 FEBRUARY 1, 13 4000 1 JANUARY 1, 13 4500 2 JANUARY 1, 13 3500 Hello
SELECT e.first_name. '_' || FOCA select e employee;
Is that what you ask?
Best regards
Bruno Vroman.
Maybe you are looking for
-
HP envy m6-n012dx: UEFI mode change in Legacy mode
Hey I want to change my boot option to the legacy of the uefi mode mode. can someone help me.
-
Photo from library missing in iMovie 09
I installed my OS to El Capitan 10.11.3. My photos are missing in iMovie 09. iMovie are not references to the correct library. How can I select the correct library?
-
Must photo images be stored in the iCloud?
I just bought a new iMac... replace my old duo core intel 20 "... and my situation is that my internet connection is a satellite which has a limited monthly data plan. There is no downloading easy and seamless with my modem and some interruption buf
-
I E1 - 571G I'm interested what bios is the best for this model and is the last. I use win 7 64 bit, also my laptop use especially integrated intel hd graphics and not nvidia geforce gt620m can do gt620m my main gpu? and 1 last thing in nvidia Contro
-
Request Sostituzione disc by server PowerEdge 2900 III [GRO]
Buongiorno,avrei need di auswechseln uno degli hd led raid 5 sul mio Server PowerEdge 2900 III (serial number ).He e modello a Western Digital WD800JD - da SATA WD Caviar SE - 75MSA3 - 80.0 GB - 7200 RPM.Indicarmi di e price terms could he prezzo? Gr