Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PL/SQL Aggregate Woes

  • 16-10-2015 11:31am
    #1
    Registered Users, Registered Users 2 Posts: 1,426 ✭✭✭


    Hi,

    I've been having a bit of a mind melt in work over this query, i just cant get it to aggregate the way I want to. I've tried, TRUNC, SUM, TO_CHAR, TO_DATE havent nested it yet but would like to see if there is a simpler way.

    Essentially i want it broken down by date, and all fields in the underlying table are in a date time format, so they come up with multiple values for the same date and type.

    The query in question is below: it should return three columns
    Type, Date, Count (*) broken down by date:

    This is what i have at the moment and I've anonymised the query, any help would be appreciated.

    SELECT tb.type " Type",TRUNC(TO_DATE(tb.datetime, 'dd/mm/yyyy'), 'dd/mm/yyyy'), Count(*)

    FROM table.tab tb

    WHERE tb.type in ('Type1','Type2','Type3')
    AND tb.datetime > TO_DATE('05/10/2015','dd/mm/yyyy')
    GROUP BY tb.type, tb.datetime;
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 374 ✭✭Zith


    Try something like this:
    SELECT tb.type " Type",TRUNC(TO_DATE(tb.datetime, 'dd/mm/yyyy'), 'dd/mm/yyyy'), Count(*)
    FROM table.tab tb
    
    WHERE tb.type in ('Type1','Type2','Type3')
    AND tb.datetime > TO_DATE('05/10/2015','dd/mm/yyyy')
    GROUP BY tb.type, TRUNC(TO_DATE(tb.datetime, 'dd/mm/yyyy');
    


    You need to group on exactly what you expect the output to be. By grouping on the tb.datetime you still include seconds hence it doesn't group as you want.


  • Registered Users, Registered Users 2 Posts: 1,426 ✭✭✭Neon_Lights


    D'oh!

    Thanks Zith, had an aul play around with the Query and got it grouped using TO_DATE() in the group by function.

    Appreciate the help :)


  • Registered Users, Registered Users 2 Posts: 374 ✭✭Zith


    Happy to help!


Advertisement