How to Compare Date between 2 different Tables - sql

How can I get LastAmount and CurrentAmount from a table which has latest Date between this two tables? For example, I want to get value from 2016 March, but the result I need is a value from the latest date.

SELECT TOP 1
*
FROM (
(SELECT * FROM TABLE1)
UNION ALL
(SELECT * FROM TABLE2)
) AS T WHERE monthCondition
ORDER BY T.CreatedData DESC

Related

Finding the most recent records from duplicates

I'm trying to get the most recent records from a table where there are duplicates for each row.
Every month a new row for some IDs is getting added to the table, but some other records might not have a new row each month so the data will be like this
ID Date
1 8/30/2022
1 7/30/2022
3 8/30/2022
3 7/30/2022
3 6/30/2022
4 1/11/2021
The query result should be
ID Date
1 8/30/2022
3 8/30/2022
4 1/11/2021
I tried to use a sub-query, but it is only returning records that actually has the most recent for the whole table not per ID so it is only returning those who has a record in 8/30/2022.
This is my query
create table test as (
select * from table1 inner join
(select EmpID, max(Record_Date) as maxdate
from table1 group by EmpID) ms
on table1.EmpID ms.EmpID and Record_Date=maxdate)
WITH DATA;
You may use NOT EXISTS operator with correlated subquery as the following:
SELECT T.ID, T.Date
FROM Table1 T
WHERE NOT EXISTS(SELECT * FROM Table1 D WHERE D.ID=T.ID AND D.Date>T.Date)
And of course, if you want to create a new table from this statement the query will be:
CREATE TABLE test AS
(
SELECT T.ID, T.Date
FROM Table1 T
WHERE NOT EXISTS(SELECT * FROM Table1 D WHERE D.ID=T.ID AND D.Date>T.Date)
) WITH DATA;

Get the most recently used date from two different tables with two different named columns

I've two tables say table 1 and table 2,
table 1 has column names as sno and useddate
table 2 has column names as sno and recentlyuseddate
I want to compare these two columns useddate and recentlyusedate and get the most recently used date.
These tables may have many entries but I need only ONE ENTRY (mostrecent) date after comparing these tables.
SELECT MAX(useddate) AS mostrecent
FROM
(SELECT useddate FROM Table1
UNION ALL
SELECT recentlyuseddate AS useddate FROM Table2) TheUnion
You can use unions for this
SELECT MAX(col)
FROM (SELECT col FROM TABLE_1
UNION ALL
SELECT col FROM TABLE_2)
If you have an index on the two dates in the two tables, I would go for:
select top 1 sno, useddate, which
from ((select top 1 sno, useddate, 'table1' as which from table1 order by useddate desc) union all
(select top 1 sno, recentlyuseddate , 'table2' as which from table2 order by recentlyuseddate desc)
)
order by useddate desc;
This version also tells you which table the date came from.

How to return second newest record in SQL?

If this:
SELECT *
FROM Table
WHERE Date=( SELECT MAX(Date)
FROM Table
)
returns newest record from the table, how to get second newest record?
SELECT *
FROM Table
WHERE Date = ( SELECT MAX(Date)
FROM Table
WHERE Date < ( SELECT MAX(Date)
FROM Table
)
) ;
or:
SELECT TOP (1) *
FROM Table
WHERE Date < ( SELECT MAX(Date)
FROM Table
)
ORDER BY Date DESC ;
or:
SELECT *
FROM
( SELECT t.*
, ROW_NUMBER() OVER(ORDER BY Date DESC) AS RowNumber
FROM Table t
) AS tmp
WHERE RowNumber = 2 ;
If the Date column has unique values, all three queries will give the same result. If the column can have duplicate dates, then they may give different results (when there are ties in 1st or 2nd place). The first query will even give multiple rows in the result if there are ties in 2nd place.
"select TOP (1) *
from Table
WHERE Date<(SELECT MAX(Date) FROM Table)
ORDER BY Date DESC"
should do the trick.
Please check this code.
SELECT * FROM category
WHERE Created_Time <(
SELECT MAX(Created_Time)
FROM category
)
ORDER BY Created_Time DESC
LIMIT 1
Prasad.
select second last date in sql:
SELECT MAX(YourDateColumn) FROM YourTable where ColumnId=2 and YourDateColumn <
(SELECT MAX(YourDateColumn) FROM YourTable where ColumnId=2)
hope helps someone.
Try this
SELECT *
FROM Table
WHERE Date = ( SELECT MAX(Date) FROM Table
WHERE Date < ( SELECT MAX(Date) FROM Table)
) ;

SQL Sum of 3 counts (different month clause) from same table

I have tried this a few different ways and it never seems to work:
SUM(
Select count(*) from table1 where month = 1 +
Select count(*) from table1 where month = 2 +
Select count(*) from table1 where month = 3
)
What is the proper way to do it? SUM(IF(case)) also didn't work.
Will this do what you want?
SELECT
COUNT(*)
FROM
Table1
WHERE month in (1,2,3)

SQL two tables with different month values. Getting one result set for all the months

If I have two tables like this:
Table1
Month
1
3
Table 2
Month
1
4
How do I get the following result set?
Result Set
Month
1
3
4
Use the UNION operator like this:
SELECT Month FROM Table1
UNION
SELECT Month FROM Table2
The SQL UNION operator combines two or more SELECT statements and produces a single result with unique values.
SELECT [Month]
FROM Table1
UNION
SELECT [Month]
FROM Table2
You will want to use a UNION to combine the results into a single set.
SELECT Month
FROM Table1
UNION
SELECT Month
FROM Table2
By default UNION only grabs distinct values, therefore your exact result. If you wanted duplicate values you could use UNION ALL which would show month 1 in your example twice.
Here is a good tutorial that shows the differences as well.
Use UNION:
SELECT month FROM table1 UNION SELECT month FROM table2
SELECT Month FROM Table 1 UNION Select Month FROM Table 2 should do the trick
Here is my solution
declare # temptable table( months varchar(10) )
inserto into #temptable(months)
select distinct Month from Table1
inserto into #temptable(months)
select distinct Month from Table2
select disticnt * from #temptable
Maybe there is a better way?