473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SQL & GROUP BY

I have a rather simple SQL statement, but I can't get it to work properly. I have four columns in the activities table "LoginID", "StartDateTime", "StopDateTime", and "ProjectID". In a second table, Projects, I have "ProjectID" and "ProjectName". The goal of the SQL statement is to join the two ProjectID together, and get the difference in the stop and start time and any projects that are the same, sum or group them together. My SQL statement looks like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT datediff('n', [StartDateTime],[StopDateTime]) AS TotalDay, Projects.ProjectName 
  2. FROM Activity INNER JOIN Projects ON Activity.ProjectID = Projects.ProjectID 
  3. GROUP BY Projects.ProjectName, Activity.LoginID, Projects.ProjectID, Activity.StartDateTime, Activity.StopDateTime 
  4. HAVING (Activity.StartDateTime Between #9/25/2008# AND #9/25/2008 8:13:29 AM# AND Activity.LoginID=1) 
  5. ORDER BY Projects.ProjectName
Long, but straight forward. I've tried adding SUM around the datediff, but that doesn't work. In the activities table I have two entries that the SQL statement selects and the "ProjectID" are identical on both of them, but the project is I keep getting back both entries (the same two rows), with the correct time difference. What I expect to get back is one row with the values/times combined. Does anyone know how I can solve this problem with a single SQL statement, multiple SQL statements, or an operation on a dataset?

Thanks in advance,

Harry
Sep 25 '08 #1
1 1875
coolsti
310 100+
I don't have any time to look into this in detail but your group_by clause looks way too wild. Too many things you are grouping by and this should not be necessary.

First of all, to get this query working, stop using the Projects table. This table is only supplying you with the project name for the given project ID, and so you can attempt to get the query working on the basis of Project ID alone. Once you get the query like you wish it, it will be not so hard to add a join to the Projects table to extract the project name as well.

You should be able to do what you want in one query. You can calculate the time interval for each row as I think you are doing. Then try to do a group by projectID with a sum(TotalDay). This should give you the total time used for the project by all users. You could then do a group by projectID,LoginID to also break it down to projectID and user.

If you are only looking to extract one user at a time always, then the query may be more efficient to drop the LoginID from the group by clause, and instead add a "where LoginID = XXX" just before the group by clause.
Sep 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mali_djuro | last post by:
Hi all, i used JDOM to create XML file. first, i get data from database and create Document object. in some data i have quotas, so it makes replacment in content of elements. for example: if i...
2
by: Francesco Moi | last post by:
Hello. I designed a form to edit some DataBase's fields. But some of these fields contain '&lt;' and '&gt;' characters. And these characters are '<' and '>' in HTML. So if want to edit these...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: Rik | last post by:
Hello, first of all, my provider sucks, newsserver is down for the #nth time now, offcourse when I have an urgent question.... So this will be me first time using Google Groups, forgive me if...
1
by: Stropher | last post by:
If one clicks useraccount via start/settings/control panel one finds the (created) users of a given computer under the reiter Username, their domains under the reiter Domain and their groups under...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
6
by: avcitamer | last post by:
We upgraded our system and problem below occured, pleas help me... Windows 2003 server SP1 When I set a decimal DB field value to "123,32" (using ADODB.recordset ) updated value was ok (123.32)...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.