Solved

ORACLE SQL DEVELOPER Query ?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

726 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