How do you create a conditional count across multiple fields?

bobtastic

I have the huge table of over 100 million rows of data which is joined to another reference table that I want to create a conditional count for.

The first table is the large one which is an audit log and contains data which lists data on countries and contains a date of audit. The second table is a smaller table which contains relational data to the audit log. The first part is the easy bit which is to identify which audit data I want to see. I have the following code to identify this:

    select aud.*
    from audit_log aud
    join database db on db.id=aud.release_id
    where aud.event_description like '% opted in'
    and r.creation_source = 'system_a'

This gives me the data in the following format:

Country             Event Description                                   Audit Date
Czech Republic      Czech Republic has been automatically opted in      11-AUG-14 07.01.52.606000000
Denmark             Denmark has been automatically opted in             12-AUG-15 07.01.53.239000000
Denmark             Denmark has been automatically opted in             11-SEP-15 07.01.53.902000000
Dominican Republic  Dominican Republic has been automatically opted in  11-SEP-15 07.01.54.187000000
Ecuador             Ecuador  has been automatically opted in            11-DEC-14 07.01.54.427000000
Ecuador             Ecuador has been automatically opted in             11-NOV-14 07.01.54.679000000

The number of results from this query still returns over 5 million rows so I cannot export the data to Excel to create a count. My two main issues are the number of rows and the date format of the 'Audit Date' field.

Ideally I want to create a count which shows the data as:

Country                |Aug-14|Nov-14|Dec-14|Aug-15|Sep-15
Czech Republic         |  1   |      |      |      |
Denmark                |      |      |      |  1   | 1
Dominican Republic     |      |      |      |      | 1
Ecuador                |      | 1    |  1   |      |        

Any idea's on how I extract the month and year and drop the figures into column by country?

Thanks

Edit - Thank you xQbert for you solution, it worked perfectly! The problem now is that I have run into a new problem. I need to constrain the count by another query, but there is no unique identifier between the tables involved.

For example, I amended your query to fit my db:

select cty.country_name, 
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2014' then 1 else 0 end) as "AUG-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2014' then 1 else 0 end) as "SEP-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='OCT-2014' then 1 else 0 end) as "OCT-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='NOV-2014' then 1 else 0 end) as "NOV-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='DEC-2014' then 1 else 0 end) as "DEC-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JAN-2015' then 1 else 0 end) as "JAN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='FEB-2015' then 1 else 0 end) as "FEB-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAR-2015' then 1 else 0 end) as "MAR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='APR-2015' then 1 else 0 end) as "APR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAY-2015' then 1 else 0 end) as "MAY-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUN-2015' then 1 else 0 end) as "JUN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUL-2015' then 1 else 0 end) as "JUL-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2015' then 1 else 0 end) as "AUG-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2015' then 1 else 0 end) as "SEP-15"
    from dschd.audit_trail aud
    join dschd.release r on r.id=aud.release_id
    join dschd.country cty on aud.EVENT_COUNTRY_ID=cty.id
    where aud.event_description like '% opted in'
    and r.creation_source = 'DSCHED'
    GROUP BY cty.COUNTRY_name

My second query is:

select  *
from DSCHD.RELEASE_COUNTRY_RIGHT rcr
join dschd.release r on rcr.RELEASE_ID=r.ID
join dschd.country cty on rcr.COUNTRY_ID=cty.id
where r.release_status in ('DRAFT', 'SCHEDULED', 'FINAL', 'DELIVERED')
and r.is_active = 'Y'
and rcr.MARKETING_RIGHT = 'Y'
and rcr.OPT_OUT = 'N'
and r.creation_source = 'DSCHED'

The problem is that I have many countries which can relate to one ID (Release_ID) but there is no unique identifier between the tables on a country level. Each country has an ID though.

So for query 1, to identify each unique row I would need the 'aud.Release_ID' and the 'aud.Event_country_id' and for query 2 to achieve the same I would need to use the 'rcr.Release_ID' and 'rcr.country_id'.

select cty.country_name, 
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2014' then 1 else 0 end) as "AUG-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2014' then 1 else 0 end) as "SEP-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='OCT-2014' then 1 else 0 end) as "OCT-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='NOV-2014' then 1 else 0 end) as "NOV-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='DEC-2014' then 1 else 0 end) as "DEC-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JAN-2015' then 1 else 0 end) as "JAN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='FEB-2015' then 1 else 0 end) as "FEB-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAR-2015' then 1 else 0 end) as "MAR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='APR-2015' then 1 else 0 end) as "APR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAY-2015' then 1 else 0 end) as "MAY-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUN-2015' then 1 else 0 end) as "JUN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUL-2015' then 1 else 0 end) as "JUL-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2015' then 1 else 0 end) as "AUG-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2015' then 1 else 0 end) as "SEP-15"
    from dschd.audit_trail aud
    join dschd.release r on r.id=aud.release_id
    join dschd.country cty on aud.EVENT_COUNTRY_ID=cty.id
    where aud.event_description like '% opted in'
    and ***** in (select  ******
                 from DSCHD.RELEASE_COUNTRY_RIGHT rcr
                 join dschd.release r on rcr.RELEASE_ID=r.ID
                 join dschd.country cty on rcr.COUNTRY_ID=cty.id
                 where r.release_status in ('DRAFT', 'SCHEDULED', 'FINAL', 'DELIVERED')
                 and r.is_active = 'Y'
                 and rcr.MARKETING_RIGHT = 'Y'
                 and rcr.OPT_OUT = 'N'
                 and r.creation_source = 'DSCHED')
GROUP BY cty.COUNTRY_name

The bit I am stuck at are the two parts which are indicated by '*****' as the join criteria is two fields.

Any ideas?

xQbert

Quick and dirty, not dynamic floating based on a 12 month cylce or anything...

select country, 
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2014' then 1 else 0 end) as "AUG-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2014' then 1 else 0 end) as "SEP-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='OCT-2014' then 1 else 0 end) as "OCT-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='NOV-2014' then 1 else 0 end) as "NOV-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='DEC-2014' then 1 else 0 end) as "DEC-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JAN-2015' then 1 else 0 end) as "JAN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='FEB-2015' then 1 else 0 end) as "FEB-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAR-2015' then 1 else 0 end) as "MAR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='APR-2015' then 1 else 0 end) as "APR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAY-2015' then 1 else 0 end) as "MAY-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUN-2015' then 1 else 0 end) as "JUN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUL-2015' then 1 else 0 end) as "JUL-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2015' then 1 else 0 end) as "AUG-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2015' then 1 else 0 end) as "SEP-15"

    from audit_log aud
    join database db on db.id=aud.release_id
    where aud.event_description like '% opted in'
    and r.creation_source = 'system_a'
GROUP BY COUNTRY

Ideally we'd simply use a Pivot statement or base it on earliest date in range and go on... Such as found in this prior stack article Dynamic pivot in oracle sql

update based on changing requirements you do know you can join on multiple criteria right? :P

Note we created an inline view with your second query alias it as z table name and then add the two columns desired to match on as part of the results. Then we join it as if it were a table!

select cty.country_name, 
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2014' then 1 else 0 end) as "AUG-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2014' then 1 else 0 end) as "SEP-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='OCT-2014' then 1 else 0 end) as "OCT-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='NOV-2014' then 1 else 0 end) as "NOV-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='DEC-2014' then 1 else 0 end) as "DEC-14",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JAN-2015' then 1 else 0 end) as "JAN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='FEB-2015' then 1 else 0 end) as "FEB-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAR-2015' then 1 else 0 end) as "MAR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='APR-2015' then 1 else 0 end) as "APR-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='MAY-2015' then 1 else 0 end) as "MAY-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUN-2015' then 1 else 0 end) as "JUN-15",
SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='JUL-2015' then 1 else 0 end) as "JUL-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='AUG-2015' then 1 else 0 end) as "AUG-15",
    SUM(CASE WHEN to_char(Audit_Date,'MON-YYYY') ='SEP-2015' then 1 else 0 end) as "SEP-15"
    from dschd.audit_trail aud
    join dschd.release r on r.id=aud.release_id
    join dschd.country cty on aud.EVENT_COUNTRY_ID=cty.id
    join (select  Release_ID, country_id
                 from DSCHD.RELEASE_COUNTRY_RIGHT rcr
                 join dschd.release r on rcr.RELEASE_ID=r.ID
                 join dschd.country cty on rcr.COUNTRY_ID=cty.id
                 where r.release_status in ('DRAFT', 'SCHEDULED', 'FINAL', 'DELIVERED')
                 and r.is_active = 'Y'
                 and rcr.MARKETING_RIGHT = 'Y'
                 and rcr.OPT_OUT = 'N'
                 and r.creation_source = 'DSCHED') Z
       ON aud.Release_ID = z.Realease_ID and
          aud.Event_country_id = z.country_id
    where aud.event_description like '% opted in'
GROUP BY cty.COUNTRY_name

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count and display duplicates across multiple fields

From Dev

How do you create a multileveled list across tables, in MS Word?

From Dev

How do you count misspelled fields using a SQL query?

From Dev

How do you create a probabilistic conditional flow in python?

From Dev

How do you search over multiple fields in a Django model

From Dev

How do you increment multiple fields in one query in mongoose?

From Dev

How do you increment multiple fields in one query in mongoose?

From Java

How do you create a "count" matrix from a series?

From Dev

How to create a formula that can count values across multiple columns in specified rows

From Dev

How to groupby count across multiple columns in pandas

From Dev

How to get count across multiple tables

From Dev

Matlab: How do I create multiple fields in a struct with a for loop

From Dev

How do you use a string across classes?

From Dev

How do you share your GOPATH via Dropbox (or similar) across multiple platforms

From Dev

How do you use play scala specs2 matchers across multiple files

From Dev

How do you restrict specific content to only one column instead of going across multiple columns?

From Dev

how do I get a count of matches across multiple tables joining by id?

From Dev

How do you create multiple strokes on text in WPF?

From Dev

How do you create a map/reduce index with multiple groupings in RavenDB

From Dev

How do you create multiple JLabels with a while loop

From Dev

Count across multiple columns

From Dev

Count across multiple columns

From Java

How do you use the ? : (conditional) operator in JavaScript?

From Dev

How to do a conditional count in noncontiguous cells

From Dev

How to do a cascading decrement across multiple columns

From Dev

Create conditional for COUNT

From Dev

Aggregation Conditional Count on Present Fields

From Dev

Tableau Desktop - How do you combine multiple Calculated Fields into one to use as a Filter?

From Dev

How to create a performance system that count tags across a large dynamic dataset

Related Related

  1. 1

    Count and display duplicates across multiple fields

  2. 2

    How do you create a multileveled list across tables, in MS Word?

  3. 3

    How do you count misspelled fields using a SQL query?

  4. 4

    How do you create a probabilistic conditional flow in python?

  5. 5

    How do you search over multiple fields in a Django model

  6. 6

    How do you increment multiple fields in one query in mongoose?

  7. 7

    How do you increment multiple fields in one query in mongoose?

  8. 8

    How do you create a "count" matrix from a series?

  9. 9

    How to create a formula that can count values across multiple columns in specified rows

  10. 10

    How to groupby count across multiple columns in pandas

  11. 11

    How to get count across multiple tables

  12. 12

    Matlab: How do I create multiple fields in a struct with a for loop

  13. 13

    How do you use a string across classes?

  14. 14

    How do you share your GOPATH via Dropbox (or similar) across multiple platforms

  15. 15

    How do you use play scala specs2 matchers across multiple files

  16. 16

    How do you restrict specific content to only one column instead of going across multiple columns?

  17. 17

    how do I get a count of matches across multiple tables joining by id?

  18. 18

    How do you create multiple strokes on text in WPF?

  19. 19

    How do you create a map/reduce index with multiple groupings in RavenDB

  20. 20

    How do you create multiple JLabels with a while loop

  21. 21

    Count across multiple columns

  22. 22

    Count across multiple columns

  23. 23

    How do you use the ? : (conditional) operator in JavaScript?

  24. 24

    How to do a conditional count in noncontiguous cells

  25. 25

    How to do a cascading decrement across multiple columns

  26. 26

    Create conditional for COUNT

  27. 27

    Aggregation Conditional Count on Present Fields

  28. 28

    Tableau Desktop - How do you combine multiple Calculated Fields into one to use as a Filter?

  29. 29

    How to create a performance system that count tags across a large dynamic dataset

HotTag

Archive