[英]Selecting an out-of-range date, column name, and timestamp from multiple tables
I have multiple tables (10+) in which I am trying to pull any dates outside a specified date range, the column name (which holds the out-of-range date), and the MODIFIED_BY of that date's row. 我有多个表(10+),其中我试图将任何日期拉到指定日期范围之外,列名(包含超出范围的日期)和该日期行的MODIFIED_BY。 Below are two example tables and the required output. 以下是两个示例表和所需的输出。 The date range I'm checking for in table A is a birthdate greater than 1990. In table B, an effective date greater than 2017 我在表A中检查的日期范围是生日,大于1990年。在表B中,有效日期是在2017年以上
TABLE_A TABLE_A
|---------------------|------------------|------------------|
| ACCT_ID | BIRTHDATE | MODIFIED_BY |
|---------------------|------------------|------------------|
| 12 | 04-MAR-96 | user1234 |
|---------------------|------------------|------------------|
| 13 | 10-MAY-79 | user9999 |
|---------------------|------------------|------------------|
TABLE_B TABLE_B
|---------------------|------------------|------------------|
| ACCT_ID | EFFECTIVE_DT | MODIFIED_BY |
|---------------------|------------------|------------------|
| 12 | 01-JAN-15 | user9876 |
|---------------------|------------------|------------------|
| 13 | 01-APR-17 | user1111 |
|---------------------|------------------|------------------|
OUTPUT 输出值
|---------------------|------------------|------------------|
| ACCT_ID | BAD_DATE | MODIFIED_BY |
|---------------------|------------------|------------------|
| 12 | 04-MAR-96 | user1234 |
|---------------------|------------------|------------------|
| 13 | 01-APR-17 | user1111 |
|---------------------|------------------|------------------|
I'm not looking for the SQL code in this instance, but rather a solution. 我不是在这种情况下寻找SQL代码,而是寻求解决方案。 I could probably accomplish this using UNION, but since not all joins are on the acct_id this could be tedious and slow when doing it for many tables. 我可能可以使用UNION完成此操作,但是由于并非所有联接都位于acct_id上,因此对许多表进行操作时可能会很乏味且缓慢。 I was able to accomplish this using two columns, one being the account ID and another called BAD_DATE in which I appended the column name and modified_by to the case return. 我能够使用两列来完成此操作,一列是帐户ID,另一列是BAD_DATE,在其中我将列名和Modifyed_by附加到案例返回中。
Any help is appreciated. 任何帮助表示赞赏。
Thanks! 谢谢!
I don't know how to do that without UNIONing those tables; 我不知道如何在不使用那些表的情况下做到这一点。 might be a tedious job, but hey - you'll do it only once. 也许是一件乏味的工作,但嘿-您只会做一次。 Here's an example: 这是一个例子:
SQL> create table table_a (acct_id number, birthdate date, modified_by varchar2(20));
Table created.
SQL> create table table_b (some_id number, effective_dt date, username varchar2(20));
Table created.
SQL> insert all
2 into table_a values (12, date '1996-03-04', 'user1234')
3 into table_a values (13, date '1979-05-10', 'user9999')
4 into table_b values (12, date '2015-01-01', 'user9876')
5 into table_b values (12, date '2017-04-01', 'user1111')
6 select * From dual;
4 rows created.
SQL> create or replace view wc as
2 select 'table_a' source, acct_id id, birthdate bad_date, modified_by from table_a union all
3 select 'table_b' source, some_id, effective_dt, username from table_b;
View created.
SQL>
SQL> select *
2 from wc
3 where (source = 'table_a' and bad_date > date '1990-01-01')
4 or (source = 'table_b' and bad_date > date '2017-01-01');
SOURCE ID BAD_DATE MODIFIED_BY
------- ---------- ---------- --------------------
table_a 12 04.03.1996 user1234
table_b 12 01.04.2017 user1111
SQL>
Found a solution! 找到了解决方案!
I am using a case statement to determine the bad dates. 我正在使用一个案例声明来确定不良日期。 If I add two additional case statements that have the same criteria, but instead have one return a string of the column name and the other return the modified_by ID, it works! 如果我添加了另外两个具有相同条件的case语句,但其中一个返回列名的字符串,而另一个返回Modifyed_by ID,则它起作用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.