473,473 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sum of hours by week

Hi,
I have the following query:

SELECT p.employee_code, p.employee_name,
CONVERT(VARCHAR(12),t.tran_date,101) AS TranDate,
CONVERT(VARCHAR(12),t.post_date,101) AS PostDate, SUM(tobill_hrs) AS
TotalHours
FROM tat_time t, hbm_persnl p
WHERE t.tk_empl_uno = p.empl_uno AND t.tran_date BETWEEN '2005-03-01'
AND '2005-03-31' AND p.employee_code IN ('0340')
GROUP BY p.employee_code, p.employee_name, t.tran_date, t.post_date
ORDER BY p.employee_code

I would like to sum the tobill_hrs by week (Monday-Sunday) instead of
by day. For instance the user will put in a date range (a from date
and to date).

Is this possible to do?

Thanks!

Jul 23 '05 #1
9 6551
On 19 Apr 2005 14:38:49 -0700, mc******@gmail.com wrote:
Hi,
I have the following query:

SELECT p.employee_code, p.employee_name,
CONVERT(VARCHAR(12),t.tran_date,101) AS TranDate,
CONVERT(VARCHAR(12),t.post_date,101) AS PostDate, SUM(tobill_hrs) AS
TotalHours
FROM tat_time t, hbm_persnl p
WHERE t.tk_empl_uno = p.empl_uno AND t.tran_date BETWEEN '2005-03-01'
AND '2005-03-31' AND p.employee_code IN ('0340')
GROUP BY p.employee_code, p.employee_name, t.tran_date, t.post_date
ORDER BY p.employee_code

I would like to sum the tobill_hrs by week (Monday-Sunday) instead of
by day. For instance the user will put in a date range (a from date
and to date).

Is this possible to do?

Thanks!


Hi mchen716,

You currently have two dates in your GROUP BY (tran_date and post_date);
for which one do you want to make a group for the whole week?

Assuming tran_date:

SELECT p.employee_code, p.employee_name,
CONVERT(VARCHAR(12),MIN(t.tran_date),101) AS TranDate,
CONVERT(VARCHAR(12),t.post_date,101) AS PostDate,
SUM(tobill_hrs) AS TotalHours
FROM tat_time t,
hbm_persnl p
WHERE t.tk_empl_uno = p.empl_uno
AND t.tran_date BETWEEN '20050301' AND '20050331'
AND p.employee_code IN ('0340')
GROUP BY p.employee_code, p.employee_name, t.post_date,
DATEPART(week, t.tran_date)
ORDER BY p.employee_code

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Hi Hugo,

Thanks for the fast reply. It is now grouping the hours, but not by
week, it is now grouping the hours for every 3 days. Is there
something missing in the code?

Thanks!

0340 User 03/01/2005 03/17/2005 31.0000
0340 User 03/04/2005 03/18/2005 17.2500
0340 User 03/07/2005 03/18/2005 43.5000
0340 User 03/10/2005 04/04/2005 21.5000
0340 User 03/14/2005 03/18/2005 6.0000
0340 User 03/15/2005 04/04/2005 47.0000
0340 User 03/25/2005 03/28/2005 8.0000
0340 User 03/22/2005 04/04/2005 50.0000
0340 User 03/30/2005 03/28/2005 16.0000
0340 User 03/29/2005 04/04/2005 8.0000

Jul 23 '05 #3
On 19 Apr 2005 15:01:13 -0700, mc******@gmail.com wrote:
Hi Hugo,

Thanks for the fast reply. It is now grouping the hours, but not by
week, it is now grouping the hours for every 3 days. Is there
something missing in the code?

Thanks!

0340 User 03/01/2005 03/17/2005 31.0000
0340 User 03/04/2005 03/18/2005 17.2500
0340 User 03/07/2005 03/18/2005 43.5000
0340 User 03/10/2005 04/04/2005 21.5000
0340 User 03/14/2005 03/18/2005 6.0000
0340 User 03/15/2005 04/04/2005 47.0000
0340 User 03/25/2005 03/28/2005 8.0000
0340 User 03/22/2005 04/04/2005 50.0000
0340 User 03/30/2005 03/28/2005 16.0000
0340 User 03/29/2005 04/04/2005 8.0000


Hi mchen716,

The data above looks correct to me. In your original query, you were
grouping by BOTH tran_date and post_date. In my suggested alternative, I
changed one of them (tran_date, IIRC) to combine all entries from the
same week into one group, but leave the others intact.

Checking the data above, I see:

* For post_date 03/17/2005: tran_date 03/01/2005 only
* For post_date 03/18/2005: tran_dates 03/04/2005, 03/07/2005, and
03/14/2005. The time difference between the first and second is only
three days, but they did cross a date boundary (for simplicity, I simply
included the earliest day from the week for which data happened to be
present - obviously, there was no data for 03/01 through 03/03).
* For post_date 04/04/2005: tran_dates 03/10/2005, 03/15/2005,
03/22/2005, and 03/29/2005. Again, one date from each week (and
apparently no data for 03/08 or 03/09).

But obviously, your requirement was different from what I thought it
was. To better explain what you need, please post DDL (CREATE TABLE
statements), sample data (as INSERT statements) and expected output.
More details: www.aspfaq.com/5006.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4
Hi Hugo,

Please see the below data:

Employee_code Employee_name tran_date post_date totalhours
340 User 3/1/2005 3/17/2005 7.5
340 User 3/2/2005 3/17/2005 12.25
340 User 3/3/2005 3/17/2005 11.25
340 User 3/4/2005 3/18/2005 10
340 User 3/5/2005 3/18/2005 3
340 User 3/6/2005 3/18/2005 4.25
340 User 3/7/2005 3/18/2005 8
340 User 3/8/2005 3/18/2005 16.5
340 User 3/9/2005 3/18/2005 16.5
340 User 3/13/2005 3/18/2005 2.5
340 User 3/14/2005 3/18/2005 6
340 User 3/25/2005 3/28/2005 8
340 User 3/30/2005 3/28/2005 8
340 User 3/31/2005 3/28/2005 8
340 User 3/10/2005 4/4/2005 12.5
340 User 3/11/2005 4/4/2005 5
340 User 3/13/2005 4/4/2005 4
340 User 3/15/2005 4/4/2005 12.5
340 User 3/16/2005 4/4/2005 15.75
340 User 3/17/2005 4/4/2005 12.25
340 User 3/18/2005 4/4/2005 4.75
340 User 3/20/2005 4/4/2005 1.75
340 User 3/22/2005 4/4/2005 13.75
340 User 3/23/2005 4/4/2005 16
340 User 3/24/2005 4/4/2005 20.25
340 User 3/29/2005 4/4/2005 8

I would like to sum up the total hours by week with monday - sunday as
the week using tran_date.
For instance I the user enters the date range of 3/1/2005-3/31/2005.
The first week would be 2/28/2005 - 3/6/2005 and second week would be
3/7/2005 - 3/13/2005 and so on. The total hours for the first week
should be 48.25 and second should be 43.5
I hope this helps, thanks for all your help, I really appreciate it.

Thanks,
Michelle

Jul 23 '05 #5
On 21 Apr 2005 09:11:21 -0700, mc******@gmail.com wrote:
Hi Hugo,

Please see the below data:

Employee_code Employee_name tran_date post_date totalhours
340 User 3/1/2005 3/17/2005 7.5
340 User 3/2/2005 3/17/2005 12.25
340 User 3/3/2005 3/17/2005 11.25
340 User 3/4/2005 3/18/2005 10
340 User 3/5/2005 3/18/2005 3
340 User 3/6/2005 3/18/2005 4.25
340 User 3/7/2005 3/18/2005 8
340 User 3/8/2005 3/18/2005 16.5
340 User 3/9/2005 3/18/2005 16.5
340 User 3/13/2005 3/18/2005 2.5
340 User 3/14/2005 3/18/2005 6
340 User 3/25/2005 3/28/2005 8
340 User 3/30/2005 3/28/2005 8
340 User 3/31/2005 3/28/2005 8
340 User 3/10/2005 4/4/2005 12.5
340 User 3/11/2005 4/4/2005 5
340 User 3/13/2005 4/4/2005 4
340 User 3/15/2005 4/4/2005 12.5
340 User 3/16/2005 4/4/2005 15.75
340 User 3/17/2005 4/4/2005 12.25
340 User 3/18/2005 4/4/2005 4.75
340 User 3/20/2005 4/4/2005 1.75
340 User 3/22/2005 4/4/2005 13.75
340 User 3/23/2005 4/4/2005 16
340 User 3/24/2005 4/4/2005 20.25
340 User 3/29/2005 4/4/2005 8

I would like to sum up the total hours by week with monday - sunday as
the week using tran_date.
For instance I the user enters the date range of 3/1/2005-3/31/2005.
The first week would be 2/28/2005 - 3/6/2005 and second week would be
3/7/2005 - 3/13/2005 and so on. The total hours for the first week
should be 48.25 and second should be 43.5
I hope this helps, thanks for all your help, I really appreciate it.

Thanks,
Michelle


Hi Michelle,

I can't test it (since you didn't post the data as INSERT statements,
nor a CREATE TABLE statement), but I think that this will do the job:

SELECT p.employee_code, p.employee_name,
CONVERT(VARCHAR(12),MIN(t.tran_date),101) AS TranDate
SUM(tobill_hrs) AS TotalHours
FROM tat_time t,
hbm_persnl p
WHERE t.tk_empl_uno = p.empl_uno
AND t.tran_date BETWEEN '20050301' AND '20050331'
AND p.employee_code IN ('0340')
GROUP BY p.employee_code, p.employee_name,
DATEPART(week, t.tran_date)
ORDER BY p.employee_code

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #6
Hi Hugo,

I'm sorry I didn't post the data correctly. The script seems to work
now. I noticed that you took out the post date. Is that the only way
that this script can work?

Thanks,
Michelle

Jul 23 '05 #7
On 21 Apr 2005 14:39:48 -0700, mc******@gmail.com wrote:
Hi Hugo,

I'm sorry I didn't post the data correctly. The script seems to work
now. I noticed that you took out the post date. Is that the only way
that this script can work?

Thanks,
Michelle
Hi Michelle,

It's not the only way, but you have to do _something_ with post date,
you can't just leace it as it was in the SELECT. Check the following two
rows from your sample:
340 User 3/9/2005 3/18/2005 16.5
340 User 3/10/2005 4/4/2005 12.5


Both have a tran_date in the same week, so they should be combined. But
they have a different post_date. Which one would you want to show?

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #8
Hi Hugo,

I believe that the user would want to see the tran_date group instead
of the post_date. If they want to see it grouped by post_date then I
could just put post_date instead of tran_date in the script right?
Thanks again for your help.

Michelle

Jul 23 '05 #9
On 22 Apr 2005 07:55:04 -0700, mc******@gmail.com wrote:
Hi Hugo,

I believe that the user would want to see the tran_date group instead
of the post_date. If they want to see it grouped by post_date then I
could just put post_date instead of tran_date in the script right?
Hi Michelle,

Yes, that's correct.

Thanks again for your help.


You're welcome :-)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: anon | last post by:
Does anyone know how many man-hours were used in creating .NET 1.0? I thought it would be an interesting statistic to know. Thanks.
0
by: Guoqi Zheng | last post by:
Dear sir, We have an intranet which we need our employee to book their hours every week. Every week, employees will be assigned with multiple tasks. So we will present a form with X-column,...
31
by: Bob | last post by:
I have recently joined a healthcare company where I am the solo programmer. I am going to be starting work on a project. The management has asked me to provide an estimate of hours I am going to...
4
by: clintonG | last post by:
On an XP Pro machine with a current and synchronized system time GMT (-06:00) Central Time my use of the Visual Studio 2005 Website Administration Tool (WAT) is writing users into the SQL Server...
3
by: Stephen Chaplin | last post by:
I'm summing up time spent on jobs over a week, some of these jobs last greater than 24 hours and when access sums these up it appears that it starts again at 00:00 once it gets past 23:59. Is it...
3
by: jbosrock | last post by:
Hi to all, Please bear with me as I am newly experienced in basic Access 2003 only. I don't know Visual Basic or macros at all but am attempting to learn on my own. Explanation: Our fleet...
18
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st...
3
by: macca | last post by:
Just out of interest, how many hours on average do you work each week as a coder/programmer/developer etc?
10
by: imran akhtar | last post by:
#Workers in a company are paid £8 per hour for each hour they work up to a #basic working week of 40 hours, and £12 an hour for any overtime they work. #Write a program that asks the user to enter...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.