473,396 Members | 2,002 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,396 software developers and data experts.

UNION ALL query - HELP!

I am having conceptual trouble with the following query:

select r.ServiceID,r.ContractID,sum(Total)
from (
select
csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total
from
iwms_tbl_CustomerSiteContainers csc,
iwms_tbl_ContractLines cl,
iwms_tbl_Contracts c,
iwms_tbl_ContractLinePricing clp
where
clp.ContractLineID = cl.ContractLineID and
clp.ContractPriceLineDescription = 'Rental' and
clp.ContractPriceLineActive=1 and
clp.ContractPriceLineExpiry > getdate() and
csc.ServiceID > 1 and
c.ContractID = cl.ContractID and
c.ContractStatusCode = 5 and
cl.ServiceID = csc.ServiceID
group by
c.ContractID,csc.ServiceID
union all
select
si.ServiceID,c.ContractID,sum(j.QuantityCollected)-sum(j.QuantityDelivered)
as Total
from
iwms_tbl_Jobs j,
iwms_tbl_ServiceInstances si,
iwms_tbl_ContractLines cl,
iwms_tbl_Contracts c,
iwms_tbl_ContractLinePricing clp
where
clp.ContractLineID = cl.ContractLineID and
clp.ContractPriceLineDescription = 'Rental' and
clp.ContractPriceLineActive=1 and
clp.ContractPriceLineExpiry > getdate() and
c.ContractID = cl.ContractID and
c.ContractStatusCode = 5 and
cl.ServiceID = si.ServiceID and
j.JobStatusCode <> 80 and
j.ServiceInstanceID = si.ServiceInstanceID 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 5360
Edward:

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

HTH,

Thanks,

Berny Zamora

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

select r.ServiceID,r.ContractID,sum(Total)
from (
select
csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total
from
iwms_tbl_CustomerSiteContainers csc,
iwms_tbl_ContractLines cl,
iwms_tbl_Contracts c,
iwms_tbl_ContractLinePricing clp
where
clp.ContractLineID = cl.ContractLineID and
clp.ContractPriceLineDescription = 'Rental' and
clp.ContractPriceLineActive=1 and
clp.ContractPriceLineExpiry > getdate() and
csc.ServiceID > 1 and
c.ContractID = cl.ContractID and
c.ContractStatusCode = 5 and
cl.ServiceID = csc.ServiceID
group by
c.ContractID,csc.ServiceID
union all
select
si.ServiceID,c.ContractID,sum(j.QuantityCollected)-sum(j.QuantityDelivered)
as Total
from
iwms_tbl_Jobs j,
iwms_tbl_ServiceInstances si,
iwms_tbl_ContractLines cl,
iwms_tbl_Contracts c,
iwms_tbl_ContractLinePricing clp
where
clp.ContractLineID = cl.ContractLineID and
clp.ContractPriceLineDescription = 'Rental' and
clp.ContractPriceLineActive=1 and
clp.ContractPriceLineExpiry > getdate() and
c.ContractID = cl.ContractID and
c.ContractStatusCode = 5 and
cl.ServiceID = si.ServiceID and
j.JobStatusCode <> 80 and
j.ServiceInstanceID = si.ServiceInstanceID 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.ContractID --,sum(Total)
.....
/***group by r.ContractID,r.ServiceID
having sum(Total) <> 0 ***/
Jul 20 '05 #3
Edward (te********@hotmail.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
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...
3
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...
1
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...
4
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...
4
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...
1
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,...
5
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...
7
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...
1
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...
27
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.