Oracle PL/SQL : report formatting [duplicate] - sql

This question already has answers here:
How to retrieve two columns data in A,B format in Oracle
(4 answers)
Closed 9 years ago.
I have been asked to create a report using PL/SQL to get names associated with a given city. While this is not in the least bit difficult, I find that the way in which the data is to be presented is something I have not seen from SQL. The report needs to be formatted such that the city name appears first and all subsequent people associated with that city are to be listed after that - in a single line.
TEMPE: Rich Allen, Jerry Black, et al..
TUSCON: Bob Adams, Frank Bruce, et al..
I can't recall ever seeing output like this and am a bit stuck on how to present this data.
Any suggestions would be appreciated.

Select city, wmConcat(namefield)
from tablename
group by City
WM_CONCAT for versions 10g and prior
LISTAGG for newer versions.

Related

Exact String match using oracle CONTAINS for multiple values

I need to get exact name string match from multiple values using Contains function.
I used below query to get data which matches exactly JOHN SMITH OR MIKE DAVID but query is fetching all data which has JOHN SMITH, JOHN, SMITH, MIKE, DAVID, MIKE DAVID, JOHN..SMITH, JOHN/SMITH,....
where contains(names,'{JOHN SMITH} OR {MIKE DAVID}')>0
Note - I don't want to use multiple like in OR conditions.We need to pass around 200 to 300 values (names) to do match pattern.
Can anyone let me know how to get exact match from multiple values using CONTAINS?
Thanks
Anand
I don't use Oracle but i liked this question and i will try to answer it based on what i read on Oracle's documentation, regarding CONTAINS.
so i am currently reading about Stored Query Expression (SQE),
Stored Query Expression (SQE)
Use the SQE operator to call a stored query expression created with
the CTX_QUERY.STORE_SQE procedure.
Stored query expressions can be used for creating predefined bins for
organizing and categorizing documents or to perform iterative queries,
in which an initial query is refined using one or more additional
queries.
So perhaps you could pass all the names you want to query in such a way. For example:
begin
ctx_query.store_sqe('mynames', 'JOHN SMITH OR MIKE DAVID OR GEORGE HARRIS OR JOHN DOE');
end;
And then call it:
SELECT SCORE(1), nameid FROM mytable
WHERE CONTAINS(names, 'sqe(mynames)', 1)> 0
ORDER BY SCORE(1);
Hope this was helpful.

How to remove the last 3 words from a string in PL/SQL? [duplicate]

This question already has answers here:
Regex - How to replace the last 3 words of a string with PHP
(3 answers)
Closed 4 years ago.
I have strings like these:
Jack & Bauer Limited Company Bristol
Streetfood Limited Company München
Brouse with High Jack UnlimiteD Company London
What I want to have is just the company names like:
Jack & Bauer
Streetfood
Brouse with High Jack
So in every case, I have to delete the last 3 words, because the names can be consist a lot of words.
I know I have to use regexp, but I dont know how.
While you can use regular expressions to do this you don't have to. This task can be accomplished using a combination of INSTR and SUBSTR:
SELECT SUBSTR(FIELD1, 1, INSTR(FIELD1, ' ', -1, 3)-1) AS NAME
FROM TABLE1
SQLFiddle here
Best of luck.
Here is one method:
select regexp_replace(str, '( [^ ]+){3}$', '')
Here is a rextester.

Regular expression to detect doubled vowels in Oracle [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Carets in Regular Expressions
(2 answers)
Have trouble understanding capturing groups and back references
(2 answers)
Closed 5 years ago.
Can anyone explain this regular expression? This query is used in Oracle to returns the last name for those employees with a double vowel (where last_name contains two adjacent occurrences of either a, e, i, o, or u, regardless of case):
SELECT last_name
FROM employees
WHERE REGEXP_LIKE (last_name, '([aeiou])\1', 'i');
The output is :
LAST_NAME
---------------
De Haan
Greenberg
Khoo
Gee
Greene
Lee
Bloom
Feeney
The regex pattern ([aeiou])\1 simply matches two vowels in succession:
([aeiou]) match and capture a single vowel
\1 then match the same vowel we just captured
If you examine the matching last names, you will see that they all have repeating vowels in some position. By the way, the term \1 is known as a backreference, which refers to a captured quantity earlier in the pattern.
Explore the helpful demo below to better understand how the pattern works.
Demo

SQL comma separated values grouped in query [duplicate]

This question already has answers here:
Concat groups in SQL Server [duplicate]
(5 answers)
SQL in (#Variable) query
(3 answers)
Closed 9 years ago.
I am using SQL Server 2008 and inherited a database that did not use many to many. They instead used a comma-separated column. I have found how to link the comma-separated values to the name of the program. But I need a list of the programs and the offices they belong to, like this
OFFICE table:
ID Name
--- ------
1 HQ
2 PA
3 CEO
PRG table:
ID Name Office Affected
-- ---- ---------------
A PRG1 1,3
B PRG2 2
C PRG3 2,3
D PRG4 1,2
Output that I need :
Name Programs
---- ---------
HQ PRG1, PRG4
PA PRG2, PRG3, PRG4
CEO PRG1, PRG3
You can manage to do this. However, because storing lists in strings is a bad idea, I don't want to compound that by putting them back in a comma-delimited list. Instead, the following query produces the data in a more normalized form, with one row per office name and program:
select o.name, p.name as program_name
from prg p join
office o
on ','+p.OfficeAffected+',' like '%,'+cast(o.id as varchar(255)) + ',%';

How to get a column with the grouped info of rows with the same id? [duplicate]

This question already has answers here:
Concatenate multiple rows in one field in Access? [duplicate]
(4 answers)
Closed 8 years ago.
How can I group multiple rows of data?
My data structure is similar to:
ID NAME PhoneNo
1 Jon 8798765
2 Jon 3134684
3 Adams 7968434
4 Phil 3435435
5 Thomas 6734354
6 Jon 2343545
7 Jeff 3435424
8 Adams 3434354
I need to use SQL to group the information so I get something like this:
ID NAME PhoneNo
1 Jon 8798765,3134684,2343545
3 Adams 7968434,3434354
4 Phil 3435435
5 Thomas 6734354
7 Jeff 3435424
See what I did there? I de-duplicated and added all the phone numbers on the same field, comparing the names: Same name= same person, so put all the names on the same cell.
I'm currently using MS access, but I guess any other variant could work (I can find the equivalency)
What you are looking for is mySQL function GROUP_CONCAT
If you are in Access, just use some kind of Macro inspired by this: is there a group_concat function in ms-access? or for MSSQL Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005
There is a Question, what are you really need.