Solved

SQL Query / Join & Sum

Posted on 2014-03-05
4
312 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query assistance 16 48
delete the first occurence of a duplicate row in sql 5 41
Check ALL SP in database make sure there are no errors 17 63
SQL Query Syntax error after > 11 41
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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