How to compare results from sub query in main query - sql

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

Related

Getting values based on other column

I have the following data in SQL.
NAME,DATE,REF
Pat1,2021-07-15,5072
'',NULL,5072
'',NULL,5072
'',NULL,5072
'',NULL,5072
Pat2,2021-07-15,5073
Is there a way using SELECT QUERY that we can replace the NULL values in DATE column based on the REF values?
Like replace the NULL values with the first available date for matching REF value, without making any change to the database.
Expected Result
NAME,DATE,REF
Pat1,2021-07-15,5072
'',2021-07-15,5072
'',2021-07-15,5072
'',2021-07-15,5072
'',2021-07-15,5072
Pat2,2021-07-15,5073
You can do it with MAX() window function:
SELECT NAME,
COALESCE(DATE, MAX(DATE) OVER (PARTITION BY REF)) DATE,
REF
FROM tablename
See the demo.
Have a derived table (the subquery) to return each ref's date. JOIN:
select t1.NAME, t2.date, t1.REF
from tablename t1
join (select ref, max(date) date
from tablename
group by ref) t2
on t1.ref = t2.ref

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

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.

How to get Full Record with MAX as aggregate function

I have a table with schema (id, date, value, source, ticker). I wanted to get record having highest ID group by date in sql server
Example Data
ID|date|value|source|ticker
3|10-Dec-2017|10|a|b
1|10-Dec-2017|11|p|q
Below query works in Sqlite. Do we know if I can do same with SqlServer
select max(id), date, value, source, ticker from table group by date
Expected return:-
ID|date|value|source|ticker
3|10-Dec-2017|10|a|b
Also how I can do same operation on UNION of 2 tables with same schema.
You can use subquery :
select t.*
from table t
where id = (select max(t1.id) from table t1 where t1.date = t.date);
However, you can also use row_number() function :
select top (1) with ties *
from table t
order by row_number() over (partition by [date] order by id desc);
You can also do it like below :
select t1.* from table1 t1
join (
select max(id) as id, [date] from table1
group by [date]
) as t2 on t1.id = t2.id
SQL HERE

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

reuse table alias in another select

I have a sql statement:
select id from table1 t1, table t2
where.....
order by ( select count(owner_id) from t2) ASC;
What I want to do here is to select the id of the item whose owner has least number of items.
Is this possible? If not, what I can do to achieve to goal?
Thanks in advance!
You don't mention what SQL you're using but you can do this, or something similar, in PL ( and My I believe ); I'm assuming you're linking table 1 and 2 on id; I haven't ordered by the count(owner_id) alone as this will always be the same value. Obviously partition by whatever you want to get the correct count you're after.
select id
from ( select t1.id, t2.ct
from table1 t1
, ( select id, count(owner_id) over ( partition by id ) as ct
from table2 ) t2
where t1.id = t2.id
order by t2.ct ASC )
;