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

x
?
Solved

SQL Query / Join & Sum

Posted on 2014-03-05
4
Medium Priority
?
319 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 750 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 750 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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