Solved

SQL Query / Join & Sum

Posted on 2014-03-05
4
306 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
ID: 39907336
post sample query...
0
 

Author Comment

by:bezellvi
ID: 39907516
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
ID: 39907646
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
ID: 39927463
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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