Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get entries from SQL that only appear in certain value?

Tags:

sql

mysql

I need to find entries in my SQL table that always and only appear in a certain value.

For example:

DeviceID    Transmission
--------    ------------
000329      Inventory
000980      Inventory
004406      Starting
000980      Stopping
000329      Inventory
004406      Inventory

Now I need to find all DeviceIDs that only have Inventory Transmissions and never Starting or Stopping. In this case 000329.

like image 650
tom Avatar asked Jun 23 '15 08:06

tom


People also ask

How do I only display certain rows in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I exclude certain items in SQL?

The SQL EXCEPT operator is used to exclude like rows that are found in one query but not another. It returns rows that are unique to one result. To use the EXCEPT operator, both queries must return the same number of columns and those columns must be of compatible data types.


5 Answers

You can use NOT EXISTS to exclude DeviceID's that also have Starting or Stopping.

select DeviceID
from tablename t1
where not exists (select 1 from tablename t2
                  where t1.DeviceID = t2.DeviceID
                    and t2.Transmission in ('Starting','Stopping'))
  and t1.Transmission = 'Inventory'
like image 147
jarlh Avatar answered Sep 25 '22 09:09

jarlh


You can use GROUP BY with HAVING like this

Query

SELECT DeviceID
FROM DevicesTable
GROUP BY DeviceID
HAVING SUM(CASE WHEN Transmission = 'Inventory' THEN 1 ELSE 0 END) > 1
AND SUM(CASE WHEN Transmission <> 'Inventory' THEN 1 ELSE 0 END) = 0

SQL Fiddle

OUTPUT

DeviceID
000329

If you only want to check against Transmission in ('Starting','Stopping'), you can add Transmission IN ('Starting','Stopping') instead of Transmission <> 'Inventory' in the second conditional aggregation.

like image 28
ughai Avatar answered Sep 21 '22 09:09

ughai


You can select all Transmission = 'Inventory' ids and filter out those exist in Transmission in('Starting', 'Stopping'):

select distinct(DeviceID) from YourTable
WHERE Transmission = 'Inventory'
and DeviceID not in
( select distinct(DeviceID) from YourTable
  WHERE Transmission in('Starting', 'Stopping')
);

SQL Fiddle: http://sqlfiddle.com/#!9/81896/12

like image 21
coderz Avatar answered Sep 21 '22 09:09

coderz


Try this...

select *
from tablename 
where DeviceID not in 
(select DeviceID from tablename 
where Transmission in('Starting','Stopping'))
and Transmission='Inventory';
like image 28
Subhadeep Pramanik Avatar answered Sep 23 '22 09:09

Subhadeep Pramanik


Asuming you have only these three states with these names ('Inventory', 'Starting', 'Stopping') you could use:

select deviceID from table1 
group by deviceID 
having max(transmission)='Inventory'

Using max here is valid because 'Starting', 'Stopping' are alphabetically orderd after 'Inventory'.

http://sqlfiddle.com/#!9/81896/8

The result is the correct 000329.

like image 20
wumpz Avatar answered Sep 22 '22 09:09

wumpz