dbTalk Databases Forums  

sum a table within a table

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss sum a table within a table in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
solonzenetzis@sbzsystems.com
 
Posts: n/a

Default sum a table within a table - 02-18-2007 , 05:54 AM






hello

I need some help. The following query works perfectly:
------------------------------------------------------------------
select *
from tableA, tableB

where tableA.num=tableB.order

and tableA.num in (
select tableB.order from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
)
------------------------------------------------------------------
I need to do the following:
------------------------------------------------------------------
select *
from tableA, tableB

where tableA.num=tableB.order

and tableA.num in (
select tableB.order, sum(tableB.ok) as s_ok from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
and s_ok<5
)
------------------------------------------------------------------
but it is not possible
Can anyone help me?

thank you in advance


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: sum a table within a table - 02-18-2007 , 06:15 AM






(solonzenetzis (AT) sbzsystems (DOT) com) writes:
Quote:
select *
from tableA, tableB

where tableA.num=tableB.order

and tableA.num in (
select tableB.order, sum(tableB.ok) as s_ok from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
and s_ok<5
)
------------------------------------------------------------------
but it is not possible
Can anyone help me?
As given the subquery could be written as

select tableB.order, sum(tableB.ok) as s_ok from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
GROUP BY tableB.order
HAVING SUM(tableB.ok) < 5

But something tells me that it should not be necessary to include noth
table twice in the whole query, and that you should use EXISTS instead.
But since I don't have the full picture I cannot suggest an alternative.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: sum a table within a table - 02-22-2007 , 10:57 AM



solonzenetzis (AT) sbzsystems (DOT) com wrote:

Quote:
I need some help. The following query works perfectly:
------------------------------------------------------------------
select *
from tableA, tableB

where tableA.num=tableB.order

and tableA.num in (
select tableB.order from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
)
FYI, better version of this query:

select *
from tableA
join tableB on tableA.num = tableB.order
where tableB.cust_no = 4895

Quote:
I need to do the following:
------------------------------------------------------------------
select *
from tableA, tableB

where tableA.num=tableB.order

and tableA.num in (
select tableB.order, sum(tableB.ok) as s_ok from tableA, tableB
where
tableB.order=tableA.num
and tableB.cust_no=4895
and s_ok<5
)
select tableA.*, tableB.*
from tableA
join tableB on tableA.num = tableB.order
join (
select order, sum(ok)
from tableB
group by order
having sum(ok) < 5
) B_subset on tableB.order = B_subset.order
where tableB.cust_no = 4895

If you need tableA in the sub-query (i.e. some rows in tableB may match
more or less than one row in tableA), then change the sub-query to

select tableB.order, sum(tableB.ok)
from tableB
join tableA on tableB.order = tableA.num
group by tableB.order
having sum(tableB.ok) < 5


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.