473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UNION ALL query - HELP!

I am having conceptual trouble with the following query:

select r.ServiceID,r.C ontractID,sum(T otal)
from (
select
csc.ServiceID,c .ContractID, sum(csc.Contain erMovement) as Total
from
iwms_tbl_Custom erSiteContainer s csc,
iwms_tbl_Contra ctLines cl,
iwms_tbl_Contra cts c,
iwms_tbl_Contra ctLinePricing clp
where
clp.ContractLin eID = cl.ContractLine ID and
clp.ContractPri ceLineDescripti on = 'Rental' and
clp.ContractPri ceLineActive=1 and
clp.ContractPri ceLineExpiry > getdate() and
csc.ServiceID > 1 and
c.ContractID = cl.ContractID and
c.ContractStatu sCode = 5 and
cl.ServiceID = csc.ServiceID
group by
c.ContractID,cs c.ServiceID
union all
select
si.ServiceID,c. ContractID,sum( j.QuantityColle cted)-sum(j.QuantityD elivered)
as Total
from
iwms_tbl_Jobs j,
iwms_tbl_Servic eInstances si,
iwms_tbl_Contra ctLines cl,
iwms_tbl_Contra cts c,
iwms_tbl_Contra ctLinePricing clp
where
clp.ContractLin eID = cl.ContractLine ID and
clp.ContractPri ceLineDescripti on = 'Rental' and
clp.ContractPri ceLineActive=1 and
clp.ContractPri ceLineExpiry > getdate() and
c.ContractID = cl.ContractID and
c.ContractStatu sCode = 5 and
cl.ServiceID = si.ServiceID and
j.JobStatusCode <> 80 and
j.ServiceInstan ceID = si.ServiceInsta nceID and
si.ServiceID > 1
group by
c.ContractID,si .ServiceID
) as r
group by
r.ContractID,r. ServiceID
having
sum(Total) <> 0
order by r.ContractID

It returns 140 rows. However, if I comment out the first select
statement inside the brackets (select csc.ServiceID,c .ContractID
....union all) and run it, it returns 4,785 rows. If I comment out the
second select statement (union all ...group by
c.ContractID,si .ServiceID) it returns 4,786 rows. So why doesn't the
*whole* thing return 9,571 rows? That's what I thought a UNION did -
append the results of one select to the bottom of the second select.

I will supply table defs if it will help, but there's a lot of stuff
here and I think it isn't a data question, but an
I-don't-understand-the-SQL question!

TIA

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk
Jul 20 '05 #1
3 5371
Edward:

My guess is that you have duplicates in there. Try using Union All.

HTH,

Thanks,

Berny Zamora

te********@hotm ail.com (Edward) wrote in message news:<25******* *************** ****@posting.go ogle.com>...
I am having conceptual trouble with the following query:

select r.ServiceID,r.C ontractID,sum(T otal)
from (
select
csc.ServiceID,c .ContractID, sum(csc.Contain erMovement) as Total
from
iwms_tbl_Custom erSiteContainer s csc,
iwms_tbl_Contra ctLines cl,
iwms_tbl_Contra cts c,
iwms_tbl_Contra ctLinePricing clp
where
clp.ContractLin eID = cl.ContractLine ID and
clp.ContractPri ceLineDescripti on = 'Rental' and
clp.ContractPri ceLineActive=1 and
clp.ContractPri ceLineExpiry > getdate() and
csc.ServiceID > 1 and
c.ContractID = cl.ContractID and
c.ContractStatu sCode = 5 and
cl.ServiceID = csc.ServiceID
group by
c.ContractID,cs c.ServiceID
union all
select
si.ServiceID,c. ContractID,sum( j.QuantityColle cted)-sum(j.QuantityD elivered)
as Total
from
iwms_tbl_Jobs j,
iwms_tbl_Servic eInstances si,
iwms_tbl_Contra ctLines cl,
iwms_tbl_Contra cts c,
iwms_tbl_Contra ctLinePricing clp
where
clp.ContractLin eID = cl.ContractLine ID and
clp.ContractPri ceLineDescripti on = 'Rental' and
clp.ContractPri ceLineActive=1 and
clp.ContractPri ceLineExpiry > getdate() and
c.ContractID = cl.ContractID and
c.ContractStatu sCode = 5 and
cl.ServiceID = si.ServiceID and
j.JobStatusCode <> 80 and
j.ServiceInstan ceID = si.ServiceInsta nceID and
si.ServiceID > 1
group by
c.ContractID,si .ServiceID
) as r
group by
r.ContractID,r. ServiceID
having
sum(Total) <> 0
order by r.ContractID

It returns 140 rows. However, if I comment out the first select
statement inside the brackets (select csc.ServiceID,c .ContractID
...union all) and run it, it returns 4,785 rows. If I comment out the
second select statement (union all ...group by
c.ContractID,si .ServiceID) it returns 4,786 rows. So why doesn't the
*whole* thing return 9,571 rows? That's what I thought a UNION did -
append the results of one select to the bottom of the second select.

I will supply table defs if it will help, but there's a lot of stuff
here and I think it isn't a data question, but an
I-don't-understand-the-SQL question!

TIA

Edward

Jul 20 '05 #2
> It returns 140 rows. However, if I comment out the first select
statement inside the brackets (select csc.ServiceID,c .ContractID
...union all) and run it, it returns 4,785 rows. If I comment out the
second select statement (union all ...group by
c.ContractID,si .ServiceID) it returns 4,786 rows. So why doesn't the
*whole* thing return 9,571 rows? That's what I thought a UNION did -
append the results of one select to the bottom of the second select.


Hi,

I think if you comment out the outer-most group by and having clauses
and the "sum(total) ", it will return 9,571 rows.

select r.ServiceID,r.C ontractID --,sum(Total)
.....
/***group by r.ContractID,r. ServiceID
having sum(Total) <> 0 ***/
Jul 20 '05 #3
Edward (te********@hot mail.com) writes:
It returns 140 rows. However, if I comment out the first select
statement inside the brackets (select csc.ServiceID,c .ContractID
...union all) and run it, it returns 4,785 rows. If I comment out the
second select statement (union all ...group by
c.ContractID,si .ServiceID) it returns 4,786 rows. So why doesn't the
*whole* thing return 9,571 rows? That's what I thought a UNION did -
append the results of one select to the bottom of the second select.


Yeah, then you have this at the end:

group by r.ContractID,r. ServiceID
having sum(Total) <> 0

So obviously most combinations of ContraceID and ServiceID in the two
virtual table has a matching Total, but not all.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

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

Similar topics

3
5447
by: Matias Silva | last post by:
Hi Everyone, I wrote a for loop to build several select statements that are combined with a UNION. When I execute one of the queries separately, it works, but when I execute the query with a UNION it returns: ERROR 2013 (HY000): Lost connection to MySQL server during query I am able to run additional queries after I get the error, so the connection is not lost. I have tried running the query with the union on MySQL Query Browser and...
3
10542
by: Dalan | last post by:
From reading Access 97 help text, it seems that to do what I need to do will require a Union Query. As this would be my first, I think I might require a little guidance. I have two tables with 10 fields that have like data (for instance both have an item description field, an item price field, a general notes field, etc.) but with different field names. The tables have approximately 20/40 other fields that are dissimilar and not needed...
1
6174
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in the one graph. The output was in the order of the months, but after unioning with the averages SQL code, the order is lost. Below is the full sql code that is the data source for the graph: SELECT (Format(.,"mmm"" '""yy")) AS Month,...
4
1935
by: Missy | last post by:
We’ve recently upgraded our computer system to XP. My union query (which was working perfectly for years) now returns hieroglyphics instead of invoice numbers. When I run the 2 queries separately (of which the Union query is made up of) they return numbers perfectly. Can you please help? The Union Query looks like this: SELECT * FROM UNION SELECT * FROM ;
4
7817
by: spam | last post by:
If I run the following query in Access 2002 then I get the expected result: SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp; I get the contents of both tables with no duplicates. If I add INSERT INTO, then it doesn't work:
1
5825
by: forey | last post by:
Hi All, I'm trying to find the best way to accomplish the following: I have a union query in an Access XP database (pasted below) SELECT Contacts.Company,Contacts.dba, Contacts.Misc1, Contacts.Misc2, Contacts.Address1, Contacts.City, Contacts.State, Contacts.Zip1, Contacts.top_50, Contacts.B_ID as BID,'BCM' as thesource FROM Contacts
5
2279
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed the query produced and opened it from a MS Query it started giving strange results. The first query when run alone returns 22 records, some of which have identical values in all fields. This is 100% correct. The second query returns nothing....
7
3312
by: KoliPoki | last post by:
Hello every body. I have a small issue. Problem: I have a table with 4 descriptor columns (type). I need to formulate a query to retrieve a count for each type so I can group by...etc. The view I have works, but doesn't work when I supplement the query with some functions... they just don't like the UNION. The real problem is I can't change any of the udf's or queries, just the view. The view is inner joined back on to the primary...
1
2679
by: bgreenspan | last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am doing and what I tried. My database has entries with Contract Names and Expiry Dates, among other fields. I have a form designed to show the expiring contracts. To do this I use a straight forward query in my form's ON LOAD code strwhere = " BETWEEN #" & Now() & "# AND #" & DateAdd("m", 6, Now()) & "#" Set MyQueryDef = MyDatabase.CreateQueryDef("qryMattersQuery",...
27
13786
by: MLH | last post by:
How can I turn the following into a make-table query? SELECT & " " & AS Recipient FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON tblVehicleJobs.VehicleJobID = tblAddnlOwnrs.VehicleJobID WHERE tblVehicleJobs.VehicleJobID=GetCurrentVehicleJobID(); UNION SELECT AS Recipient FROM tblVehicleJobs INNER JOIN tblLienHolders ON tblVehicleJobs.VehicleJobID = tblLienHolders.VehicleJobID WHERE
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.