How to exclude all rows with the same ID based on one record's value in psql? - sql

Say I have the results above, and want to exclude all rows with ID of 14010497 because at least one of the rows has a date of 2/25. How would I filter it down? Using a WHERE table.end_date > '2019-02-25' would still include the row with a date of 2-23

Try something like this:
select * from your_table
where id not in (
select distinct id
from your_table
where end_date > '2019-02-25'
)
/

I would use not exists:
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and t2.end_date = '2019-02-25'
);
I strongly advise using not exists over not in because it handles NULL values much more intuitively. NOT IN will return no rows at all if any value in the subquery is NULL.

Related

How to compare results from sub query in main query

I want to compare results from subquery with the column in main query
--this returns multiple rows
select id, MAX(created_date) as maxdate from table
group by id
I want to use the result set in the another query to compare date (already exist in the table) with created_date for matching id, since sub query is return multiple rows unable to use it in a sub query I get the following error More than one value was returned by a subquery..
Any help is appreciated
Something like this
select t1.id, t1.created_date, t2.maxdate
from table1 as t1
join (
select id, MAX(created_date) as maxdate from table1
group by id
) as t2 on
t1.id = t2.id

What is the quickest way in Oracle SQL to find out if one or more duplicates exist in a table?

I'm looking to create a statement that stops and returns true the very second it finds a duplicate value on a column. I don't care what the value is and simply need to know whether a duplicate exists or not; nothing else.
I know i can write Select count(*) from myTable group by primary_id having count(*) > 1; but this goes through every single row of the table, whereas I want the query to stop as soon as it encounters a single case of a duplicate existing.
The best shot i've attempted with what i know is this:-
select 1 as thingy from dual outer_qry
where exists
(
select * from
(
select some_ID,
case when COUNT(*) > 1 then 'X' else 'N' end as TRIG
from myTable
group by some_ID
)INNER_QRY
where INNER_QRY.trig = outer_qry.dummy
);
However this takes 13 seconds and I doubt it takes that long to find the first duplicate.
Can anyone please suggest where my thinking is going wrong as, hopefully from my SQL, my assumption is that the EXISTS function will be checked for every row returned for the inner_qry, but this doesn't seem to be the case.
You would use exists. This returns all the duplicates:
select t.*
from mytable t
where exists (select 1
from mytable t t2
where t2.some_id = t.some_id and t2.rowid <> t.rowid
);
In Oracle 12c, you would add fetch first 1 row only. And it can take advantage of an index on mytable(some_id).
In earlier versions:
select 1 as HasDuplicate
from (select t.*
from mytable t
where exists (select 1
from mytable t t2
where t2.some_id = t.some_id and t2.rowid <> t.rowid
)
) t
where rownum = 1;
If this returns no rows, then there are no duplicates.
select * from table1 t1 natural join table1 t2 where t1.rowid < t2.rowid;
you can use this to understand which id is dublicate
select some_ID
from myTable
group by some_ID having count(*) >1

How to get duplicate text values from SQL query

I have to get table only with duplicate text values using SQL query. I have used Having count(columnname) > 1 but I'm not getting result, only with duplicate values instead getting all values.
Can anyone suggest whether I have to add anything to my query?
Thanks.
Use the below query. mention the column which is getting duplicated in the patition by clause..
with CTE_1
AS
(SELECT *,COUNT(1) OVER(PARTITION BY LTRIM(RTRIM(REPLACE(yourDuplicateColumn,' ',''))) Order by -anycolunm- ) cnt
FROM YourTable
)
SELECT *
FROM CTE_1
WHERE cnt>1
Assuming id is a primary key
select *
from myTable t1
where exists (select 1
from myTable t2
where t2.text = t1.text and t2.id != t1.id)
You can use similar to following query:
SELECT
column1, COUNT(*)
FROM table
GROUP BY column1
HAVING COUNT(*) > 1

Using a value from one query in second query sql

SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
This produces the result
AS COUNT
5 2
I then want to use the AS value in another query and only output the end result. Is this possible.i was thinking something like.
SELECT *
FROM
TABLE 2
Where AS =(
SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
);
This is called a subquery. To be safe, you would use in instead of = (and as is a bad name for a column, because it is a SQL key word):
SELECT *
FROM TABLE2
WHERE col IN (SELECT col
FROM Table1
GROUP BY col
HAVING COUNT(col) > 1
);
Your first query is also incorrect, because the having clause goes after the group by.
You could use a subquery with the in operator:
SELECT *
FROM table2
WHERE AS IN (SELECT AS
FROM table1
GROUP BY AS
HAVING COUNT(*) > 1)

SQL Pick by Index When Using ORDER BY

I want to pick say the 10th, 20th, and 50th entry in a dataset after it has been ordered by a column. What's the best way to achieve this?
The easiest and most efficient way is to just use LIMIT/OFFSET:
SELECT * FROM MyTable ORDER BY whatever LIMIT 1 OFFSET 9
UNION ALL
SELECT * FROM MyTable ORDER BY whatever LIMIT 1 OFFSET 19
UNION ALL
SELECT * FROM MyTable ORDER BY whatever LIMIT 1 OFFSET 49
Let's assume that we have the following table:
create table Test
(
value int
);
Here is a query that returns first, third and sixth row:
select value
from
(
select value, (select count(*) + 1 from Test t2 where t2.value < t1.value) as OrderId
from Test t1
) tbl
where tbl.OrderId in (1,3,6)
You can try it here. If there are duplicates in the Test table the solution above can return more than 3 rows.
UPDATE
If you want to sort by different column than value from my example you should modify the condition t2.value < t1.value. The general form is t2.COLUMN_NAME < t1.COLUMN_NAME.