473,795 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difficult summing query

Hello,

Here is a brief summary:

Table 1 = All Accounts
- with fields such as Customer ID and Account #

Table 2 = Deposit Balance Table
- with fields such as Account #, Balance

Table 3 = Loan Balance Table
- with fields such as Account #, Balance

All accounts are either deposit accounts or loan accounts. What I need
to do is to gather information about total balances in both deposits
and loans for each customer. I haven't been able to hit the right query
for doing this. I can easily get information about one or the other,
such as the following:

SELECT All_Accounts.Cu stomer_ID, COUNT (DISTINCT
(Deposit_Balanc e_Table.Account _Number)), Sum
(Deposit_Balanc e_Table.Balance )
FROM Product_Table, Deposit_Balance
WHERE (Product_Table. Account_Number= Deposit_Balance .Account_Number )
GROUP BY Product_Table.C ustomer_ID ORDER BY 1

Which will give me one row for each user, and show me the total number
of deposit accounts each customer has and a sum of the balances in each
of those accounts. I can make a similar query involving Loan Accounts.
As soon as I try to draw both, however, I wind up below my depth.
Something to do with the handedness of my joins, I believe. Often I
will get one column of information (either deposits or loans), or the
query will fail because the join I'm attempting is invalid, etc. I need
to take every row in the All_Accounts table, match each one to its
balance in either the Deposit or Loan table, and then group them all by
the Customer ID and sum them, so that I can find out the total
relationship balance per customer. Any help would be appreciated.

Nov 23 '05 #1
5 1804
(jm********@gma il.com) writes:
Here is a brief summary:

Table 1 = All Accounts
- with fields such as Customer ID and Account #

Table 2 = Deposit Balance Table
- with fields such as Account #, Balance

Table 3 = Loan Balance Table
- with fields such as Account #, Balance

All accounts are either deposit accounts or loan accounts. What I need
to do is to gather information about total balances in both deposits
and loans for each customer. I haven't been able to hit the right query
for doing this. I can easily get information about one or the other,
such as the following:

SELECT All_Accounts.Cu stomer_ID, COUNT (DISTINCT
(Deposit_Balanc e_Table.Account _Number)), Sum
(Deposit_Balanc e_Table.Balance )
FROM Product_Table, Deposit_Balance
WHERE (Product_Table. Account_Number= Deposit_Balance .Account_Number )
GROUP BY Product_Table.C ustomer_ID ORDER BY 1

Which will give me one row for each user, and show me the total number
of deposit accounts each customer has and a sum of the balances in each
of those accounts. I can make a similar query involving Loan Accounts.
As soon as I try to draw both, however, I wind up below my depth.


SELECT a.CustomerID, SUM(b.DepositBa lance), SUM(b.LoanBalan ce)
FROM All_Accounts a
JOIN (SELECT Account#, DepositBalance, LoanBalance = 0
FROM DepositBalanceT able
UNION ALL
SELECT Account#, 0, LoanBalance
FROM LoanBalanceTabl e) AS b ON a.Account# = b.Account#
GROUP BY a.CustomerID

The thing in parentheses is a derived table. Conceptually a temp
table within the query, but it is never materialized. Often a very
powerful tool to tackle complicated problems.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Nov 23 '05 #2
Ah, this sort of thing is exactly what I need. However, when I modified
the query you gave to fit the particular table names, etc, I now have
an error: "Missing Keyword" at this point:

FROM LoanBalanceTabl e) AS b ON a.Account# = b.Account#
**

Do you know what would cause this? Thanks,

Nov 28 '05 #3
(jm********@gma il.com) writes:
Ah, this sort of thing is exactly what I need. However, when I modified
the query you gave to fit the particular table names, etc, I now have
an error: "Missing Keyword" at this point:

FROM LoanBalanceTabl e) AS b ON a.Account# = b.Account#
**

Do you know what would cause this? Thanks,


When I run the query:

SELECT a.CustomerID, SUM(b.DepositBa lance), SUM(b.LoanBalan ce)
FROM All_Accounts a
JOIN (SELECT Account#, DepositBalance, LoanBalance = 0
FROM DepositBalanceT able
UNION ALL
SELECT Account#, 0, LoanBalance
FROM LoanBalanceTabl e) AS b ON a.Account# = b.Account#
GROUP BY a.CustomerID

All I get is complaints on the tables missing.

But I don't recognize the error message "Missing keyword". Are you
really using Microsoft SQL Server?
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Nov 28 '05 #4
Nope, using Oracle9i. Does that change things? From what I could see
online, the "Missing keyword" error is a poorly defined syntax error of
some kind - something is/was wrong with my query and that is how it
told me so. However, I had modified the query in subtle ways to try to
make it work, so I'll backtrack a bit. Here's the query verbatim:

SELECT
AL1.INVOLVED_PA RTY_ID_PRIMARY| |','||SUM(B.Dep ositBalance)||' ,'||SUM(B.LoanB alance)||','
FROM INFORMENT.PRODU CT_OFFER_PURCHA SE AL1
JOIN (SELECT AL2.ACCOUNT_NUM BER, AL2.BAL_LEDGER_ CURRENT AS
DepositBalance, LoanBalance = 0
FROM INFORMENT.DEPOS IT_BALANCE AL2
UNION ALL
SELECT AL3.ACCOUNT_NUM BER, 0, AL3.BAL_PRIN AS LoanBalance
FROM INFORMENT.LOAN_ BALANCE AL3) AS B ON AL1.ACCOUNT_NUM BER =
B.ACCOUNT_NUMBE R
GROUP BY AL1.INVOLVED_PA RTY_ID_PRIMARY
ORDER BY 1;

This query is the closest I can get to mirroring your example code. It
produces a different error:

JOIN (SELECT AL2.ACCOUNT_NUM BER, AL2.BAL_LEDGER_ CURRENT AS
DepositBalance, LoanBalance = 0
*
ERROR at line 3:
ORA-00923: FROM keyword not found where expected

The only real changes I made to the example you gave were to format the
output as a csv, and to try to change some of the names around. It
seems to be getting angry at the = 0 part, not sure why. One thing I
made different was that I pulled the balances AS the names LoanBalance
and DepositBalance, since by default they'd be less clear. I assume
this should have no impact on the query. Not quite sure what's
incorrect. Any more thoughts?

Thanks very much for your help thus far.

Nov 29 '05 #5
(jm********@gma il.com) writes:
Nope, using Oracle9i. Does that change things?
Yes. You should post in comp.databases. oracle, to get more accurate
answers. In this newsgroup you will get syntax that works in Microsoft
SQL Server.

JOIN (SELECT AL2.ACCOUNT_NUM BER, AL2.BAL_LEDGER_ CURRENT AS
DepositBalance, LoanBalance = 0
*
ERROR at line 3:
ORA-00923: FROM keyword not found where expected


That syntax is indeed proprietary to MS SQL Server (and Sybase). I believe
that the ANSI way of writing this is "0 AS LoanBalance" or just "0
LoanBalance". Both syntaxes works on SQL Server, and may work in Oracle.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Nov 29 '05 #6

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

Similar topics

9
2577
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302 java: 325 gcc: 348 Python with Psyco: 1317 Pure Python using sum: 2312
1
5883
by: wireless | last post by:
We recently added a new database at the company. It has only one purpose - to hold massive amounts a daily data generated by telephone calls on a network. The amount of data was so large (several gigabytes a day) that the guy who set up the database creates a new table for it each day. His thinking was that if we only need to query one day's worth of data then it would be a lot faster to query a table with one day's data than having...
13
1654
by: Don Sealer | last post by:
I have about 40 different defects I'm tracking. I'd like to have all of these defects totaled individually, both by month and by year. I'd like to show these results in a report. I know I could create 40 queries (one for each defect) with just the two fields, count and defect, and sum the count field but that sounds like a lot of work. Is there a way I can do this much simpler and quicker? My database has two fields, one is called...
0
1922
by: flamingo | last post by:
I have a query that works just fine, but because I need to use it in Business Objects and I have to use a prompt for the date, I need the initial query for the count as a correlated subquery in the from clause, not in the select statement. I've figured out what I'd like to have, however, I only get one row back (and it is correct!) I need to get the results that the initial query gives me, which is about 10 rows. Is there any way I can do...
0
2143
by: flamingo | last post by:
I have a query that works just fine, but because I need to use it in Business Objects and I have to use a prompt for the date, I need the initial query for the count as a correlated subquery in the from clause, not in the select statement. I've figured out what I'd like to have, however, I only get one row back (and it is correct!) I need to get the results that the initial query gives me, which is about 10 rows. Is there any way I can do...
0
1384
by: King | last post by:
Hi I have 2 queries where I calculated time elapsed. In the first I calculated time spent by a employee in group meetings and in the second query, I calculated time spent by an employee in personal meetings with the clients. I wish to join the two queries to sum up the total time. I have the following fields Fields for Group hours query
4
2536
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
7
15006
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the code I have (I am using Python): def generate (rows, cols): # This just prints the coordinates and the number that goes in it, It also prints the matrix in a square import random m = {} for r in range(rows): for c in...
3
3562
by: NewlytoSQL | last post by:
Hi all, im fairly new to SQL and im stuck half way through a query, im using DB2 here is what im tryng to do. i have a query that brings back an item number , shelf req, sum of all orders columns, based on the shelf req column the item number column has more than one row for the same item number therefore my sum of all orders is summing them based on shelf req which i need, what i cant get is how to keep this info plus get the sum of all...
0
9672
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
9519
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
10214
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
10164
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
10001
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
7538
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
6780
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();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.