How can I select unique data in SQL?

Asked

Viewed 358 times

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:

Filtros únicos

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');
  • 1

    @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?

2 answers

3


SELECT DISTINCT EX.GSM, EX.MOTIVO_ENVIO, EX.STATUS
FROM EXPORT EX 
WHERE EX.STATUS = 'ANOMALIA TEMPORÁRIA'
AND EXISTS (
           SELECT TMP.GSM 
           FROM EXPORT TMP 
           WHERE TMP.GSM = EX.GSM 
           GROUP BY TMP.GSM 
           HAVING COUNT(TMP.GSM) = 1)

This way, you display all GSM results that are unique (the subquery uses HAVING COUNT to ensure this). The link between the main query and the subquery is made by GSM, using the alias I used in the queries. You can understand better around here: http://www.postgresqltutorial.com/postgresql-exists/

  • muitooooooo thanks, you literally saved my lives rsrs, vlw even saw, by the attention, it worked cool here, I have another filter to be made you could help me?

2

Having does what you need.

This link can help you

SELECT
 column_1,
 aggregate_function (column_2)
FROM
 tbl_name
GROUP BY
 column_1
HAVING
 condition;
  • I saw it yesterday bro, but I’m beginner with Database, I did so: select gsm, status from export Where status = 'TEMPORARY ANOMALY' group by 1, 2 having Count(gsm) = 1 order by 1; But I don’t know if this is right, it even returns me 234 data, but it is difficult to know because the spreadsheet has more than 3000 lines

Browser other questions tagged

You are not signed in. Login or sign up in order to post.