Solved

Help- Selecting sums summary from multiple Tables (SQL)

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 64
Linked Server - SP with Param to VIew 7 25
SQL- GROUP BY 4 26
SQL Server code help needed 14 35
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

730 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