如何在Oracle中找到特定值的精确列名称
我有一个报告要求,该报告应包含匹配以下标准的行(销售)中具有类似列名称的以下条件的行,例如
CUST_ID, DEPT,CODE1_ID,CODE2_ID,CODE3_ID,CODE1_ID_STOP_DT,CODE2_ID_STOP_DT,CODE3_ID_STOP_DT
示例数据:
CUST_ID, DEPT,CODE1_ID,CODE2_ID,CODE3_ID,CODE1_ID_STOP_DT,CODE2_ID_STOP_DT,CODE3_ID_STOP_DT.
123, 11, 10, 20, 100, NULL, 31-OCT-2022, 31-DEC-2022
234, 21, NULL, 100, 30, NULL, NULL, 31-DEC-2022
345, 11, 100, 30, NULL, NULL, NULL, 31-JUL-2022
456, 31, 10, 60, 100, 01-JAN-2023, NULL, 31-OCT-2022
fetch行的标准:代码为100,stop_dt不是31-dec-dec-2022或无效。 我能够用以下代码100获取行。
select * from SALES where '100' in (nvl(CODE1_id,0), nvl(CODE2_id,0),nvl(CODE3_id,0);
但是我无法编写SQL来获取代码的行*_id_stop_dt列 该值为null或不是31-DEC-2022。请帮忙。提前致谢!
I have a report requirement where the report should contain the rows that match the below criteria in a table (SALES) that have similar column names like
CUST_ID, DEPT,CODE1_ID,CODE2_ID,CODE3_ID,CODE1_ID_STOP_DT,CODE2_ID_STOP_DT,CODE3_ID_STOP_DT
Sample data:
CUST_ID, DEPT,CODE1_ID,CODE2_ID,CODE3_ID,CODE1_ID_STOP_DT,CODE2_ID_STOP_DT,CODE3_ID_STOP_DT.
123, 11, 10, 20, 100, NULL, 31-OCT-2022, 31-DEC-2022
234, 21, NULL, 100, 30, NULL, NULL, 31-DEC-2022
345, 11, 100, 30, NULL, NULL, NULL, 31-JUL-2022
456, 31, 10, 60, 100, 01-JAN-2023, NULL, 31-OCT-2022
Criteria to fetch rows: where CODE is 100 and STOP_DT is not 31-DEC-2022 or null.
I am able to fetch rows with code 100 as below.
select * from SALES where '100' in (nvl(CODE1_id,0), nvl(CODE2_id,0),nvl(CODE3_id,0);
But I am not able to write the SQL to fetch the rows of the CODE*_ID_STOP_DT columns
where the value is NULL or not 31-DEC-2022. Please help. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照您的ID条件为100,您可以尝试使用涉及日期的同一逻辑。这是一种可能将零值从日期转换为日期(任何日期)不等于31-Dec-2022。的一种可能的解决方案
。被排除在外。
带有子句只是为了创建示例数据,而不是答案的一部分。
问候...
Following your condition for IDs with value of 100, you can try the same logic with dates involved. Here is the one possible solution with converting null values from dates into a date (any date) not equal to 31-DEC-2022.
This way the dates with value of 31-DEC-2022 or NULL will be excluded.
The WITH clause is here just to create sample data and is not part of the answer.
Regards...