3
Hello would you like to know how can I select unique data in SQL? I have an Excel spreadsheet that has the daods that I need to extract information by applying the following filter in sql:
- GSM which does not recur (that is, which is unique), and which has the STATUS = 'TEMPORARY ANOMALY'
I tried with the DISTINCT:
SELECT DISTINCT [GSM], [MOTIVO_ENVIO], [STATUS]
FROM export WHERE [STATUS] = 'ANOMALIA TEMPORÁRIA'
But this does not solve my problem, because DISTINCT for example if it has 5 repeated data it will bring only 1 of those 5, and that is not what I need, I only need GSM that do not repeat themselves, or that are unique.
I also tried using GROUP BY, HAVING:
select gsm, status from export
where status = 'ANOMALIA TEMPORÁRIA'
group by 1, 2
having count(gsm) = 1
order by 1;
But don’t return me the way I need, someone could help me?
Example:
In the example above, I have some data, among them the ones I need are in bold and underlined, because their GSM are unique (not repeated) and have the STATUS = 'TEMPORARY ANOMALY'
I will go here to create this table, and some data to have how to test:
create table export (
gsm bigint,
status varchar
);
insert into export values
('3154321575', 'ANOMALIA TEMPORÁRIA'),
('3154321575', 'PENDENTE'),
('3101234567', 'ANOMALIA TEMPORÁRIA'),
('2198465289', 'PENDENTE'),
('3274185296', 'ANOMALIA TEMPORÁRIA'),
('3274185296', 'ANOMALIA TEMPORÁRIA'),
('3274185296', 'ANOMALIA TEMPORÁRIA'),
('3140028926', 'LIBERADO'),
('3140028926', 'PENDENTE'),
('3176543210', 'ANOMALIA TEMPORÁRIA');
@Bacco edited the tag, thanks, so I tried to reproduce what I needed and return me some duplicated GSM, I need ONLY those that are unique and that have STATUS = 'TEMPORARY ANOMALY' the new query looked like this: select gsm, status from export Where status = 'TEMPORARY ANOMALY' group by 1, 2 having Count(gsm) > 1 order by 1; But it happened that she picked up some duplicate GSM that she has in both STATUS = 'TEMPORARY ANOMALY' and that can’t happen, could help me friend?
– Magno