Solved

Help- Selecting sums summary from multiple Tables (SQL)

Posted on 2012-04-12
3
393 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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