Afternoon All,
I am new to Oracle and SQL but i have a query with the below code i have created. Im currently using Oracle 10g. Im just wondering if anyone can help me make this code dynamic instead of hard coded.
The code i have simply looks at a table that logs the users activity. I then essentially count the number of records that we have for each user / PC and display this in a pivot style table.
Its not a very difficult query as such but i could have about 30 or so PC's that i would need to enter and this hard coded method is definitly not the best way to complete this task.
I have been searching on the internet to look at a dynamic statement i could use based on the host name or user_ID but i have not managed to find anything that simply loops through my data and then generates this piviot style view.
I have been looking at 'Cursors' but im think im way off by a long shot.
Ant help is much appriechiated in advance.
Regards Betty
SELECT USER_ID,
SUM(CASE WHEN host LIKE 'PC1' THEN 1 ELSE 0 END) AS PC1,
SUM(CASE WHEN host LIKE 'PC2' THEN 1 ELSE 0 END) AS PC2,
SUM(CASE WHEN host LIKE 'PC3' THEN 1 ELSE 0 END) AS PC3,
SUM(CASE WHEN host IS NOT NULL THEN 1 ELSE 0 END) AS grand_total
FROM table_Name
GROUP BY USER_ID