I have two different tables that contain one value each. For example:
table1 table2
|Hours_worked| |Work_Completed|
250 346
I want my query to subtract Hours_worked from Work_completed. I tried this:
SELECT (SELECT Hours_worked FROM table1)- (SELECT Work_Completed FROM table2) AS Diff
I get the error: "Query input must contain at least one table or query"
Any help would be great, thanks
I think it would just be:
select Hours_worked - Work_Completed as diff
from table1, table2
Just add FROM dual, i mean:
SELECT
(SELECT Hours_worked FROM table1) - (SELECT Work_Completed FROM table2) AS Diff
FROM DUAL
Related
I have table1 with the following data :
ID
Name
Date
1
Paul
01-11-2020
1
Paul
03-11-2020
and have table2 only with a Date column:
Date
02-11-2020
I want to get output from those tables as:
ID
Name
Date
1
Paul
01-11-2020
1
Paul
02-11-2020
1
Paul
03-11-2020
Could someone help me how to join two tables to get the output like above. I tried so many ways but I couldn't solve this issue. Thank you...
You can use CROSS JOIN after applying UNION ALL in order to return from the both tables in a row-wise manner such as
SELECT DISTINCT COALESCE(tt.ID,t1.ID) AS ID,
COALESCE(tt.Name,t1.Name) AS Name, tt.Date
FROM table1 AS t1
CROSS JOIN (SELECT * FROM table1 UNION ALL
SELECT null,null,Date FROM table2) AS tt
ORDER BY 3
Demo
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
I want to do a query that inserts the result of table1 with table2 union's result. But when I try on beeline:
insert into table table3
select * from (
select * from table1 t1
where
h_time > '2019-05-01 00:00:00'
and t1.id in (select id from table4)
union all
select * from table2 t2
where
h_time > '2019-05-01 00:00:00'
and t2.id not in (select id from table4)
);
Consider that the both tables 1 and 2 have the same column numbers and datatypes have already fixed previously.
The result in table3 is only the rows of table1. And when I change the position of table 1 and 2, I get only the rows of table2.
Anyone have a guess what's happening?
Tks in advance!
it may be problem with table too, it may not show the actual count sometimes, use select * from tablename. If the count is as expected the run Analyze statement it'll re-compute and repair the statistics
It gives me error when i try to join two tables.Both tables has same number of columns
Here is the error
failed to find conversion function from unknown to text
select * from table1
union
select * from table 2;
When you use UNION columns need the same type as the same position.
use clear column name instead of *, Because we can't predict the number of columns after table1 or table2 is different.
select col1,col2...
from table1
union
select col1,col2...
from table2;
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.