Question : SQL Query for count function on multiple tables

Hello experts - I need to build a SQL query against an Oracle Database (two tables) that would do the following:


- count the number of sucessful hits (authentication success) to a particular application for each day and sorted by Department, Departmentcode, and day of the month.

such  that output of the query looks like:

Department      code            Day            Portal            App1             App2
                                    
Marketting      36            YY-MM-DD      134                     54                    89
Finance            16            YY-MM-DD      3                     5                     3
Development      12            YY-MM-DD      3                     76                     0


There are two tables of interest one that contains the audit information (audit) and the other that contains the department information (user).

Here is the audit table information:

 CREATE TABLE AUDIT
   (      "EVENTDATEANDTIME" TIMESTAMP (6),
      "SERVERID" NVARCHAR2(255),
      "EVENTNAME" NVARCHAR2(255),
      "URL"       NVARCHAR2(255),
      "OPERATION" NVARCHAR2(255),
      "TARGETUSERDN" NVARCHAR2(255),
      "IPADDRESS" NVARCHAR2(255),
      "USERPROFILEATTRS" NVARCHAR2(255),
   )


The sample data from audit table looks like this:


EVENTDATEANDTIME = 18-AUG-10 07.33.01.000000000 PM      
SERVERID = myserver1            
EVENTNAME = AUTHN_SUCCESS                                    
URL = xyz.mydomain.com%2Fportal%2Flang%2Fen-us%2default.aspx                                    
OPERATION = GET
TARGETUSERDN = uid=markuser1,ou=people,o=mycomany,c=com
IPADDRESS = 10.8.146.134      
USERPROFILEATTRS = uid=markuser1

EVENTDATEANDTIME = 19-AUG-10 05.30.09.000000000 PM      
SERVERID = myserver2            
EVENTNAME = AUTHN_SUCCESS                                    
URL = xyz.mydomain.com%2FAPP1%2Flang%2Fen-us%2default.aspx                                    
OPERATION = GET
TARGETUSERDN = uid=finuser1,ou=people,o=mycomany,c=com
IPADDRESS = 10.8.146.130      
USERPROFILEATTRS = uid=finuser1


Here is the sample user table information:

CREATE TABLE user
(       userid                 varchar2(50)       not null,
        department_name       varchar2(50)       not null,
        department_code         number(6)       not null,      
)

The sample data from user table looks like this:

userid = markuser1      
Department_name = Marketting
Department_code = 36

userid = finuser1      
Department_name = Finance
Department_code = 12

Thanks,

Answer : SQL Query for count function on multiple tables

try this :

select
department_name,
department_code, day,
sum(succ_por_count) Portal,
sum(succ_app1_count) App1,
sum(succ_app2_count) App2
from (
select u.department_name, u.department_code, to_char(eventdateandtime,'yy-mm-dd') Day,
case when EVENTNAME = 'AUTHN_SUCCESS' and url like '%portal%' then 1 else 0 end succ_por_count,
case when EVENTNAME = 'AUTHN_SUCCESS' and url like '%APP1%' then 1 else 0 end succ_app1_count,
case when EVENTNAME = 'AUTHN_SUCCESS' and url like '%APP2%' then 1 else 0 end succ_app2_count
from user_tbl u, audit_tbl a
where u.userid = substr(USERPROFILEATTRS,5)
order by u.department_name, u.department_code, to_date(to_char(eventdateandtime,'yy-mm-dd'),'yy-mm-dd')
)
group by department_name, department_code, day

Random Solutions  
 
programming4us programming4us