Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL - filter out partitions or row groups that contain rows with specific value

Tags:

sql

oracle

filter

I'm trying to solve the following: the data is organized in the table with Column X as the foreign key for the information (it's the ID which identifies a set of rows in this table as belonging together in a bundle, owned by a particular entity in another table). So each distinct value of X has multiple rows associated with it here. I would like to filter out all distinct values of X that have a row associated with them containing value "ABC" in Column Q.

i.e.

data looks like this:

Column X   Column Q
--------   ---------
123        ABC
123        AAA    
123        ANQ
456        ANQ
456        PKR
579        AAA
579        XYZ
886        ABC

the query should return "456" and "579" because those two distinct values of X have no rows containing the value "ABC" in Column Q.

I was thinking of doing this with a minus function (select distinct X minus (select distinct X where Q = "ABC")), as all I want are the distinct values of X. But i was wondering if there was a more efficient way to do this that could avoid a subquery? If for example I could partition the table over X and throw out each partition that had a row with the value "ABC" in Q?

like image 399
ubanerjea Avatar asked Jan 29 '13 19:01

ubanerjea


People also ask

What SQL clauses filter rows or groups of rows?

You use the WHERE clause in the SQL statements such as SELECT, UPDATE and DELETE to filter rows that do not meet a specified condition.

How do I filter specific rows in SQL?

In SQL, the SELECT statement is used to return specific columns of data from a table. Similarly, the WHERE clause is used to choose the rows of a table, and the query will return only the rows that meet the given criteria.

Which is better GROUP BY or partition by?

Therefore, in conclusion, the PARTITION BY retrieves all the records in the table, while the GROUP BY only returns a limited number. One more thing is that GROUP BY does not allow to add columns which are not parts of GROUP BY clause in select statement. However, with PARTITION BY clause, we can add required columns.

How do I filter rows in Oracle SQL Developer?

Selecting Rows with a FILTER Clause. Use a FILTER clause to select rows based on a numeric value by using basic operators or one or more Oracle GoldenGate column-conversion functions. Note: To filter a column based on a string, use one of the Oracle GoldenGate string functions or use a WHERE clause.


1 Answers

I prefer to answer questions like this (i.e. about groups within groups) using aggregation and the having clause. Here is the solution in this case:

select colx
from data d
group by colx
having max(case when colq = 'ABC' then 1 else 0 end) = 0

If any values of colx have ABC, then the max() expression returns 1 . . . which does not match 0.

like image 124
Gordon Linoff Avatar answered Sep 24 '22 13:09

Gordon Linoff