【问题标题】:SQL Query in Oracle SQL DeveloperOracle SQL Developer 中的 SQL 查询
【发布时间】:2013-06-20 09:50:29
【问题描述】:

我有四个表要加入并一起显示输出。我不确定 Oracle SQL Developer 的语法是如何工作的。我知道这对程序员来说是一个简单的问题,我希望有人可以就代码的外观提出建议。

表格是表格是:JNL1 JNL2 JNL3 JNL4

JNL1
JNL2
JNL3
JNL4

我只想使用 UserCode=Automation 显示结果,并且日期比当前日期早一天。代码是:

  UserCode = 'Automation' AND     
  CONVERT(VARCHAR,LEFT(DATE,8))=CONVERT(VARCHAR(8),GETDATE()-1,112)

这四个表的通用键是 ItemID、Date 和 UserCode。

我不确定查询会是什么样子,我可以将连接表查询和 UserCode/Date 查询生成为单独的查询。但是,当我将它们组合到一个查询中时,我要么在不明确的列名上出现错误,要么在运行查询时没有从数据库中显示任何数据。

【问题讨论】:

  • 您能否提供示例数据、预期结果以及您尝试过的代码?此外,查询标记为“oracle”,但在示例中使用的是 SQL Server 语法。

标签: sql oracle date join


【解决方案1】:

不要加入表格。 由于您只需要从一个表中获取数据并且表结构相同,因此您可以拥有多个unions

-- 在 oracle 数据库中,你不能将列名作为日期,我假设 date 列名是 c_date-

select ItemID, c_Date, and UserCode
from Jul1
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul2
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul3
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1
union all 
select ItemID, c_Date, and UserCode
from Jul4
where UserCode = 'Automation' 
  AND c_date = trunc(sysdate) -1

*sysdate 是系统函数,它将返回当前日期和时间值。

【讨论】:

  • 如果我想要每个表中的所有列,而不仅仅是 ItemID、c_Date 和 UserCode,代码会是什么样子?
  • @KJ .. 请告诉.. 您如何要求所有表格中的所有列?因为您的表名表明这些表存储了各个日期的数据..而您只想获取昨天的数据..在这种情况下,您将如何从其他三个表中获取数据?
猜你喜欢
  • 2021-04-12
  • 2021-03-31
  • 2012-06-14
  • 2012-12-30
  • 1970-01-01
  • 2021-01-15
  • 2022-10-13
  • 1970-01-01
  • 2015-01-07
相关资源
最近更新 更多