Question : Need help in updating a SQL query to retrieve multiple data from a signle column

Hi - we have a SQL query that runs against two tables of DB and retrieve data for us.The existing query counts the number of sucessful hits (authentication success) to a particular application for each day and sorted by Department, Departmentcode, and day of the month.

Now we need to achieve the same with one table only. one of the column of interest contains multiple data value for us i.e. USERPROFILEATTRS = uid=markuser1 uid=markuser1 departmentname=Marketting

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 uid=markuser1 departmentname=Marketting departmentcode=36

Note: the attributes are seprated with a space seperator in the database column.

Can we modified the SQL query now so that it retrieves data from one table and produce the same output?

The existing SQL query is below:


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


Also, is it possible to only retrieve data for the last month only as oppose to all of the months with this query i.e. We need to run this query on the first day of each month for the previous month i.e. if we run this on Sep 1st, 2010 then it should only shows records for August 2010 month.

Note: nav_kum_v was helping me to build the first  query and i am hoping he can pick this up as well.

Thanks,

Answer : Need help in updating a SQL query to retrieve multiple data from a signle column

if you run this anytime, it will check sysdate ( for example today is 25-aug-2010 ) and the query will bring data for the july-2010 month. Let me know if you need any modifications.

Try this :

select
department_name,
department_code,
day1,
sum(succ_por_count) Portal,
sum(succ_app1_count) App1,
sum(succ_app2_count) App2
from (
select
substr(USERPROFILEATTRS,instr(userprofileattrs,'=',1,2)+1,
instr(substr(USERPROFILEATTRS,instr(userprofileattrs,'=',1,2)+1),' ')-1) department_name,  
substr(userprofileattrs,instr(userprofileattrs,'=',-1)+1) department_code,
to_char(eventdateandtime,'yy-mm-dd') Day1,
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 audit_tbl
where to_char(eventdateandtime,'yyyymm') = to_char(add_months(trunc(sysdate),-1),'yyyymm')
order by department_name, department_code, to_date(to_char(eventdateandtime,'yy-mm-dd'),'yy-mm-dd')
)
group by department_name, department_code, day1;
Random Solutions  
 
programming4us programming4us