简体   繁体   中英

oracle sql developer(4.0.0.12) returns wrong date

It seems that a query of mine returns wrong results, and I'm not sure why. I don't yet rule out the possibility that the SQL is actually doing something else then what I expect/want it to do since I haven't used SQL for a time now.

I post it here because I'm kind a stuck with the why it returns wrong results sometimes.

The error is in the MIN(FIRM.account_recharge.X__INSDATE) (or at least the ones I noticed)

SELECT
 FIRM.customer.CUSTOMER_ID,
 FIRM.customer.CORPORATION,
 FIRM.customer.CUSTOMER_NAME_PREFIX,
 FIRM.customer.CUSTOMER_NAME,
 FIRM.account.ACCOUNT_TYPE,
 FIRM.account.ACCOUNT_TYPE,
 FIRM.customer.LANGUAGE,
 FIRM.customer.VALIDATED,
 FIRM.account.X__INSDATE,
 SUM(FIRM.account_recharge.GROSS_VALUE) SUM_FELTOLTESEK,
 MIN(FIRM.account_recharge.X__INSDATE),                                           
 INNER JOIN FIRM.account
 ON FIRM.customer.CUSTOMER_ID = FIRM.account.CUSTOMER
 INNER JOIN FIRM.customer_address
 ON FIRM.account.CUSTOMER = FIRM.customer_address.CUSTOMER          
 INNER JOIN FIRM.account_recharge
 ON FIRM.account.ACCOUNT_ID = FIRM.account_recharge.ACCOUNT         
 GROUP BY FIRM.customer.CUSTOMER_ID,
 FIRM.account.X_INSDATE,
 FIRM.customer.CORPORATION,
 etc,etc
 HAVING MIN(FIRM.account_recharge.X__INSDATE) BETWEEN to_date('2014-JAN. -01','YYYY-MON-DD') AND to_date('2014-DEC. -31', 'YYYY-MON-DD');

This code should return information abut our customers, their sum account 'recharging'/'replenishing'/'paying in' , sorry not sure of what word to use here. and their first payment/money upload to their account in 2014. Yet sometimes the return values seems to just ignore the actual first time our client paid in money, and shows the second or third date. (my random manual check returned that around 1/10 of the time the returned values are wrong.)

A costumer of ours can have more the one account linked to him. I'm using Oracle SQL developer (4.0.0.12) please ask if you would like to know anything else about this pickle im in.

Otherwise It seems to work nicely, but if you have any other tuning tip, I would be glad to hear them.

Thank you for your help!

HAVING MIN(FIRM.account_recharge.X_INSDATE) BETWEEN '14-JAN. -01' AND '14-DEC. -31'

This is definitely incorrect. You are comparing dates. so, you must convert the string literal explicitly into a date using TO_DATE and proper format mask .

For example,

HAVING MIN(FIRM.account_recharge.X_INSDATE) 
   BETWEEN to_date('2014-JAN-01','YYYY-MON-DD') 
   AND     to_date('2014-DEC-31', 'YYYY-MON-DD')

Also, do not use YY to denote the year. You don't have to re-invent the Y2K bug again. Always use YYYY format for an year. Else, if you are stuck with YY values for year, then use RR format. But, I would insist, always use YYYY format for year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM