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

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.

Related

Get rows in sequence from multiple table in SQL Server

I want to get all rows in sequence from 2 tables in SQL Server.
Output should be 1st row from 1st table, then 1st row from 2nd table,
2nd row from 1st table, 2nd row from 2nd table....etc
What #eshirvana suggested will not get you the desired. Instead, it'll be table1.row1, table2.row1, table2.row2, table1.row2
You can use UNION to join data from two tables when the column names and types match. I'm making an assumption on how to order the data based on your desired outcome.
SELECT RowID, Row, z
FROM table1
UNION
SELECT *
FROM table2
ORDER BY z, RowID
Here's the working code:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=068c0fd2056cc48718345e85b74b7bba
probably something like that :
select * from
(
select rowID,Row,z from table1
union all
select rowID,Row,z from table2
) alltables
order by z
You can try with below approach:
SELECT * FROM
(
SELECT RowId,Row,Z,1 AS TableOrder From Table1
UNION ALL
SELECT RowId,Row,z,2 AS TableOrder From Table2
)
ORDER BY Z,TableOrder

Order two parts of a union independently of one another

Is it possible to do something like this:
select name from table1 order by name
union
select name from table2 order by name
I know I can do this:
select name from table1
union
select name from table2 order by name
However, I want the names from table1 to appear first. I have spent the last hour Googling this and I have go nowhere. For example, I have looked here: How to order by with union in SQL?
The query needs to be a bit more complicated:
select name
from ((select distinct name, 1 as is_1 from table1)
union
(select distinct name, 0 from table2)
) n
group by name
order by max(is_1), name;
This uses select distinct in the subqueries because that can take advantage of an index on name.
Add a "sort" field and put the union inside a subquery so you can sort after the union.
untested
select a.name
from (
select name, 1 sort
from table1
union all
select name, 2 sort
from table2
) a
order by a.sort, a.name
I changed it to union all to make it clear this approach won't do a union. You could also select the sort column if you want to see it. If you don't want duplicate names, then this approach won't work.
You need another column to sort on. UNION does not allow the individual queries to have an ORDER BY clause.
Adding in a column to sort on before name allows for it to sort the individual result sets. See my example below:
CREATE TABLE #Table1 (Name VARCHAR(50))
CREATE TABLE #Table2 (Name VARCHAR(50))
INSERT INTO #Table1 VALUES ('Bart'), ('Lisa'), ('Maggie')
INSERT INTO #Table2 VALUES ('Chris'), ('Meg'), ('Stewie')
SELECT Name, 0 AS Sort FROM #Table1
UNION
SELECT Name, 1 AS Sort FROM #Table2
ORDER BY Sort, Name

when joining two tables using union how to use order by clause for two tables seperatly in sql

i am creating two tables with same type of attributes i have to join these tables using union in such a way that the resultant union set should have 1st table in ascending order and 2nd table in descending order
I think you want something like this:
select t.*
from ((select t1.*, 1 as which
from t1
) union all
(select t2.*, 2 as which
from t2
)
) t
order by which,
(case when which = 1 then col end) asc,
(case when which = 2 then col end) desc;

Combine 2 result sets in SQL?

How do I combine the resultsets to return a single result in SQL? For example -
SELECT * FROM Table1
SELECT * FROM Table2
I want to combine the two resultsets with the columns from the second resultset appended to the first.
Table 1 and Table 2 are not related to each other in any way. If Table 1 has 2 columns and Table 2 has 4 columns, I wanted 6 columns returned total in a single resultset. And if Table 1 has 4 rows and Table 2 has only 2 rows, I want NULLS in Table 2 rows for 3rd and 4th row.
Is it possible?
Edit: I do not know how many columns are present in Table1 and Table2, so cannot use UNION with nulls.
If your RDBMS supports ROW_NUMBER() you could do something like this.
WITH T1 AS
(
SELECT *, ROW_NUMBER() OVER ( ORDER BY T1id) AS RN1 FROM Table1
),
T2 AS
(
SELECT *, ROW_NUMBER() OVER ( ORDER BY T2id) AS RN2 FROM Table2
)
SELECT * FROM T1 FULL OUTER JOIN T2 ON RN1 =RN2
It's possible but it's probably quite a bad idea to do this. Why not just run two queries?
If you really want to do it, join the two result sets on a ROW_NUMBER() field.
Not a general solution, but works if you know your schema:
select a1, a2, null as b1, null as b2 from table1
union
select null as a1, null as a2, b1, b2 from table2

How to add 2 temporary tables together

If I am creating temporary tables, that have 2 columns. id and score. I want to to add them together.
The way I want to add them is if they each contain the same id then I do not want to duplicate the id but instead add the scores together.
if I have 2 temp tables called t1 and t2
and t1 had:
id 3 score 4
id 6 score 7
and t2 had:
id 3 score 5
id 5 score 2
I would end up with a new temp table containing:
id 3 score 9
id 5 score 2
id 6 score 7
The reason I want to do this is, I am trying to build a product search. I have a few algorithms I want to use, 1 using fulltext another not. And I want to use both algorithms so I want to create a temporary table based on algorithm1 and a temp table based on algorithm2. Then combine them.
How about:
SELECT id, SUM(score) AS score FROM (
SELECT id, score FROM t1
UNION ALL
SELECT id, score FROM t2
) t3
GROUP BY id
This is untested but you should be able to perform a union on the two tables and then perform a select on the results, grouping the fields and adding the scores
SELECT id,SUM(score) FROM
(
SELECT id,score FROM t1
UNION ALL
SELECT id,score FROM t2
) joined
GROUP BY id
Perform a full outer join on the ID. Select on the ID and the sum of the two "score" columns after coalescing the values to 0.
SELECT id, SUM(score) FROM
(
SELECT id, score FROM #t1
UNION ALL
SELECT id, score FROM #t2
) AS Temp
GROUP BY id
select id, sum(score)
from (
select * from table 1
union all
select * from table2
) tables
group by id
You need to create an union of those two tables then You can easily group the results.
SELECT id, sum(score) FROM
(
SELECT id, score FROM t1
UNION
SELECT id, score FROM t2
) as tmp
GROUP BY id;