SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query-restrict output


Query-restrict output

Author
Message
Peter D-251663
Peter D-251663
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 223
I have a system that logs users who use the system daily. Some user may log in several times during the day which creates additional entries into the logging table in a date format as "'2012-06-01 00:50:59.000' "

What I'm trying to do is query and return back just one entry for the user (s) even if they are captured in the logging table more than once per day
(i.e. jbaker .'2012-06-01 00:50:59.000'
'2012-06-01 12:50:59.000'
'2012-06-01 15:50:59.000' )

I tried selecting distinct user (oprid) from the logging table (psaccesslog)but still came back with multiple entries for the day.


SELECT * FROM PSACCESSLOG
WHERE LOGINDTTM BETWEEN '2012-06-01 00:50:59.000' AND '2012-07-01 00:50:59.000'
ORDER BY OPRID, LOGINDTTM

Appreciate insight
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60413 Visits: 17954
When there are multiples which one do you want? It be far more helpful if you could post ddl and sample data with your problem. See the first link in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40608 Visits: 19814
I don't know the whole structure of your table, but this might give you something to start.
You might need to add some fields or add the extra time to get the 00:59:50.

Be aware that BETWEEN is inclusive for both values, so you might have values that get in two days.

CREATE TABLE #PSACCESSLOG(
oprid varchar(6),
logindttm datetime)

INSERT INTO #PSACCESSLOG
SELECT 'jbaker', '2012-06-01 00:50:59.000' UNION ALL
SELECT 'jbaker', '2012-06-01 12:50:59.000' UNION ALL
SELECT 'jbaker', '2012-06-01 15:50:59.000'

SELECT oprid,
DATEADD( dd, DATEDIFF( dd, 0, LOGINDTTM), 0) AS LOGINDT,
COUNT(*) AS entries
FROM #PSACCESSLOG
WHERE LOGINDTTM BETWEEN '2012-06-01 00:50:59.000' AND '2012-07-01 00:50:59.000'
GROUP BY oprid, DATEADD( dd, DATEDIFF( dd, 0, LOGINDTTM), 0)
ORDER BY OPRID, LOGINDT

DROP TABLE #PSACCESSLOG




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Peter D-251663
Peter D-251663
SSC-Enthusiastic
SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)SSC-Enthusiastic (182 reputation)

Group: General Forum Members
Points: 182 Visits: 223
Thanks looks like this will work. Thanks everyone for your input. Evena 50yr can still learn things..Cool
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search