Solved

SQL Query / Join & Sum

Posted on 2014-03-05
4
294 Views
Last Modified: 2014-03-19
I need some help getting a SQL query to do exactly what I want.

I have a list of locations, one records for each location. There is a field that contains the size of each location, listed one time.

When I Left Join a table that has information about events at the location, it duplicates the list of locations for every event record. This works fine for the text data, but when I try to then group the data by a shared category, it Sums the size - I want it to sum the size ONCE per location, not for every event. Any ideas?
0
Comment
Question by:bezellvi
  • 2
4 Comments
 
LVL 8

Expert Comment

by:5teveo
Comment Utility
post sample query...
0
 

Author Comment

by:bezellvi
Comment Utility
SELECT Room.ID, Room.OfficeCode, Room.[Room#], Room.SpaceType, Room.Capacity, Occupant.EventID, Occupant.[Date/Time], Occupant.Count
FROM Room LEFT JOIN Occupant ON Room.ID = Occupant.RoomID;
 
I then want to group by Space Type, but when I do that, it sums the Room.Capacity
0
 
LVL 8

Assisted Solution

by:5teveo
5teveo earned 250 total points
Comment Utility
Try
adding group by statement.

The 'Group By' will show grouped column values and tally count as you expect.

It appears you can SUM(Occupant.Count) but Do not SUM(Room.Capacity)...  


SELECT Room.ID, Room.OfficeCode, Room.[Room#], Room.SpaceType, Room.Capacity, Occupant.EventID, Occupant.[Date/Time], SUM(Occupant.Count)
FROM Room LEFT JOIN Occupant ON Room.ID = Occupant.RoomID
Group by
Room.ID, Room.OfficeCode, Room.[Room#], Room.SpaceType, Room.Capacity, Occupant.EventID, Occupant.[Date/Time];
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
try this.
SELECT R.ID,R.OfficeCode,R.[Room#],R.SpaceType,R.Capacity,O.EventID,O.[Date/Time],O.[Count] 
  FROM (SELECT Room.ID,Room.OfficeCode,Room.[Room#],Room.SpaceType,SUM(Room.Capacity) Capacity 
          FROM Room 
         GROUP BY Room.SpaceType) R 
       LEFT JOIN Occupant O 
              ON R.ID = O.RoomID;

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

11 Experts available now in Live!

Get 1:1 Help Now