English 中文(简体)
1. 如果在哪一条款中作出声明,则以书面形式提出。
原标题:Oracle if statement in where clause

我有以下疑问:

SELECT * FROM(
  SELECT
       CASE
            WHEN TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),  MM ) =  10  
            THEN
                TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),  MM )
            WHEN SUBSTR (TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),  MM ), 1, 1) =  0  
            THEN
               SUBSTR (TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),  MM ), 2, 1)
            ELSE
               SUBSTR (TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),  MM ), 1, 2) 
         END
            AS CUR_MONTH_FRMTD,
       P.OPR_DATE,
       P.INDICE,
       P.LOAD_TYPE,
       P.CONTRACT_MONTH,
       P.CONTRACT_YEAR,
       P.VALUE,
       P.DATE_INSERTED,
       P.DATE_UPDATED
  FROM ZE_DATA.PWX_FWD_CURVE P ) Q
  WHERE
  Q.LOAD_TYPE =  HLH 
  AND Q.INDICE =  MidC 
  AND Q.CONTRACT_YEAR = TO_CHAR(:DATEINPUT,  YYYY ) 
  AND Q.CONTRACT_MONTH = Q.CUR_MONTH_FRMTD
  AND TO_CHAR(Q.DATE_INSERTED,  MM ) = TO_CHAR(:DATEINPUT,  MM )
  AND TO_CHAR(Q.DATE_INSERTED,  YY ) = TO_CHAR(:DATEINPUT,  YY )
  AND TO_CHAR(Q.DATE_INSERTED,  DD ) = TO_CHAR(:DATEINPUT,  DD )

它努力将记录缩小到插入日,只按要求归还一个记录。 但问题是,星期六或星期天没有列入任何记录,从而打破过滤的有用性。

星期六或星期日 我需要获得星期五的数据。 我认为,以下这样的逻辑将会奏效。 但这不能在单体内进行。

  IF TO_CHAR(:DATEINPUT,  DAY ) =  SATURDAY  THEN
  WHERE
  ....
  AND TO_CHAR(Q.DATE_INSERTED,  DD ) = TO_CHAR(:DATEINPUT-1,  DD )
  ELSE IF TO_CHAR(:DATEINPUT,  DAY ) =  SUNDAY  THEN
  WHERE
  ....
  AND TO_CHAR(Q.DATE_INSERTED,  DD ) = TO_CHAR(:DATEINPUT-2,  DD ) 

现在,我需要作一个选择发言。 能够使用储存的程序。 谁会想解决办法或工作?

最佳回答

不要说“国际自由论坛”为什么不仅如此nes或如此。

Where ...
  AND 
    (
        (TO_CHAR(:DATEINPUT,  DAY ) =  SATURDAY  AND TO_CHAR(Q.DATE_INSERTED,  DD ) = TO_CHAR(:DATEINPUT-1,  DD ))
        OR 
        (TO_CHAR(:DATEINPUT,  DAY ) =  SUNDAY  AND TO_CHAR(Q.DATE_INSERTED,  DD ) = TO_CHAR(:DATEINPUT-2,  DD ))
    )
    AND ...
问题回答

如果我理解这一要求,如何做到这一点:

where  q.date_inserted =
       case to_char(:dateinput, fmDy , nls_date_language = English )
           when  Sat  then :dateinput -1
           when  Sun  then :dateinput -2
           else :dateinput
       end;

(如果日期栏和参数能够包含时间,则会稍加复杂。) 如果日期栏按指数或分门槛值计算,那么你只能将<条码>trunc(>围绕所有事项;如果是的话,可能应当建立<条码>>>> <>>>。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签