Solved

SQL Query / Join & Sum

Posted on 2014-03-05
4
314 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
[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
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 41

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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