简体   繁体   中英

Oracle query for finding the records

I am not getting any result from the below 2 queries. Please let me know if anything is wrong in this queries.

  1. Display all records where the gender is female and salary is greater than 5000.50 and birth_date is between the Unix Timestamp values of '946684800' and '1609372800'
SELECT * 
FROM PERSON 
where gender = 'female' and salary  > '5000.50'  and birth_date BETWEEN to_date('19700101', 'YYYYMMDD') + 946684800/24/60/60/1000  and to_date('19700101', 'YYYYMMDD') + 1609372800/24/60/60/1000;
  1. Display the number of records grouped by gender and salary (where salary is rounded up to the nearest thousandth)

SELECT count(*), gender FROM PERSON where ceiling(salary/1000.0)*1000 group by gender;

Likely, the problem is the fiter on the dates. You seem to assume that 946684800 is a unix epoch in milliseconds , while it looks like it is expressed in seconds.

Consider:

alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';

-- your expression
select to_date('19700101', 'YYYYMMDD') + 946684800/24/60/60/1000 dt from dual

| DT                  |
| :------------------ |
| 1970-01-11 22:58:05 |

-- isn't this better?
select to_date('19700101', 'YYYYMMDD') + 946684800/24/60/60 dt from dual

| DT                  |
| :------------------ |
| 2000-01-01 00:00:00 |

I would also recommend giving a literal number to the filer on salary rather than a string (unexpected things happen when you compare values of different datatypes). Finally, date literals should be preferred to to_date() whenever possible.

You could phrase the query as:

select * 
from person 
where 
    gender = 'female' 
    and salary > 5000.50 
    and birth_date between date '1970-01-01' +  946684800/24/60/60 
                       and date '1970-01-01' + 1609372800/24/60/60;

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