0

好的,所以我试图在本月之前的一个月内返回所有信息。我在格式和代码方面有点挣扎。这是我的尝试。如果需要,我可以提供表格结构

SELECT hd.Holiday_desc,
soh.Site_Name,
FROM   holiday_details hd
WHERE TRUNC(LAST_DAY(SYSDATE), 'MM' - 1) = TRUNC(hdep.Holiday_Departure, 'MM')
       INNER JOIN holiday_departure hdep 
               ON hd.holiday_code = hdep.holiday_code
Inner join site_visited sv on hd.Holiday_Code = sv.Holiday_code
inner join site_of_holiday soh on sv.Site_code = soh.Site_code
4

2 回答 2

1

我将使用该ADD_MONTHS()函数和BETWEEN运算符来完成此操作:

SELECT hd.Holiday_desc,
soh.Site_Name,
FROM   holiday_details hd
WHERE hdep.Holiday_Departure BETWEEN trunc(add_months(sysdate,-1),'MM') 
                                 AND last_day(add_months(sysdate,-1)) + .99999
       INNER JOIN holiday_departure hdep 
               ON hd.holiday_code = hdep.holiday_code
Inner join site_visited sv on hd.Holiday_Code = sv.Holiday_code
inner join site_of_holiday soh on sv.Site_code = soh.Site_code;

此外,如果您的列hdep.Holiday_Departure被索引,您将希望避免使用该TRUNC函数,这样您就不会使该列上的任何索引无效。

于 2012-11-30T00:55:57.397 回答
1

我想你想要

SELECT 
  hd.Holiday_desc,
  soh.Site_Name
FROM   holiday_details hd
       INNER JOIN holiday_departure hdep 
               ON hd.holiday_code = hdep.holiday_code
       INNER JOIN site_visited sv 
               ON hd.Holiday_Code = sv.Holiday_code
       INNER JOIN site_of_holiday soh 
               ON sv.Site_code = soh.Site_code
WHERE 
  TRUNC(hdep.Holiday_Departure, 'MM') = ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1)

您需要了解一些有关语法的知识。'MM' - 1不会起作用,就像WHERE在查询中间有一个子句一样。:) 另外,很多人在 . 之前有一个逗号FROM

于 2012-11-30T00:58:01.313 回答