Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL join query

Posted on 2009-04-07
10
218 Views
Last Modified: 2012-05-06
Hi,

I have a database table with 4 columns, username, loginDate, loginTime, logoutTime.  This table get populated nightly from a text file.  I need to draw from this table the username, the date, the min(loginTime) and the max(logoutTime) for that username and date, and also return the total hours.  I know this can be done using an inner join of some sort but beyond that I really don't know how to start on it.  I could easily do it using multiple queries but since I am pulling it into an ASP.NET Datasource control it has to be condensed into a single query.

Any suggestions would be greatly appreciated.
Thanks,
Geoff
0
Comment
Question by:GeoffSutton
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24086800
what values do the logintime and logouttime hold?  are they datetime fields?
0
 
LVL 16

Accepted Solution

by:
Auric1983 earned 250 total points
ID: 24086904
select username,loginDate, min(loginTime) as LoginTime, max(Logouttime) as Logouttime from table group by username,Logindate

0
 
LVL 11

Expert Comment

by:Sven
ID: 24086912
SELECT loginDate, username, MIN(loginTime), MAX(logoutTime) FROM tbl GROUP BY loginDate, username

Hours could be calculated within .NET code.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 22

Assisted Solution

by:pivar
pivar earned 250 total points
ID: 24087022
Hi,

You could calculate hours with DATEDIFF(hh, min(loginTime), max(Logouttime))


/pter
0
 
LVL 10

Author Comment

by:GeoffSutton
ID: 24087027
LoginTime and Logout time are both stored as nvarchar.  
@DarthSonic - No, hours cannot be calculated in the .net Code.  I am using a datasource and don't want to be having to individually parsing each row to calculate the hours.  I am reasonably certain SQL server can do that for me.
Geoff
0
 
LVL 10

Author Comment

by:GeoffSutton
ID: 24087034
Thanks Peter.  I'll try that.
0
 
LVL 22

Expert Comment

by:pivar
ID: 24087051
Depending on dateformat, you may have to use some sort of conversion. How does your format look like?
0
 
LVL 10

Author Comment

by:GeoffSutton
ID: 24087067
@pter - The DateDiff doesn't work because the columns are nvarchar.  I tried casting them with no success.  Will try converting.
Geoff
0
 
LVL 22

Expert Comment

by:pivar
ID: 24087110
You were right the first time. My name is Peter. My fingers were faster than my brain. ;-)

What format do you have for the dates?
0
 
LVL 10

Author Comment

by:GeoffSutton
ID: 24087724
It was nvarchar.  I solved it(with a lot of help from you guys) with:
SELECT     username, loginDate, MIN(loginTime) AS LoginTime, MAX(logoutTime) AS Logouttime, DATEDIFF(hh, MIN(CONVERT(DateTime, loginTime)),
                      MAX(CONVERT(DateTime, logoutTime))) AS hours
FROM         tblAttendanceScript
GROUP BY username, loginDate
I was sure I would have to use a join of some sort as i have had to use in the past.  I suppose I was just being lazy this morning, wanting to let someone else do my thinking for me :)  The only thing I have left is to ensure the data in the time columns is the correct type, and I can do that on the insert.
Thanks to all for the help.
Geoff
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question