I have an Archive Table. The columns are PK, Users and Logdatetime. When Users log in the system i am writing to archive table the user and datetime. A user can login more than one in one day.
The data in Logdatetime is like '2010-03-16 00:00:00.000'
I need to query all users FIRST Login time in one day and get the user with the datetime like:
user1 '2010-03-16 03:21:00.000'
user2 '2010-03-16 04:11:00.000'
It does not matter the users order.
I tried too many way but couldnt find a way. The only solution i found is the query for all users one by one. But it will take time. Any idea how to do this with a one query or more efficent way?
PS: I am using MS SQL Server 2005
Do you mean that you want the users very first login or the just first of a specific day?
This example query might lead you in the right direction;
SELECT [User], MIN(LogDateTime) AS FirstLogin FROM [Archive Table] GROUP BY [User]
Related
I need to get all the log in times of each users from our data store.
fields of the data store are
users, logInTime, LogOutTime, ...
I know I can use count(logInTime) and group it by users to see how many times a user logged in to our system, but how can I get all the logged in times in a list?
Thank you guys!
you can use the group_concat function so something like this:
select
userid, group_concat(logInTime,'|')
from table
group by userid
I am new to SQL Server. I have been assigned to do some simple queries to start off, then eventually move on to more complex queries.
I have spent a lot of time on this website: http://www.w3schools.com and I understand it, I think, but then when I go back to my company's database, I find myself searching from many, many, different tables with different information.
For example, a table would say [Acct_Name] and the query comes back with not the correct account name (s) that I need. Any advice that you think might help me? Thank you.
It sounds like you are looking to limit your results to specific accounts. There are many ways to go about this, so no one will be able to give you a all encompassing answer but if you are looking to just pull a single account
SELECT * FROM (your table name) WHERE Acct_Name = 'the account name'
The * means you are selecting all columns in the table and your WHERE clause is where you set your search conditionals, like account name or by account ID. If you had a account creation date, you could get all accounts created on or before a date like this
SELECT * FROM (your table name) WHERE Created < '2016-06-01 00:00:00'
Replace the column name 'Created' with the column that holds the date field of account creation
Learning the WHERE clause and what you can do there to limit your results will get you on a solid footing to start, from there you will want to learn JOINs and how to link tables by primary keys.
Code academy has some great tutorials https://www.codecademy.com/learn/learn-sql
I have a requirement to produce a daily report at the end of the day to track user activity.
I need to report for each user first logging on time, if possible first activity too, then last activity and if possible logging off time.
optional.
It might be good to also record the amount of activity for the user.
I have been looking at Oracle tables/views dba_audit_trail and v$session but can't seem to find a query or an example which may do this online.
Should I look at other tables?
I'm currently on an Oracle 11g database.
thanks Shaun
I have below situation.I have one table named UserCheckIn.
This contain the below column
Id, UserId, CheckInTime, CheckOutTime, CheckInStatus.
I want number of user checkedin in hourly, daily, weekly. Lets consider as hourly, like i will take hours 8 am -9 am, 9 am to 10 am etc. I want one sql/linq statement give result me in array or list format like 11,23,12.
How can i achieve this?
You can use event scheduler for this.
Create events as per your requirement. Hourly/Daily/Monthly etc.
Here is the link for your perusal. This has detailed information and sample codes as well. Please note your DB user needs to have required privileges for Events creation/usage.
My objective is to create a table that stores the information of the users and their passwords. Also, I want to keep track the number of failed logins. However, Here is a brief description about the tables:
First: the table of users
UserNo: ID of the user
Name: user's name
Email
Second: the table of login
UserNo: is the same as the last one
Password
Third: the table of tracking
UserNo
DateTime: date and time of login
count: counter that increases every time the user logins incorrectly.
From the last three tables, I do not think what I have done is a professional way. Is there any better ideas? especially with the tracking table.
Thanks
A couple of points:
There's no reason for your second table unless you're going to keep a password history (in which case you need an additional field to track the date the password was created)
In the third table, don't use a counter for the number of incorrect logins; instead, write a record for every login attempt (successful or otherwise), and use a query to retrieve the count whenever you need it.
Why are you keeping them separate? Why not just have a users table with the id, username, password hash, last login date and failed attempts counter?
Otherwise you could separate out login attempts to it's own table and record a status e.g. SUCCESS or FAIL so you can procedurally calculate number of failed attempts since the last successful one, and it also provides a full audit of logins, failed or not.
You might want to merge the first two tables together, as there is only one password per user, so it makes sense to have all the data for a user on one row in one table.
The third table records the timestamp of each login, and could also record the timestamp of each failed login if you added an extra column, then a quesy will give you a count of total failed logins per user.
Or, if you are counting the number of failed logins since last login, then you could just put the counter in the first table again.
You can put all your fields in a single table
UserID, UserName, UserEmail, Password, Login_Timestamp, Counter
then you can save all the values in a single table