Solved

Help- Selecting sums summary from multiple Tables (SQL)

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

21 Experts available now in Live!

Get 1:1 Help Now