Solved

syntax for sql - easy ?

Posted on 2014-10-07
4
121 Views
Last Modified: 2014-10-07
I have two tables with payments and deductions. How do I substract the payments from the deductions being in two separate tables ?

I need to take this total:
SELECT SUM (PmtRecd) as totalpayments
FROM dbo.BillPaymentsRecvdTA

and substract this total:
SELECT SUM (amount) AS totaltransactions
FROM    dbo.TATransactions

And call it "TotalAvailable"
0
Comment
Question by:amucinobluedot
4 Comments
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 40367191
Hi!

You do it like this

select X.totalpayments - y.totaltransactions as TotalAvailable
from (
SELECT SUM (PmtRecd) as totalpayments
FROM dbo.BillPaymentsRecvdTA ) as X,
(
SELECT SUM (amount) AS totaltransactions
FROM    dbo.TATransactions) as Y

Open in new window


Regards,
     Tomas Helgi
0
 
LVL 58

Expert Comment

by:Gary
ID: 40367194
SELECT SUM (PmtRecd) as totalpayments,SUM (amount) AS totaltransactions,sum(PmtRecd)-sum(amount) as TotalAvailable
FROM dbo.BillPaymentsRecvdTA, dbo.TATransactions

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 40367202
The second option did not do the math correctly for some reason
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40367279
The second option produces a Cartesian product (or "cross join")  between the 2 tables. e.g. if there was 10 rows in the first table and 10 rows in the second table there would be 100 rows in total so any SUM() of the expanded set of rows will be wrong.

The reason this is not a problem in the first solution (by Thomas) is that each subquery produces just one row and 1*1 remains 1, so the unstated cross join of that query does not impact the arithmetic.

I really don't understand why points were awarded to an incorrect answer (sorry Gary)
---------------------------

To AVOID making accidental Cartesian products is SQL queries:
TIP:
          Do NOT use commas in the FROM clause

Using ANSI 92 syntax instead of commas to list out tables stops accidental Cartesian products because you actually have to ask for it by using CROSS JOIN
SELECT
      X.totalpayments - y.totaltransactions AS TotalAvailable
FROM (
           SELECT
                 SUM(PmtRecd) AS totalpayments
           FROM dbo.BillPaymentsRecvdTA
     ) AS X
CROSS JOIN (              --<< here a cross join is OK because 1 * 1 = 1
           SELECT
                 SUM(amount) AS totaltransactions
           FROM dbo.TATransactions
     ) AS Y
;

Open in new window


and the second:
SELECT
      SUM(PmtRecd)               AS totalpayments
    , SUM(amount)                AS totaltransactions
    , SUM(PmtRecd) - SUM(amount) AS TotalAvailable
FROM dbo.BillPaymentsRecvdTA
CROSS JOIN dbo.TATransactions --<< why CROSS JOIN here? this would be wrong!!!
;

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Read about why website design really matters in today's demanding market.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now