Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ORACLE SQL DEVELOPER Query ?

Posted on 2016-09-02
5
Medium Priority
?
126 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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 …
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

618 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