oracle sql analytics

Posted on 2014-12-04
Last Modified: 2015-02-16
i have a table which records students preferences. i need to flag up the students who have only preferenced 20, 21 or 22.
the table below shows the output from the pref table (2 columns), and the output shows the desired output.

select * from pref			output					
student	preference		student	only_20_21_22				
1		1				1		n
1		2				2		n
1		3				3		y
2		1				4		n
2		2				5		y
3		20				6		n
4		21				7		n
4		22				8		y
4		1				9		n
4		2				10		n
5		20						
5		22						
5		21						
6		1						
6		2						
7		1						
7		20						
8		20						
9		20						
9		1						
9		2						
9		22						
10		2						

Open in new window

any help is appreciated
Question by:tonMachine100

Expert Comment

by:Ahmed Merghani
ID: 40480446
Try this:
Select student, CASE 
            WHEN preference = 20 or preference = 21 or preference = 22 
               THEN 'y' 
               ELSE 'n' 
       END as only_20_21_22 from pref

Open in new window

LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40480461
select p.student,
       case regexp_count(listagg(p.preference, ',') within group(order by p.student), '^20|^21|^22')
         when 1 then
       end only_20_21_22
  from pref p
 group by p.student
 order by p.student;

Open in new window

LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40480464
@Ahmed Merghani: Please see the asker's expected results! Your statement will not work upon what he asked for...
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40480578
Assuming the sample data provided is not a complete sample and the following data is possible:
student, preference

Open in new window

The above query will also fail.

Building on it try this:
select p.student,
	case when ltrim(regexp_replace(listagg(p.preference, ',') within group(order by p.student),'2[012]'),',') is null then 'y' else 'n' end 
  from pref p
 group by p.student
 order by p.student;

Open in new window

LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40480598
Yes, slightwv's regex will work better upon the (sample) data provided...
LVL 32

Accepted Solution

awking00 earned 250 total points
ID: 40480872
select student,
min(case when not regexp_like(preference,'2[012]') then 'n' else 'y' end) student_only_20_21_22
from pref
group by student
order by student;
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40480890
Knew there had to be a cleaner way...

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now