[英]Oracle - Joining a inline table or table on the fly
I have a problem where a validation Code is stored in my table along with other data.我有一个问题,验证代码与其他数据一起存储在我的表中。 Unfortunately the description is stored in a table where I dont have access to.
不幸的是,描述存储在我无权访问的表中。 I do know the values
我知道价值观
Example例子
12, 14, and 22- Description for them is available in an excel. 12、14 和 22——它们的描述在 Excel 中可用。 I have no permissions to create a table to store the data and I need to run them every day as a reporting work.
我无权创建一个表来存储数据,我需要每天运行它们作为报告工作。
Question: how do I write a query where I can create a dummy table(inline? On the fly?) with the values in excel and join to my table so that results looks like this问题:我如何编写一个查询,我可以在其中创建一个虚拟表(内联?即时?)与 excel 中的值并加入我的表,以便结果看起来像这样
Note:笔记:
Any thoughts.有什么想法吗。 I tried searching.
我试着搜索。 I think I am not using the right terminology and hence not finding results.
我认为我没有使用正确的术语,因此没有找到结果。
You can do that by joining a subquery that SELECT
s each code description pair FROM dual
and uses UNION ALL
to combine their results into one set. 您可以通过加入
SELECT
的每个代码描述对FROM dual
的子查询来实现,并使用UNION ALL
将它们的结果合并到一个集合中。
SELECT t1.date,
t1.task,
t1.code,
x.description
FROM table1 t1
LEFT JOIN (SELECT 12 code,
'Missing Work' description
FROM dual
UNION ALL
SELECT 14 code,
'Absent' description
FROM dual
...) x
ON x.code = t1.code;
As you know those codes and descriptions, a simple option is to create a CTE; 如您所知,这些代码和描述,一个简单的选择是创建一个CTE; in the following example, it is called
excel
. 在以下示例中,它被称为
excel
。 Another CTE in this example represents your "real" table1
(I could have created it in my database, but CTE is good enough to show what to do). 此示例中的另一个CTE代表您的“真实”
table1
(我可以在我的数据库中创建它,但CTE足以显示要执行的操作)。
SQL> with excel (code, description) as
2 (select 12, 'Missing Work' from dual union all
3 select 14, 'Absent' from dual union all
4 select 22, 'Incomplete' from dual
5 ),
6 table1 (c_date, task, code) as
7 (select date '2019-05-30', 'Homework', 12 from dual union all
8 select date '2019-05-31', 'Labwork' , 14 from dual union all
9 select date '2019-05-30', 'Other' , 22 from dual
10 )
11 select t.c_date, t.task, t.code, e.description
12 from table1 t join excel e on e.code = t.code;
C_DATE TASK CODE DESCRIPTION
---------- -------- ---------- ------------
05/30/2019 Homework 12 Missing Work
05/31/2019 Labwork 14 Absent
05/30/2019 Other 22 Incomplete
SQL>
You can use something called Common Table Expressions (CTE) 您可以使用称为公用表表达式(CTE)的东西
WITH Code_lookup AS (
SELECT 12 Code, 'Missing Work' Description FROM DUAL UNION ALL
SELECT 14, 'Absent' FROM DUAL UNION ALL
SELECT 22, 'Incomplete' FROM DUAL)
SELECT <your columns list>
FROM <your table>
JOIN Code_lookup USING (Code)
Perhaps use a common table expression with a union: 也许使用带有union的公用表表达式:
with descr (code, description) as (
select 12, 'Missing Work' from dual union
select 14, 'Absent' from dual union
select 22, 'Incomplete' from dual
)
select table1.date, table1.task, table1.code, descr.description
from table1
left join descr on table1.code = descr.code
In Oracle there is a DECODE function that would be suitable.在 Oracle 中有一个合适的 DECODE 函数。
select code, DECODE(code, 12, 'Missing work', 14, 'Absent' 22, 'Incomplete', 'UNKNOWN Code) as Description from table;
The first argument is the field to decode, followed by pairs of values for the mapping, and a final value which is the decoded value when there is no match.第一个参数是要解码的字段,后面是映射的成对值,最后一个值是不匹配时的解码值。 You code use a CTE and distinct to turn it into a proper table if that's what's needed.
如果需要,您可以使用 CTE 和 distinct 将其转换为适当的表。 Then you would have the decode in a single place and wouldn't have to inline it everywhere.
然后,您将在一个地方进行解码,而不必在任何地方内联它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.