简体   繁体   English

Teradata SQL-联合和where子句

[英]Teradata SQL - Union and Where Clause

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. 有一个Teradata SQL UNION查询,并且需要知道如何从2个表中提取数据,其中UNION第一部分的日期早于UNION查询第二部分的日期。 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. 只想查看date2在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; 从“医院”表中选择Pat_Name,提供者,Clinic_Encounter_Date UNION从“医院”表中选择Pat_Name,提供者,Hosp_Encounter_Date,其中Hosp_Encounter_Date <Clinic_Encounter_Date;

My SQL query runs fine, but when I add the WHERE clause I keep getting an error message. 我的SQL查询运行正常,但是当我添加WHERE子句时,我不断收到错误消息。 Teradata just doesn't like me!!! Teradata只是不喜欢我!

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. UNION之前和之后的SQL是独立的SQL,因此在第二条语句中,它无法识别date1。 ie 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM