You are here: Re: sum a table within a table « MsSQL Server « IT news, forums, messages
Re: sum a table within a table

Posted by Erland Sommarskog on 02/18/07 12:15

(solonzenetzis@sbzsystems.com) writes:
> 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@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация