Identify only when value matches - sql

I need to return only rows that have the match e.g Value = A, but I only need the rows that have A and with no other values.
T1:
ID Value
1 A
1 B
1 C
2 A
3 A
3 B
4 A
5 B
5 D
5 E
5 F
Desired Output:
2
4
how can I achieve this?
when I try the following, 1&3 are also returned:
select ID from T1 where Value ='A'

With NOT EXISTS:
select t.id
from tablename t
where t.value = 'A'
and not exists (
select 1 from tablename
where id = t.id and value <> 'A'
)
From the sample data you posted there is no need to use:
select distinct t.id
but if you get duplicates then use it.
Another way if there are no null values:
select id
from tablename
group by id
having sum(case when value <> 'A' then 1 else 0 end) = 0
Or if you want the rows where the id has only 1 value = 'A':
select id
from tablename
group by id
having count(*) = 1 and max(value) = 'A'

I think the simplest way is aggregation with having:
select id
from tablename
group by id
having min(value) = max(value) and
min(value) = 'A';
Note that this ignores NULL values so it could return ids with both NULL and A. If you want to avoid that:
select id
from tablename
group by id
having count(value) = count(*) and
min(value) = max(value) and
min(value) = 'A';

Oracle Setup:
CREATE TABLE test_data ( ID, Value ) AS
SELECT 1, 'A' FROM DUAL UNION ALL
SELECT 1, 'B' FROM DUAL UNION ALL
SELECT 1, 'C' FROM DUAL UNION ALL
SELECT 2, 'A' FROM DUAL UNION ALL
SELECT 3, 'A' FROM DUAL UNION ALL
SELECT 3, 'B' FROM DUAL UNION ALL
SELECT 4, 'A' FROM DUAL UNION ALL
SELECT 5, 'B' FROM DUAL UNION ALL
SELECT 5, 'D' FROM DUAL UNION ALL
SELECT 5, 'E' FROM DUAL UNION ALL
SELECT 5, 'F' FROM DUAL
Query:
SELECT ID
FROM test_data
GROUP BY ID
HAVING COUNT( CASE Value WHEN 'A' THEN 1 END ) = 1
AND COUNT( CASE Value WHEN 'A' THEN NULL ELSE 1 END ) = 0
Output:
| ID |
| -: |
| 2 |
| 4 |
db<>fiddle here

Related

Need to get the mismatcted cells in specific partition

ID
TC_No
Result
1
tc_1
PASS
1
tc_2
PASS
1
tc_3
FAIL
1
tc_4
PASS
1
tc_5
FAIL
2
tc_1
FAIL
2
tc_2
PASS
2
tc_3
FAIL
2
tc_4
FAIL
2
tc_5
FAIL
I'm trying to find all records that have conflicting "Result" on the same "TC_No" and among different "ID" values, filtered by ID IN (1,2).
Here's the expected output:
ID
TC_No
Result
1
tc_1
PASS
1
tc_4
PASS
2
tc_1
FAIL
2
tc_4
FAIL
and my attempted query:
SELECT * From
(SELECT * from Excel As T1
UNION
SELECT * from Excel As T2)
As c
where ID in(1,2) order By TC_NO
find the distinct count of result for each tc_no and then select the records having count greater than 1
Query
select * from your_tbl_name a
where exists(
select 1 from (
select tc_no, count(distinct result) as cnt
from your_tbl_name
where result in ('PASS','FAIL')
group by c_no
) b
where a.tc_no = b.tc_no
and b.cnt > 1
)
You can simply INNER JOIN the table onto itself and add a predicate in the WHERE clause to return only mismatched results.
SQL:
SELECT
a.ID,
a.TC_No,
a.Result
FROM
Excel a
INNER JOIN Excel b ON a.TC_No = b.TC_No
WHERE
a.Result <> b.Result;
Result:
| ID | TC_No | Result |
|----|-------|--------|
| 1 | tc_1 | PASS |
| 1 | tc_4 | PASS |
| 2 | tc_1 | FAIL |
| 2 | tc_4 | FAIL |
SQL Fiddle Demo:
Here
Check when the maximum result is different than the minimum one, in your filtered data, using window functions.
WITH cte AS (
SELECT tab.*,
MAX(Result_) OVER(PARTITION BY TC_No) AS max_result,
MIN(Result_) OVER(PARTITION BY TC_No) AS min_result
FROM tab
WHERE ID IN (1,2)
)
SELECT Id, Tc_No, Result_
FROM cte
WHERE min_result < max_result
Check the demo here.
You could use analytic function COUNT() OVER() to find the rows with different content and then just filter the result in Where clause:
SELECT ID, TC_NO, RESULT
FROM ( Select ID, TC_NO, RESULT,
CASE WHEN Count(DISTINCT RESULT) OVER(Partition By TC_NO) = 2 THEN 'Y' END "IS_DIFF"
From tbl
)
WHERE IS_DIFF = 'Y'
ORDER BY ID
With your sample data:
WITH
tbl (ID, TC_NO, RESULT) AS
(
Select 1, 'tc_1', 'PASS' From Dual Union All
Select 1, 'tc_2', 'PASS' From Dual Union All
Select 1, 'tc_3', 'FAIL' From Dual Union All
Select 1, 'tc_4', 'PASS' From Dual Union All
Select 1, 'tc_5', 'FAIL' From Dual Union All
Select 2, 'tc_1', 'FAIL' From Dual Union All
Select 2, 'tc_2', 'PASS' From Dual Union All
Select 2, 'tc_3', 'FAIL' From Dual Union All
Select 2, 'tc_4', 'FAIL' From Dual Union All
Select 2, 'tc_5', 'FAIL' From Dual
)
... the result is:
ID
TC_NO
RESULT
1
tc_1
PASS
1
tc_4
PASS
2
tc_1
FAIL
2
tc_4
FAIL

How can I group/aggregate values correlated to 2 distinct values as one?

So in the database there is a value in the 'value_type' column that are interchangeable with each other. When I aggregate, I want to be able to sum / aggregate the two values (A AND B) together but not the other(s) (C)? Some Sample Data is below:
Value_ID Value_Tx Value_Type
1 5 A
2 2 A
3 7 B
4 5 C
5 3 C
6 1 D
7 7 F
The Result I want:
Sum Value_Type
14 A | B
8 C
1 D
7 F
I know that to sum normally I would do:
select sum(value)tx, value_type
from table
group by value_type;
Thanks in advance.
Use case expression with group by.
SELECT SUM(value_tx),
CASE
WHEN value_type IN ( 'A', 'B' ) THEN 'A | B '
ELSE value_type
END
FROM t
GROUP BY CASE
WHEN value_type IN ( 'A', 'B' ) THEN 'A | B '
ELSE value_type
END
ORDER BY 2;
Demo
select sum(value)tx, 'A | B' value_type
from table where value_type in ('A','B')
union all
select sum(value)tx, value_type
from table where value_type not in ('A','B')
group by value_type;
easy way,hope help you : )
I have only tried my query in MySQL
select sum(value_tx),
case
when value_type IN ('A', 'B') THEN 'A | B'
else value_type
end as valtype
from table
group by valtype
Example of how to set the groups dynamically:
WITH test_data AS
(
SELECT 1 val_id, 5 val_tx, 'A' val_typ FROM dual
UNION ALL
SELECT 2, 2, 'A' FROM dual
UNION ALL
SELECT 3, 7, 'B' FROM dual
UNION ALL
SELECT 4, 5, 'C' FROM dual
UNION ALL
SELECT 5, 3, 'C' FROM dual
UNION ALL
SELECT 6, 1, 'D' FROM dual
UNION ALL
SELECT 7, 7, 'F' FROM dual
)
SELECT break_group, SUM(val_tx) total_tx, value_type FROM
(
SELECT val_id, val_tx, val_typ
, CASE WHEN dense_rnk IN (1, 2) THEN 1 ELSE (dense_rnk-1) END break_group
, CASE WHEN dense_rnk IN (1, 2) THEN 'A|B' ELSE val_typ END value_type
FROM
(
SELECT val_id, val_tx, val_typ
, DENSE_RANK() OVER (ORDER BY val_typ) dense_rnk
FROM test_data
)
)
GROUP BY break_group, value_type
ORDER BY break_group
/
Output:
BREAK_GROUP TOTAL_TX VALUE_TYPE
-----------------------------------
1 14 A|B
2 8 C
3 1 D
4 7 F

T-SQL ORDER BY base on MIN of a group's column

Hi take the following data as an example
id | value
----------
A | 3
A | 9
B | 7
B | 2
C | 4
C | 5
I want to list out all the data base on the min value of each id group, so that the expected output is
id | value
----------
B | 2
B | 7
A | 3
A | 9
C | 4
C | 5
i.e. min of group A is 3, group B is 2, group C is 4, so group B first and then the rest of group B in ascending order. Next group A and then group C
I tried this but thats not what I want
SELECT * FROM (
SELECT 'A' AS id, '3' AS value
UNION SELECT 'A', '9' UNION SELECT 'B', '7' UNION SELECT 'B', '2'
UNION SELECT 'C', '4' UNION SELECT 'C', '5') data
GROUP BY id, value
ORDER BY MIN(value)
Please help! Thank you
SELECT * FROM (
SELECT 'A' AS id, '3' AS value
UNION SELECT 'A', '9' UNION SELECT 'B', '7' UNION SELECT 'B', '2'
UNION SELECT 'C', '4' UNION SELECT 'C', '5') data
ORDER BY MIN(value) OVER(PARTITION BY id), id, value
OVER Clause (Transact-SQL)
Add the over() clause to your query output and you can see what it does for you.
SELECT *,
MIN(value) OVER(PARTITION BY id) OrderedBy FROM (
SELECT 'A' AS id, '3' AS value
UNION SELECT 'A', '9' UNION SELECT 'B', '7' UNION SELECT 'B', '2'
UNION SELECT 'C', '4' UNION SELECT 'C', '5') data
ORDER BY MIN(value) OVER(PARTITION BY id), id, value
Result:
id value OrderedBy
---- ----- ---------
B 2 2
B 7 2
A 3 3
A 9 3
C 4 4
C 5 4

Get distinct rows based on priority?

I have a table as below.i am using oracle 10g.
TableA
------
id status
---------------
1 R
1 S
1 W
2 R
i need to get distinct ids along with their status. if i query for distinct ids and their status i get all 4 rows.
but i should get only 2. one per id.
here id 1 has 3 distinct statuses. here i should get only one row based on priority.
first priority is to 'S' , second priority to 'W' and third priority to 'R'.
in my case i should get two records as below.
id status
--------------
1 S
2 R
How can i do that? Please help me.
Thanks!
select
id,
max(status) keep (dense_rank first order by instr('SWR', status)) as status
from TableA
group by id
order by 1
fiddle
select id , status from (
select TableA.*, ROW_NUMBER()
OVER (PARTITION BY TableA.id ORDER BY DECODE(
TableA.status,
'S',1,
'W',2,
'R',3,
4)) AS row_no
FROM TableA)
where row_no = 1
This is first thing i would do, but there may be a better way.
Select id, case when status=1 then 'S'
when status=2 then 'W'
when status=3 then 'R' end as status
from(
select id, max(case when status='S' then 3
when status='W' then 2
when status='R' then 1
end) status
from tableA
group by id
);
To get it done you can write a similar query:
-- sample of data from your question
SQL> with t1(id , status) as (
2 select 1, 'R' from dual union all
3 select 1, 'S' from dual union all
4 select 1, 'W' from dual union all
5 select 2, 'R' from dual
6 )
7 select id -- actual query
8 , status
9 from ( select id
10 , status
11 , row_number() over(partition by id
12 order by case
13 when upper(status) = 'S'
14 then 1
15 when upper(status) = 'W'
16 then 2
17 when upper(status) = 'R'
18 then 3
19 end
20 ) as rn
21 from t1
22 ) q
23 where q.rn = 1
24 ;
ID STATUS
---------- ------
1 S
2 R
select id,status from
(select id,status,decode(status,'S',1,'W',2,'R',3) st from table) where (id,st) in
(select id,min(st) from (select id,status,decode(status,'S',1,'W',2,'R',3) st from table))
Something like this???
SQL> with xx as(
2 select 1 id, 'R' status from dual UNION ALL
3 select 1, 'S' from dual UNION ALL
4 select 1, 'W' from dual UNION ALL
5 select 2, 'R' from dual
6 )
7 select
8 id,
9 DECODE(
10 MIN(
11 DECODE(status,'S',1,'W',2,'R',3)
12 ),
13 1,'S',2,'W',3,'R') "status"
14 from xx
15 group by id;
ID s
---------- -
1 S
2 R
Here, logic is quite simple.
Do a DECODE for setting the 'Priority', then find the MIN (i.e. one with Higher Priority) value and again DECODE it back to get its 'Status'
Using MOD() example with added values:
SELECT id, val, distinct_val
FROM
(
SELECT id, val
, ROW_NUMBER() OVER (ORDER BY id) row_seq
, MOD(ROW_NUMBER() OVER (ORDER BY id), 2) even_row
, (CASE WHEN id = MOD(ROW_NUMBER() OVER (ORDER BY id), 2) THEN NULL ELSE val END) distinct_val
FROM
(
SELECT 1 id, 'R' val FROM dual
UNION
SELECT 1 id, 'S' val FROM dual
UNION
SELECT 1 id, 'W' val FROM dual
UNION
SELECT 2 id, 'R' val FROM dual
UNION -- comment below for orig data
SELECT 3 id, 'K' val FROM dual
UNION
SELECT 4 id, 'G' val FROM dual
UNION
SELECT 1 id, 'W' val FROM dual
))
WHERE distinct_val IS NOT NULL
/
ID VAL DISTINCT_VAL
--------------------------
1 S S
2 R R
3 K K
4 G G

SQL to check for all values in column

I have the following table in Oracle DB.
ID VALUE
-----------
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
4 1
How can I select ID's which have all 3 values (1,2,3)
The simplest option is generally something like this
SQL> ed
Wrote file afiedt.buf
1 with x as (
2 select 1 id, 1 val from dual union all
3 select 1 id, 2 val from dual union all
4 select 1 id, 3 val from dual union all
5 select 2 id, 1 val from dual union all
6 select 2 id, 2 val from dual union all
7 select 3 id, 1 val from dual union all
8 select 3 id, 2 val from dual union all
9 select 3 id, 3 val from dual union all
10 select 4 id, 1 val from dual
11 )
12 select id
13 from x
14 where val in (1,2,3)
15 group by id
16* having count(distinct val) = 3
SQL> /
ID
----------
1
3
The WHERE clause identifies the values you're interested in. The HAVING clause tells you how many of those values need to exist. If you wanted all the rows that had at least 2 of the 3 values, for example, you'd change the HAVING clause to look for a COUNT of 2.
If a particular val is guaranteed to occur at most once per id, you can eliminate the distinct in the HAVING clause.
Try this:
SELECT ID
FROM TABLENAME T
WHERE EXISTS (SELECT *
FROM TABLENAME T1
WHERE T1.ID = T.ID AND T1.VALUE = '1')
AND EXISTS (SELECT *
FROM TABLENAME T2
WHERE T1.ID = T.ID AND T2.VALUE = '2')
AND EXISTS (SELECT *
FROM TABLENAME T3
WHERE T1.ID = T.ID AND T2.VALUE = '3')
or
SELECT ID
FROM TABLENAME T
WHERE (SELECT COUNT( * )
FROM (SELECT VALUE
FROM TABLENAME T1
WHERE T1.ID = T.ID
GROUP BY VALUE)) = 3;
where 3 is number of values which can be calculated by a
SELECT COUNT( * )
FROM TABLENAME T1
GROUP BY VALUE
so this will be general purpose:
SELECT ID
FROM TABLENAME T
WHERE (SELECT COUNT( * )
FROM (SELECT VALUE
FROM TABLENAME T1
WHERE T1.ID = T.ID
GROUP BY VALUE)) = (SELECT COUNT( * )
FROM TABLENAME T2
GROUP BY VALUE)
Here's an option... each expression in the HAVING clause is counting the number of values that are found equal to 1, 2, or 3. If any of these counts is less than 1, then the ID will not be returned.
http://sqlfiddle.com/#!4/00fdc/8
SELECT ID
FROM myTable
GROUP BY ID
HAVING
SUM(DECODE(VALUE, 1, 1, 0)) > 0 AND
SUM(DECODE(VALUE, 2, 1, 0)) > 0 AND
SUM(DECODE(VALUE, 3, 1, 0)) > 0
EDIT - To require value 1, and either 2 or 3:
SELECT ID
FROM myTable
GROUP BY ID
HAVING
SUM(DECODE(VALUE, 1, 1, 0)) > 0 AND
(
SUM(DECODE(VALUE, 2, 1, 0)) > 0 OR
SUM(DECODE(VALUE, 3, 1, 0)) > 0
)
select id from (select id,sum(case when value=1 then 1 else 0 end) as 'v1',
sum(case when value=2 then 1 else 0 end) as 'v2',
sum(case when value=3 then 1 else 0 end) as 'v3'
from orac group by id) as final
where v1>0 and v2>0 and v3>0
With this option you will get more than the IDs, up to your application to select the column you want:
SELECT ID,
sum(CASE WHEN VALUE = 1 THEN 1 ELSE 0 END) AS ONE,
sum(CASE WHEN VALUE = 2 THEN 1 ELSE 0 END) AS TWO,
sum(CASE WHEN VALUE = 3 THEN 1 ELSE 0 END) AS THREE
FROM MYTABLE
GROUP BY ID
HAVING ONE >= 1 AND TWO >= 1 AND THREE >= 1;
alternatively if your case is specific (only values 1, 2, 3 are possible, and no duplicate values are allowed), then you could try the following one:
SELECT ID,
count(VALUE) AS VALUECOUNT
FROM MYTABLE
GROUP BY ID
HAVING VALUECOUNT = 3;
I would take care before going that way, as you might get side effects if later you want to add additional values. But it's still worth proposing if your current case fits the restrictions given above.
And, of course, if you don't like the idea of fetching these intermediate counts, enclose the queries I gave within another select
SELECT ID FROM (
...
)