?
Solved

ORACLE SQL DEVELOPER Query ?

Posted on 2016-09-02
5
Medium Priority
?
136 Views
Last Modified: 2016-11-12
I have the below query that pulls the number of calls by hour today, what i would like to do is pull the data for the last 10 days, but have each day represented as a column and then it show null or 0 if no data for that interval.

I need to use the query in ORACLE SQL DEVELOPER

select to_char(startdate, 'hh24'), count(*) as totalcalls
from tbl_calls
where startdate >=  TRUNC(SYSDATE) - 1/86400
group by to_char(startdate, 'hh24')
order by to_char(startdate, 'hh24');


I would like data to be represented like this

HR   - Then a column for previous 10 days.
7         11    12  22  35   16  12
0
Comment
Question by:dkilby
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41782411
select to_char(startdate, 'hh24'), count(*) as totalcalls
from tbl_calls
where startdate >=  TRUNC(SYSDATE -10)
group by to_char(startdate, 'hh24')
order by to_char(startdate, 'hh24');

Open in new window

Give this a try - I don't have an Oracle server available at the moment to test
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41782415
It would perhaps be more conventional (and more flexible) to have hours as columns and dates as rows because the hours in a day is a fixed number. However you can use conditional aggregates for what you ask:
SELECT
      to_char(startdate, 'hh24')
    , count(case when startdate >= TRUNC(SYSDATE) - 10 and startdate < TRUNC(SYSDATE) - 9 then startdate end) as D10
    , count(case when startdate >= TRUNC(SYSDATE) -  9 and startdate < TRUNC(SYSDATE) - 8 then startdate end) as D9
    , count(case when startdate >= TRUNC(SYSDATE) -  8 and startdate < TRUNC(SYSDATE) - 7 then startdate end) as D8
    , count(case when startdate >= TRUNC(SYSDATE) -  7 and startdate < TRUNC(SYSDATE) - 6 then startdate end) as D7
    , count(case when startdate >= TRUNC(SYSDATE) -  6 and startdate < TRUNC(SYSDATE) - 5 then startdate end) as D6
    , count(case when startdate >= TRUNC(SYSDATE) -  5 and startdate < TRUNC(SYSDATE) - 4 then startdate end) as D5
    , count(case when startdate >= TRUNC(SYSDATE) -  4 and startdate < TRUNC(SYSDATE) - 3 then startdate end) as D4
    , count(case when startdate >= TRUNC(SYSDATE) -  3 and startdate < TRUNC(SYSDATE) - 2 then startdate end) as D3
    , count(case when startdate >= TRUNC(SYSDATE) -  2 and startdate < TRUNC(SYSDATE) - 1 then startdate end) as D2
    , count(case when startdate >= TRUNC(SYSDATE) -  1 and startdate < TRUNC(SYSDATE)     then startdate end) as D1
    , COUNT(*) AS totalcalls
FROM tbl_calls
WHERE startdate >= TRUNC(SYSDATE) - 10 and and startdate < TRUNC(SYSDATE)
GROUP BY
      to_char(startdate, 'hh24')
ORDER BY
      to_char(startdate, 'hh24')
;

Open in new window


Please also note that you do not need (and should not use)  minus 1 second because you are using greater than OR EQUAL TO
TO_CHAR(TRUNC(SYSDATE)-1/86400,'YYYY-MM-DDHH24:MI:SS')
2016-09-01 23:59:59

TO_CHAR(TRUNC(SYSDATE),'YYYY-MM-DDHH24:MI:SS')
2016-09-02 00:00:00
Deducting 1 more second is just unnecessary and opens up a small margin for error if the data happens to be timestamps.
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41782416
Deducting 1 more second is just unnecessary and opens up a small margin for error if the data happens to be timestamps.
This seemed odd to me as well.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41782425
Deducting one second has a long history (i.e. "old fashioned"), it can easily lead to error so whenever I see it (or something similar) I try to correct it.

There was a time when databases only dealt with time precision to the closest second, but that is no longer true. In Oracle 11 & 12  a timestamp's precision is up to 9 decimal points of a second.

So if a column is timestamp, or changed from date to timestamp, deducting one second is simply no longer accurate. It isn't even needed if the data is of dates only because of the equal condition, so, as I said it is simply not needed and should not be used.
0
 

Author Closing Comment

by:dkilby
ID: 41783005
thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question