?
Solved

ORACLE SQL DEVELOPER Query ?

Posted on 2016-09-02
5
Medium Priority
?
116 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

762 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