Teradata SQL - Union and Where Clause - sql

have a Teradata SQL UNION query and need to know how I can extract data from 2 tables where date in first part of UNION is before the date in the second part of the UNION query. Example:
Select
name1,date1
from table1
UNION
Select
name2,date2
from table2
where date2 < date1
Would like to see only records where date2 is before date1. This is just an example of what I need to retrieve from my data.
Select
Pat_Name, Provider, Clinic_Encounter_Date
from Clinic table
UNION
Pat_Name, Provider, Hosp_Encounter_Date
from Hospital table
where
Hosp_Encounter_Date < Clinic_Encounter_Date;
My SQL query runs fine, but when I add the WHERE clause I keep getting an error message. Teradata just doesn't like me!!!
Hopefully, this is a bit more helpful. :-)
Thank you in advance for your assistance.

The SQLs in before and after the UNION are independent SQLs, so in the second statement it is not recognizing date1. i.e. the following statement is not valid.
Select name2,date2 from table2 where date2 < date1
You will need to change your statement to
Select
name1,date1
from table1
UNION
Select
name2,date2
from table2
where date2 < (select Min(date1) from table1)

Related

How to check the Data gap in the query

Scenario: in the below scenario inserted date is missing for 4/21/2021
for the latest record should check whether it has 4/20 if not then count as 1
the data starting from 4/14 to 4/20 we dont find any gap - Please help on this query
Let me know if more info need assume table name is TABA
One typical way you would handle this would be to use a calendar table, containing all dates which your data might cover. In lieu of a calendar table, we can inline one like this:
SELECT d.Date
FROM
(
SELECT '2021-04-14' AS Date UNION ALL
SELECT '2021-04-15' UNION ALL
SELECT '2021-04-16' UNION ALL
SELECT '2021-04-17' UNION ALL
SELECT '2021-04-18' UNION ALL
SELECT '2021-04-19' UNION ALL
SELECT '2021-04-20' UNION ALL
SELECT '2021-04-21' UNION ALL
SELECT '2021-04-22'
) d
LEFT JOIN yourTable t
ON t.InsertedDate = d.Date
WHERE
t.InsertedDate IS NULL;

SQL subtracting values from two different tables in Access

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

It is possible to do a SELECT MIN(DATE) searching in 2 or more columns?

I have a table which have 2 columns of dates, and I want to look for the closest date in the future (Select min()?) searching in both columns, is that possible?
For example, if I have column1=23/11/2014 and column2=22/11/2014, in the same row, I want get 22/11/2014
I hope it is clear enough, ask me if it doesn't.
Greetings.
In a single table use CASE
SELECT CASE column1 < column2
THEN column1
ELSE column2
END mindate
FROM yourtable
If you have the date column in multiple tables, just replace yourtable with your tables JOINed together
This is what the least() function is for:
select least(column_1, column_2)
from your_table;
min() is an aggregate function that operates on a single column but multiple rows.
try something like this:
select min(date) from(
select a.date date from table1 a
union
select b.date2 date from table1 b
)

Oracle Select query with dynamic table name

I m trying to create a complicated select query which uses temp tables and syntax as below
with as
table1
( select start_date, end_date ....somequery - returns only 1 row ),
table2
( select ... somequery use table1 columns in where clause.. ),
table3
( select ... use table1 columns in where clause .. )
select * from
select case when ( start_date < sysdate -1000 and end_date > sysdate ) then 'table2' else 'table3' end from table1
where rownum < 10
So logic is simple based on return value from table1 i may want to query table 2 or may want to query table3
Problem: Oracle doesnt allow table name to be dynamically generated in a sql query
I know i can write a procedure and use EXECUTE IMMEDIATE but for some reason i have to do everything via a single query. Any help will be greatly appreciated.
You can do something like this for your main select:
select *
from table1 cross join
table2
where start_date < sysdate - 1000 and end_date > sysdate and rownum < 10
union all
select *
from table1 cross join
table3
where not (start_date < sysdate - 1000 and end_date > sysdate) and rownum < 10
The idea is to use union all for the two conditions and where to guarantee that no rows are returned subject to your conditions. Note that this form of the where statement do not take into account NULL values for start_date or end_date.
I suspect that there might be another way to write this query, if more details about table2 and table3` were available.

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?