Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 November 9th, 2004, 07:25 AM
Authorized User
 
Join Date: Aug 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with UNION - Please Help

I have the following sql which works:

SELECT scheduleddtdesc AS REASONCODE, SUM(scheduleddthours) AS [DOWNTIME]
FROM staworkpatterns
INNER JOIN tbllines ON staworkpatterns.workcentreid = tbllines.line
INNER JOIN staarea ON tbllines.areaid = staarea.areaid
INNER JOIN stafacilitylocation ON staarea.facilityid = stafacilitylocation.facilityid
WHERE scheduleddthours <> 0
AND staFacilityLocation.FacilityID = 6474
AND staArea.AreaID = 6475
AND tblLines.Line = 'M-BP-2'
AND currentdate >= '1-Sep-2004'
AND currentdate < '10-Nov-2004'
GROUP BY scheduleddtdesc

UNION

SELECT REASONCODE, SUM(DATEDIFF(mi,StopTime,StartTime)) as [DOWNTIME]
FROM tblDowntime
INNER JOIN tblPo on tblPO.xlink = tbldowntime.xlink
WHERE DowntimeType = 'SD'
AND tbldowntime.xLink IN (SELECT xLink FROM TBLPO INNER JOIN tblLines ON tblLines.Line = tblPO.Line INNER JOIN staArea ON staArea.AreaID = tblLines.AreaID INNER JOIN staFacilityLocation ON staFacilityLocation.FacilityID = staArea.FacilityID WHERE OEERATE > 0 AND OEETIMEUNIT > 0 AND staFacilityLocation.FacilityID = 6474 AND staArea.AreaID = 6475 AND tblLines.Line = 'M-BP-2' AND date >= '1-Sep-2004'AND date < '10-Nov-2004')
GROUP BY REASONCODE
ORDER BY DOWNTIME DESC


This code produces the following:

REASONCODE DOWNTIME
---------- --------
Breaks 680.00
Breaks 8.30


My problem is that i need it to group the breaks so that I get a downtime of 688.30

Any help woulb be greatly appreciated.

Thanks.:(
 
Old November 9th, 2004, 08:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this.
Code:
Select ReasonCode, Sum(Downtime) 
From
(
SELECT scheduleddtdesc AS REASONCODE, SUM(scheduleddthours) AS DOWNTIME
FROM staworkpatterns
INNER JOIN tbllines ON staworkpatterns.workcentreid = tbllines.line
INNER JOIN staarea ON tbllines.areaid = staarea.areaid
INNER JOIN stafacilitylocation ON staarea.facilityid = stafacilitylocation.facilityid
WHERE scheduleddthours <> 0
AND staFacilityLocation.FacilityID = 6474
AND staArea.AreaID = 6475
AND tblLines.Line = 'M-BP-2'
AND currentdate >= '1-Sep-2004'
AND currentdate < '10-Nov-2004'
GROUP BY scheduleddtdesc

UNION

SELECT REASONCODE, SUM(DATEDIFF(mi,StopTime,StartTime)) as DOWNTIME
FROM tblDowntime
INNER JOIN tblPo on tblPO.xlink = tbldowntime.xlink
WHERE DowntimeType = 'SD'
AND tbldowntime.xLink IN (SELECT xLink FROM TBLPO INNER JOIN tblLines ON tblLines.Line = tblPO.Line INNER JOIN staArea ON staArea.AreaID = tblLines.AreaID INNER JOIN staFacilityLocation ON staFacilityLocation.FacilityID = staArea.FacilityID WHERE OEERATE > 0 AND OEETIMEUNIT > 0 AND staFacilityLocation.FacilityID = 6474 AND staArea.AreaID = 6475 AND tblLines.Line = 'M-BP-2' AND date >= '1-Sep-2004'AND date < '10-Nov-2004')
GROUP BY REASONCODE
ORDER BY DOWNTIME DESC
) as DerivedTable
Group by ReasonCode
Order by ReasonCode
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
UNION and ORDER BY darcome SQL Language 11 October 10th, 2009 07:52 AM
Union Qry Corey Access 1 November 20th, 2006 06:21 AM
UNION query. rupen Access 3 April 28th, 2006 02:49 AM
UNION of Tables neo_jakey Classic ASP Professional 2 January 31st, 2006 03:08 PM





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