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
Solved

ORACLE SQL DEVELOPER Query ?

Posted on 2016-09-02
5
82 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 48

Accepted Solution

by:
PortletPaul earned 500 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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

856 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