Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2004, 09:55 AM
Registered User
 
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help writing a SQL

I have created a temp table (#TempTable) as follow...
     DATE HOURS TYPE TIME_BEG TIME_END
Sep 1 2004 12:00AM 5.0 P 12.0 17.0
Sep 6 2004 12:00AM 2.0 W 0.0 0.0
Sep 6 2004 12:00AM 2.0 HP 17.0 19.0
Sep 6 2004 12:00AM 1.0 HP 19.0 20.0
Sep 6 2004 12:00AM 3.0 HP 20.0 23.0
Sep 9 2004 12:00AM 4.0 P 0.0 0.0
Sep 9 2004 12:00AM 11.0 HWO 6.0 17.0
Sep 9 2004 12:00AM 1.0 HWO 17.0 18.0

There are serveral creteria that I want before I could sum up the total hours.
1. Same date
2. Same type
3. Time_End must be equal to the Time_Beg of other record.

For example...
There are 3 records on Sep 6 2004 that matches the above creteria.
Sep 6 2004 12:00AM 2.0 HP 17.0 19.0
Sep 6 2004 12:00AM 1.0 HP 19.0 20.0
Sep 6 2004 12:00AM 3.0 HP 20.0 23.0

Therefore, I would like to group them together as follow...
Sep 6 2004 12:00AM 6.0 HP 17.0 23.0

Here is another example...
There are 2 records on Sep 9 2004 that matches the creteria.
Sep 9 2004 12:00AM 11.0 HWO 6.0 17.0
Sep 9 2004 12:00AM 1.0 HWO 17.0 18.0

Therefore, I would like to group them together as follow...
Sep 9 2004 12:00AM 12.0 HWO 6.0 18.0

I hope I have explained this clearly to all of you.
Thanks in advance!!


 
Old October 6th, 2004, 01:56 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:)

Hi, reply for you;

select DATE,
    TYPE,
    sum(HOURS) SUM_HOURS,
    min(TIME_BEG) MIN_TIME_BEG,
    max(TIME_END) MAX_TIME_END
from @x
group by DATE, TYPE

 
Old October 6th, 2004, 02:16 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I've done mistake,
instead of @x must be #TempTable

Right script is :

select DATE,
        TYPE,
        sum(HOURS) SUM_HOURS,
        min(TIME_BEG) MIN_TIME_BEG,
        max(TIME_END) MAX_TIME_END
from #TempTable
group by DATE, TYPE






Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing DLL fom Report Rendering(SQL Server Repor) Somesh Reporting Services 0 December 31st, 2007 07:26 AM
I need help writing a SQL statement kscase SQL Server 2005 3 June 24th, 2007 02:43 PM
Help writing SQL Statement/ .net code for function carswelljr Classic ASP Databases 2 August 24th, 2006 03:31 PM
writing SQL queries in MS access,VBA NovieProgrammer Access VBA 2 April 2nd, 2005 07:15 PM
Writing plugins dotnetprogrammer VS.NET 2002/2003 0 December 29th, 2004 06:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.