Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2004, 02:23 PM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default multiple self joins and sum

I am running into a few problems getting data that looks to be correct, it appears the sum function is adding more records then I want.

table structure
ID
Code
RunID
Amt
HoursOrDays
Type * Tells if the record is hours or days
---------

I am trying to return the following:
sum(Code) WHERE RunID=6,
sum(Code) WHERE RunID=6 + sum(Code) WHERE RunID=6 AND RunID=3,
(Sum(HoursOrDays) WHERE RunID=6 + Sum(HoursOrDays) WHERE RunID=6) AND WHERE Type=1,
(Sum(HoursOrDays) WHERE RunID=6 + Sum(HoursOrDays) WHERE RunID=6) AND WHERE Type=2

Any help would be greatly appreciated.



 
Old May 21st, 2004, 11:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:sum(Code) WHERE RunID=6 + sum(Code) WHERE RunID=6 AND RunID=3,
Are u adding RunId=6's sum(Code) twice with that of RunId=3?

Quote:
quote:(Sum(HoursOrDays) WHERE RunID=6 + Sum(HoursOrDays) WHERE RunID=6) AND WHERE Type=1,
(Sum(HoursOrDays) WHERE RunID=6 + Sum(HoursOrDays) WHERE RunID=6) AND WHERE Type=2
Do you want twice the sum(Code) here in the above statements??

I'm not sure if this is what you are looking for.

SELECT
(
    (SELECT Sum(Code) FROM <TABLENAME> WHERE RunId=6) as SUM_RUNID_6,
    (SELECT Sum(Code) FROM <TABLENAME> WHERE RunId in(6,3)) as SUM_RUNID_6AND3,
    (SELECT Sum(HoursOrDays) FROM <TABLENAME> WHERE RunId=6 and Type=1) as SUM_RUNID_6_Type_1,
    (SELECT Sum(HoursOrDays) FROM <TABLENAME> WHERE RunId=6 and Type=2) as SUM_RUNID_6_Type_2
)

Hope that Helps.
Cheers!

-Vijay G
 
Old May 24th, 2004, 08:15 AM
Registered User
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, that didnt quite work but I am not sure if I explained what I was looking for correctly.

Let's say I have the following rows,

ID | Code | RunID | Amt | LiqAmt | HoursOrDays | Type
1 | A1 | 3 | 50 | 50 | 20 | 1
1 | A1 | 3 | 50 | 50 | 4 | 2
1 | A2 | 3 | 50 | 0 | 20 | 1
1 | A2 | 3 | 50 | 0 | 5 | 2
1 | A1 | 6 | 150 | 0 | 0 | 1
1 | A1 | 6 | 250 | 0 | 0 | 1
1 | A2 | 6 | 50 | 0 | 0 | 1

What I am trying to return here is:
1) Sum (HoursOrDays) for RunID=3 AND Type 1
2) Sum (HoursOrDays) for RunID=3 AND Type 2

3) Sum (Amt) for RunID=3
4) Sum (Amt) For RunID=3 + Sum(Amt) from RunId =6
5) Sum (LiqAmt) For RunID=3 + Sum(LiqAmt) from RunId =6

I think the problem that I am running into is I need these grouped by Code, when doing this and using sum I get odd Totals.

An example of what I am looking for to be returned in

Code | Hours | Days | RunTotal | Amt (Run3+Run6 Total) | LiqAmt (Run3+Run6 Total)
A1 | 20 | 4 | 100 | 500 | 100
A2 | 20 | 5 | 100 | 150 | 0

The query I cam up with that kind of works is:
SELECT
    <Table>.Code,
    SUM(<Table>.Amt) AS RunTotal,
    (SELECT SUM(<Table>.HoursOrDays) FROM <Table> WHERE <Table>.PayTypeCode=1) AS RunHours,
    SUM(Pay1.PayAmt) AS AmtRunTotals,
    SUM(Pay1.LiqAmt) AS LiqRunTotals
FROM
    <Table>
    Inner Join <Table> as Pay1 ON <Table>.AcctCode= Pay1.AcctCode AND Pay1.PayRun=3
WHERE
    PayEmpCkBudget.PayRun = 6
GROUP BY <Table>.AcctCode ORDER BY <Table>.AcctCode;

The problem with this is my RunHours Field is the sum of all HoursOrDays and the same value is used for each row.



 
Old May 25th, 2004, 06:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Looks like there are more than one TABLE being involved, and The fields too are different from what the structure you have mentioned. So I cannot get you the exact query that works.

For getting the RunHours right, I would suggest you to use CASE... WHEN... THEN to get the expected result. You can checl BOL for the syntax and usage of CASE construct.

Hope that helps.
Cheers!

_________________________
-Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple sql joins in mySQL 5 Dean Lovell SQL Language 4 January 19th, 2007 02:43 PM
Joins for multiple tables Jinn SQL Server 2000 2 November 5th, 2006 11:39 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Multiple Inner Joins msmagied Classic ASP Databases 0 October 6th, 2004 05:11 PM
Multiple Joins in Multiple Table Search query pookster Access 4 September 23rd, 2004 03:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.