Oracle SQL

I have a following table with the datasets as a example.

Date                            Type
01/02/2014 12:32     GEO
01/03/2014  14:43    GEO
01/04/2014  19:34    GEO
01/07/2014   01:23    SCI
01/07/2014   02:23     ENG

02/05/2015 12:33 GEO
02/02/2015 01:33 MAT

I want to group the date by month and the result should be like this:

Month                 GEO  SCI   MAT ENG   Total
01/01/2014         3        1      0       1         5
02/01/2015         1        0      1       0          2
techlearnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Haris DulicIT ArchitectCommented:
Hi,

try this:
select date as Date, sum(a) as GEO, sum(b) as SCI, sum(c) as MAT, sum(d) as ENG, sum(a+b+c+d) as TOTAL
from (
select to_char(date,'mm/dd/yyyy') as date, count(1) as a , 0 as b, 0 as c, 0 as d from TABLE_NAME where type ='GEO'
union
select to_char(date,'mm/dd/yyyy') as date, 0 as a , count(1) as b, 0 as c, 0 as d from TABLE_NAME where type ='SCI'
union
select to_char(date,'mm/dd/yyyy') as date, 0 as a , 0 as b, count(1) as c, 0 as d from TABLE_NAME where type ='MAT'
union
select to_char(date,'mm/dd/yyyy') as date, 0 as a , 0 as b, 0 as c, count(1) as d from TABLE_NAME where type ='ENG'
from) a
group by date

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdstuberCommented:
SELECT TRUNC(yourdate, 'mm') month,
         COUNT(DECODE(yourtype, 'GEO', 1)) geo,
         COUNT(DECODE(yourtype, 'SCI', 1)) sci,
         COUNT(DECODE(yourtype, 'MAT', 1)) mat,
         COUNT(DECODE(yourtype, 'ENG', 1)) eng,
         COUNT(*) total
    FROM yourtable
GROUP BY TRUNC(yourdate, 'mm')
ORDER BY 1

or

SELECT month,geo,sci,mat,eng,geo+sci+mat+eng total
 FROM (SELECT trunc(yourdate,'mm') month, yourtype FROM yourtable )
    PIVOT(COUNT(*) FOR yourtype IN ('GEO' geo,'SCI' sci,'MAT' mat,'ENG' eng))
ORDER BY month;
Mark GeerlingsDatabase AdministratorCommented:
The output you are asking for is called a "matrix" or "cross-tab" report.  Simple SQL does not do this.  So, you need the more-complex examples like what others have suggested, or you need to use a reporting tool that can do this for you.
sdstuberCommented:
if the expected columns are known before hand it can be done,  as illustrated above in 2 different forms.

you could also do a bunch of self-joins or unions but that's horribly inefficient
Steve WalesSenior Database AdministratorCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.