Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help- Selecting sums summary from multiple Tables (SQL)

Posted on 2012-04-12
3
Medium Priority
?
417 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

597 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