I have this view in an Oracle db and I'm trying to generate a report with an SQL query but it's not working for last few days now so decide to get expert advice.
View does runs well. Basically what view does is it records all users who log into any database for auditing purpose.
View columns:
dbname
username
server
program
logon_time
Questions:
I am trying to generate SQL to group by all username and program who log in to dbname=x on date=26-Dec-2012
count distinct username and program where user logged on 26-Dec-2012
Basically trying to get... if dbname=x get 50000 login via username=abc then abc was connected from which server and what program?
Tried the following query
select dbname, username, server, program, logon_time from audit
where username = abc and
dbname in (select dbname from audit
where dbname='x' and logon_time = to_date('26-DEC-2012','DD-MON-YYY'))
group by username, program
This seems very straight-forward, except for issues with reformatting the date+time information into just date information.
Query 1:
SELECT username, program
FROM Audit
WHERE dbname = 'x'
AND TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program;
This should give you a list of the distinct username, program combinations that logged into database 'x' on 26th December 2012.
Query 2:
You want the COUNT of the rows from Query 1 except for the criterion about database name, so:
SELECT COUNT(*)
FROM (SELECT username, program
FROM Audit
WHERE TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program
)
This should give you a count of the distinct user name, program combinations that connected to any database on 26th December 2012.
You could try something like this:
SELECT dbname, username, server, program, trunc(logon_time), count(*) FROM audit
WHERE username = 'abc'
AND dbname = 'x'
AND logon_time >= to_date('26-DEC-2012','DD-MON-YYY')
AND logon_time < to_date('27-DEC-2012','DD-MON-YYY')
GROUP BY dbname, username, server, program, trunc(logon_time)
Edit: Answer revised to fix grouping mistake Conrad pointed out.
Still not 100% sure what is required, but this should be much closer.
Related
Need SQL help as I am new to writing SQL Queries
Given below are the sample table structure and the sample GROUP BY result
Assuming that the table can have a very large no of records by having different column values.
The output I am expecting is the sum of each different group as per the user selection.
e.g: below is the expected output:
Here is the SQL Query which I am using to fetch the above shown result:
SELECT project,
grant,
program,
department,
SUM(amount) AS Total
FROM tran_table
WHERE ( project = 'pj1'
AND grant = 'gr1'
AND program = 'pg1'
AND department = 'dp1' )
OR ( project = 'pj3'
AND grant = 'gr2'
AND program = 'pg1'
AND department = 'dp2' )
OR ( project = 'pj6'
AND grant = 'gr3'
AND program = 'pg2'
AND department = 'dp1' )
GROUP BY project,
grant,
program,
department
Question:
Is this a correct way to write the SQL Query with all different group values in the WHERE condition when the expected output could be for 100 different groups?
Your approach is fine for a 100 selections, you will fail with more than 1000 selections which is the IN list Oracle restriction.
You may use the following WHERE clause
WHERE (project, grant, program, department) in
(('pj1', 'gr1', 'pg1', 'dp1'),
('pj3', 'gr2', 'pg1', 'dp2'),
('pj6', 'gr3', 'pg2', 'dp1')
)
As this is reporting query leading to full table scan on large table you need not care much about bind variables and limiting the parsing. You should however care about checking the user input to prevent the SQL injection.
Of course the alternative is to use a temporary table filled with the input data with an INNER JOIN to filter the data.
How to know currently logged in username in Oracle SQL, if we are dealing with multiple users within the same DB, it is little bit confusing to understand!
By using this query we can get currently logged in user
select user from dual;
There are many different ways.
Option 1
Use the V$SESSION view.
SELECT USERNAME FROM V$SESSION;
Option 2
This one gives all the detailed information because there are times that you have to locate locked sessions.
select
substr(a.spid,1,9) pid,
substr(b.sid,1,5) sid,
substr(b.serial#,1,5) ser#,
substr(b.machine,1,6) box,
substr(b.username,1,10) username,
-- b.server,
substr(b.osuser,1,8) os_user,
substr(b.program,1,30) program
from v$session b, v$process a
where
b.paddr = a.addr
and type='USER'
order by os_user,username;
Here is the table in question, I don't really know where to start.
You can group by the user name, and then count many games a user is signed up to:
SELECT username
FROM mytable
GROUP BY username
HAVING COUNT(DISTINCT gamename) = 2
I have a table that includes the following columns:
UserName (nvarchar), TransactionDateTime (datetime), Balance (money).
Each time a user makes a transaction, this is recorded as a row in the table.
The majority of users have multiple transactions stored in the database.
I want to write an SQL query that takes the most recent TransactionDateTime for each UserName and then creates a total of all these balances.
I do not want to include the balances of any user transactions apart from the most recent for each user.
I hope that I have explained this clearly. Any help is gratefully appreciated.
Using Microsoft SQL Server 2008 and SQL management studio 2008.
Thanks,
Nelson
select UserName, max(TransactionDateTime), sum(Balance)
from tablename group by Username Having TransactionDateTime=max(TransactionDateTime)
I have found out how to accomplish this:
SELECT SUM(Balance) AS Total_Balance
FROM tablename a
WHERE TransactionDateTime = (
SELECT MAX(TransactionDateTime)
FROM tablename b
WHERE a.UserName = b.UserName)
I work at a school and we are trying to run a query to see all students that should be starting in the next week who don't already have an AD account (we use ID numbers in the pager field and so search on that). We have linked SQL to Ad so we can run these queries and never had any issue but when I run the following I don't get any results.
select *
from FutureStudents
where id not in (select * from OPENQUERY(ADSI, '
SELECT pager
FROM ''LDAP://OU=students,DC=stcatherines,DC=net,DC=au''
WHERE objectCategory = ''Person''
AND objectClass = ''user'' '
)
)
When I run the not in command I expect that I should get one result but I do not. But if I change the query to in (instead of not in) I do get all the students that are in the table who have accounts. Plus at the same time as running these queries I have done a simple query looking for the pager in AD that I have been moving in order to get a result and when the account is in the student OU I get a result and when it is moved I do not.
So I am fairly sure is something to do with the not in statement. Am I using the right statement or is there something different I should be doing to get true results?