Solved

Help- Selecting sums summary from multiple Tables (SQL)

Posted on 2012-04-12
3
401 Views
Last Modified: 2012-04-12
I need assistance in wirting this sql statement from 4 tables, and not sure how to write it. I have the below example...Any help will be greatly appreciated...thx


Table1                          Table2                             Table3                    Table4
JobDate                        JobDate                            JobDate                  JobDate
JobNumber                  JobNumber                      JobNumber            JobNumber
EquipSubtotal              LaborSubTotal                   SuppliesTotal         MiscExpenseSubTotal
                                    HotelSubtotal

Input Variables:    @JobNumber, @StartDate, @EndDate

Here is 4 Tables. The end result I am looking for:
Selected JobNumber:
Day 1
EquipmentTotal        LaborTotal      HotelTotal      SuppliesTotal   MiscExpenseTotal
Day2
Equipment Total     LaborTotal      HotelTotal      SuppliesTotal   MiscExpenseTotal
etc...

Table1 contains multiple entries for each day for each piece of equipment(Subtotal), but I only want the Sum(EquipSubtotal) as equipmentTotal returned for that day.

Table2 contains multiple rows for each day for Hotel and Labor, but only need the Sum(HotelSubtotal) and Sum(LaborSubTotal ) returned for each day

Table3 contains only 1 row of the sum of the supplies total for each day, there is no multiple entries for each day, just once per day and the total.

Table4 contains multiple rows for each day for mis expenses. I need to only return the Sum(MiscExpenseSubTotal) for that day.

There may not be a anything for that day in 1 of the tables, but I need the other tables to be returned in the query. All Tables have a default of $0.00 so there is no Nulls.
0
Comment
Question by:BKennedy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 500 total points
ID: 37837619
SELECT t.jobdate, sum(t.EquipSubTotal) sub, sum(LaborSubTotal) lab FROM
(
select jobdate, EquipSubTotal, 0 as LaborSubTotal, 0 as HotelSubtotal, 0 as SuppliesTotal , 0 as MiscExpenseTotal FROM table1
UNION ALL
select jobdate, 0 as EquipSubTotal, LaborSubTotal, HotelSubtotal, 0 as SuppliesTotal , 0 as MiscExpenseTotal FROM table2
) AS t
GROUP BY T.jobdate

something like the above, but with all the tables in, all the fields and the right rows selected
0
 

Author Comment

by:BKennedy2008
ID: 37837771
I am getting how you are creating this table, as I am writing this out, but should the first line be  Select t.jobdate, sum(EquipSubTotal) sub, .........insteasd of sum(t.EquipSubTotal).....?
or should it be sum(t.laborSubTotal) in the first line?
0
 

Author Closing Comment

by:BKennedy2008
ID: 37837903
That is awesome, exactly what I need,  thanks for the quick repsonse....!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

728 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