Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query-restrict output Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 5:11 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 27, 2014 11:50 AM
Points: 46, Visits: 210
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
Post #1384935
Posted Thursday, November 15, 2012 7:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:27 PM
Points: 13,198, Visits: 12,681
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 Moden's 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)
Post #1385151
Posted Thursday, November 15, 2012 9:12 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:29 PM
Points: 3,909, Visits: 8,848
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1385232
Posted Friday, November 16, 2012 9:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 27, 2014 11:50 AM
Points: 46, Visits: 210
Thanks looks like this will work. Thanks everyone for your input. Evena 50yr can still learn things..
Post #1385900
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse