Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
tech
Starting Member
32 Posts |
Posted - 2011-03-25 : 14:40:11
|
I have the following tables:UsersID int (PK)CompanyID int {FK, nullable)DistributorID int (FK, nullable)ScoresID int (PK)UserID int {FK)Score intCompanyCompanyID int (PK)....DistributorsDistributorID int (PK)DistributorName nvarchar(50)DistributorCompaniesLinkID int (PK)CompanyID int (FK)DistributorID int (FK)A User is associated with a company OR distributorA User has scores.A Distributor has one or more companies associated to it.I want to be able to calculate the sum of all scores for users for all companies which belong to a distributor. makes sense? not sure how to think of the query! I also want to do this across all distributors.so if Distributor1 has 3 companies and there are a mixture of number of users per company, I want to calculate the total scores for all companies for the distributor. |
|
srujanavinnakota
Starting Member
34 Posts |
Posted - 2011-03-25 : 16:45:48
|
try this..Select Distributors.DistributorName,Company.CompanyName,Sum(Score) as Scorefrom DistributorCompaniesLinkINNER JOIN Distributors ON DistributorCompaniesLink.DistributorID=Distributors.DistributorIDINNER JOIN Company ON DistributorCompaniesLink.CompanyID=Company.CompanyID/**If User is assosiated with company use company table or if not use distibuter table**/INNER JOIN Users ON Company.UsersID=Users.UsersIdINNER JOIN Scores ON Scores.UsersID=Users.UsersIdGROUP BY Distributors.DistributorName,Company.CompanyNameyou could do this in sub query format also. |
 |
|
tech
Starting Member
32 Posts |
Posted - 2011-03-29 : 15:58:01
|
Thanks. Problem I have is that when I implement paging on the SPROC, I get an error when executing saying that there was more than 1 subquery results that were returned....different query but you get the hint-- ================================================-- Template generated from Template Explorer using:-- Create Procedure (New Menu).SQL---- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below.---- This block of comments will not be included in-- the definition of the procedure.-- ================================================SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE dbo.[GetLeaderboardCompanyScores] ( @itemsPerPage int, @pageNumber int, @totalRecords int OUTPUT)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @firstRow int DECLARE @lastRow int SELECT @firstRow = (@pageNumber - 1) * @itemsPerPage + 1, @lastRow = (@pageNumber - 1) * @itemsPerPage + @itemsPerPage, @totalRecords = (SELECT COUNT(c.[CompanyID]) FROM Company c LEFT OUTER JOIN Users u ON u.CompanyID = c.CompanyID LEFT OUTER JOIN UserAttributes ua ON ua.UserID = u.UserID GROUP BY c.CompanyID); WITH ManyItems AS ( SELECT c.CompanyID, c.CompanyName, ISNULL(SUM(ua.TotalPoints), 0) AS CompanyPoints, ROW_NUMBER() OVER (ORDER BY ua.TotalPoints DESC) AS RowNumber FROM Company c LEFT OUTER JOIN Users u ON u.CompanyID = c.CompanyID LEFT OUTER JOIN UserAttributes ua ON ua.UserID = u.UserID GROUP BY c.CompanyID, c.CompanyName, ua.TotalPoints ) SELECT RowNumber, CompanyID, CompanyName, CompanyPoints FROM ManyItems WHERE RowNumber BETWEEN @firstRow AND @lastRowENDGO |
 |
|
|
|
|
|
|