Microsoft
Software
Hardware
Network
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%2Fe
n-us%2defa
ult.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(eventdatea
ndtime,'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,in
str(userpr
ofileattrs
,'=',1,2)+
1,
instr(substr(USERPROFILEAT
TRS,instr(
userprofil
eattrs,'='
,1,2)+1),'
')-1) department_name,
substr(userprofileattrs,in
str(userpr
ofileattrs
,'=',-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(s
ysdate),-1
),'yyyymm'
)
order by department_name, department_code, to_date(to_char(eventdatea
ndtime,'yy
-mm-dd'),'
yy-mm-dd')
)
group by department_name, department_code, day1;
Random Solutions
pcAnywhere Host
Mail not being sent
SQL database issues over ipsec site to site tunnel on Juniper SSG520
SpellNumber
Setting Dell 2330dn Printer to Automatically Print from Multi-Purpose Tray
Windows 7 recently used programs is blank
Server 2008 quickest way to delete a large folder with millions of files
Registry, Winlogon, KEY_WOW64_64KEY errors
How many instances of a web application can run on a server
Terminal server 2008 R2 Pinned Taskbar Links