The original table has three not unique columns, where id value separate data by groups:
id name flag
----------------------------
1 Andrey 0
1 Andrey 1
1 Olga 0
1 Sasha 0
2 Masha 1
2 Masha 0
2 Katya 1
2 Vera 0
Here we have two groups of people, where each group has two rows with equal names. I would like to write a select statemets which duplicate name rows with flag 0, if and only if this exact name has 1 flag in the same group.
So, expected result for this data should be:
id name flag
----------------------------
1 Andrey 1
2 Masha 1
because Andrey and Masha are duplicates and they have 0 flag too.
My attempt to select these values is to use query like following:
select * from names
where flag = 1 and id in
(select id from names where flag = 0)
but it's returns Katya row too, what is unexcepted.
You can use EXISTS:
SELECT n.* FROM Names n
WHERE n.flag = 1
AND EXISTS
(
SELECT 1 FROM Names n2
WHERE n.id = n2.id
AND n.name = n2.name
AND n2.flag = 0
)
You can use the intersect operator to get the name which has both the flags.
select name from names where flag = 0
intersect
select name from names where flag = 1
In MySQL, you could do,
select n1.name
from names n1
join names n2 on n1.name = n2.name and n2.flag = 1
where n1.flag = 0
Related
let's say I have the following entries in a table with two columns:
Id
Value
1
F
1
N
1
N
2
F
3
N
3
N
3
N
4
N
5
F
5
N
I only want to output the Ids that have a value of 'N' and at the same time not the value 'F' in the second column. In this example my output from the query would be the Ids: 3 & 4.
Thank you very much!
If the database supports EXISITS, you can check for the existence of a row with the id and the value F.
SELECT
DISTINCT Id
FROM mytable m1
WHERE Value = 'N'
AND NOT EXISTS(SELECT 1 FROM mytable WHERE Value = 'F' AND m1.Id = Id)
I need help with writing a query to generate a result that will provide me with all record numbers NOT assigned to Group D from a table structured like the below example. From the below table my desired result would be record number "3" .
Record_Number Assigned_To_Group
1 A
1 B
1 C
1 D
2 A
2 E
2 D
3 A
3 B
3 E
One method uses aggregation:
select Record_Number
from t
group by Record_Number
having sum(case when Assigned_To_Group = 'D' then 1 else 0 end) = 0;
Currently, I've got a single set of data in which I want to exclude based on if a condition is meet. The group has a common column reference.
Name Sequence Value
-----------------------------------
Text 1 1
Don 1 30
Text 2 0
Sid 2 240
Florence 2 300
Text 3 200
Casper 3 20
Cat 3 10
Text 4 0
Dem 4 50
Basically any row in which Text is not equal to 0 needs to be excluded be excluded. In addition the rows in which share the same sequence. Expected outcome is to only have data from sequence 2 and 4.
You can try with NOT EXISTS as below-
SELECT Name,
Position,
Value
FROM your_table
WHERE NOT EXISTS (
SELECT Name,Position,Value
FROM your_table
WHERE (Name = 'Text' AND Value = 1)
OR (Position = Value)
)
As you are looking for options other than NOT EXISTS, you can try this below-
SELECT *
FROM your_table
WHERE [Sequence] NOT IN (
SELECT DISTINCT [Sequence]
FROM your_table
WHERE [Name] = 'Text'
AND [Value] <> 0
)
I need to select Name entries for which none of the rows fulfills a certain requirement. This is best described as an example- lets consider the following table:
Name Number ID
A 1 2
A 2 2
B 1 2
B 2 3
C 3 3
The requirement would be that Number=ID. For the name B this is never the case, so I would like to return the Name B.
Name Number ID
A 1 2
A 2 2 <---- fulfills requirement for A
B 1 2
B 2 3
C 3 3 <---- fulfills requirement for C
Is this possible in SQL?
You can use a NOT EXISTS clause
select * from Table1 t1
where not exists (select null
from Table1
where t1.Name = Name
and Number = Id)
If you just want the names which don't fulfill the requirement, just change the select to
select distinct t1.Name
see SqlFiddle with both versions.
Try this
Select Name
from table
where name not in (Select distinct name from table where number = id )
I essentially would like to execute a statement where x=1 AND a=2. If a<>2 then return results for filter x=1.
I've tried OR statement but it ignores my a=2 filter if there is a scenario where a does equal 2. Example below
select *
from dbo.test
where (x=1 and a=2)
or
x=1
For output purposes below: type = x and Id = a
Expected result when Type = 1.
Id(a) Name Person Type(x)
2 a Mike 1
7 b Jim 1
3 c Tom 1
4 d Tim 1
5 e Dave 1
Expected result when Type = 1 and Id = 2
Id(a) Name Person Type(x)
2 a Mike 1
Expected result when Type = 1 and Id <> 2 (scenario when there is no '2' in Id column)
Id(a) Name Person Type(x)
8 a Mike 1
7 b Jim 1
3 c Tom 1
4 d Tim 1
5 e Dave 1
The issue is not when Id = 2. It is returning Type = 1 when Id <> 2. Does that mean a case statement?
select *
from dbo.test
where (x=1 and a=2)
or x=1
Is the same as:
select *
from dbo.test
where x=1
AND requires both conditions be met, and OR requires one condition to be met. The value of a is irrelevant.
UPDATE:
You can get what you're after using the RANK() function in conjunction with a CASE statement:
;WITH cte AS (SELECT *,RANK() OVER(ORDER BY CASE WHEN id = 2 THEN 1 ELSE 2 END)RNK
FROM Table1
WHERE Type = 1)
SELECT *
FROM cte
WHERE RNK = 1
If id = 2 is present in the table, only that record will be returned, otherwise all records will be returned.
Demo: SQL Fiddle
I'm guessing that you want to return a single row.
insert into test(x,a,v) values (0, 0, 'No good')
insert into test(x,a,v) values (1, 0, 'OK')
insert into test(x,a,v) values (1, 2, 'Better')
You want the row that has x=1 a=2 if it is there
You want the row that has x=1 if there is none better
You want nothing if there is no x=1 row
If that's the case then it is a bit tricky. You can score the rows using a CASE statement and then pick the row that has the best value. This may give you more than one row back if there is a tie.
SELECT v
FROM test
WHERE CASE WHEN x=1 AND a=2 THEN 1000
WHEN x=1 AND a<>2 THEN 100
ELSE 0 END =
(SELECT MAX(CASE WHEN x=1 AND a=2 THEN 1000
WHEN x=1 AND a<>2 THEN 100
ELSE 0 END)
FROM test)