473,385 Members | 1,622 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,385 software developers and data experts.

sum a table within a table

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

Feb 18 '07 #1
2 1421
(so***********@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, es****@sommarskog.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
Feb 18 '07 #2
so***********@sbzsystems.com wrote:
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
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
Feb 22 '07 #3

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

Similar topics

36
by: toedipper | last post by:
Hello, I am designing a table of vehicle types, nothing special, just a list of unique vehicle types such as truck, lorry, bike, motor bike, plane, tractor etc etc For the table design I am...
5
by: Josh Renaud | last post by:
I'm still trying to solve a problem I have experienced in Safari. This is my third post on the subject. I'm hoping someone can shed some light. The problem is that, in Safari, a table with no...
18
by: Dalibor Kezele | last post by:
Is there a way to have something like <table onclick="f(this)"> <tr><td>... <tr><td>... .... </table> f() {
11
by: Norman L. DeForest | last post by:
Am I misunderstanding the CSS specifications or is Firefox (version 1.0.6) (and Opera) doing the wrong thing? It appears that Firefox 1.0.6 includes the border in width calculations for tables...
1
by: khoegen | last post by:
I've created a Table and wish to create links between the records wtihin the table. Background: The table consists of a list of Documents. These documents contain references to other...
2
by: clickon | last post by:
I am confused about the way in which asp:table objects work. When a control is within an asp table it generally appears to be in the scope of the tables parent control. E.g. if i have a page that...
1
by: daniellee2006 | last post by:
I am creating a basic website to store people profiles and within this website i have a page that creates a table dependent on the number of records in mysql written in PHP within these tables...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
2
by: shankar2 | last post by:
Hello All, This is my first try at posting a query in this forum, your patience is appreciated. App Details: Access 2000 on Win 2000, Slightly better than a Novice, comfortable with various...
3
by: nzaiser | last post by:
Hello! This is my first time asking a question here, so please bear with me if I seem 'unconventional!' My issue is this..... OS: Windows_NT App: MS Access 2000 (also in 2003) ODBC linked...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.