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 |
|
alorenzini
Starting Member
3 Posts |
Posted - 2007-11-26 : 17:25:49
|
| I have the following code:DECLARE @ConsultantID AS nVarChar(50),@StartDt AS DateTime,@EndDt AS DateTimeSET @ConsultantID = '0000112'SET @StartDt = '2007-05-01'SET @Enddt = '2007-05-31'DECLARE @Quarter AS DateTimeDeclare @Year AS DateTimeSET @StartDt = Convert(DateTime,Convert(nVarChar(50),@StartDt,101) + ' 00:00:00.000')SET @EndDt = Convert(DateTime,Convert(nVarChar(50),@EndDt,101) + ' 23:59:59.997')SET @Quarter = DATEADD(qq, DATEDIFF(qq,0,@StartDt), 0)SET @Year = DATEADD(yy, DATEDIFF(yy,0,@StartDt), 0);--[consultantreports].[uspS_ConsultantDownline] '0000003'With downline (ConsultantID,ConsultantName,SponsorID,SponsorName,DownLineLevel,ConsultantXId,SponsorXID, AdjustedPartOneTotal)AS(SELECT Convert(NVARCHAR(MAX),A.ConsultantID),A.FirstName + ' ' + A.LastName as ConsultantName,CAST(A.SponsorID AS Nvarchar(MAX)),(SELECT [FirstName] + ' ' + [LastName] FROM Consultant WHERE ConsultantID = a.SponsorID),0 as DownLineLevel ,A.ConsultantXID,A.SponsorXID,O.AdjustedPartOneTotalFROM dbo.consultant AINNER JOIN uvw_DownlineOrder O ON A.ConsultantID = O.ConsultantIDWHERE A.ConsultantID = @ConsultantID UNION ALLSELECT CAST (A.ConsultantID AS NVARCHAR(MAX))AS ConsultantID,A.FirstName + ' ' + A.LastName as ConsultantName,cast(A.SponsorID as nvarchar(MAX)) AS SponsorID,(SELECT [FirstName] + ' ' + [LastName] FROM Consultant WHERE ConsultantID = a.SponsorID) As SponsorName,DownLineLevel + 1 ,A.ConsultantXID,A.SponsorXID,B.AdjustedPartOneTotalFROM dbo.consultant AS AINNER JOIN DownLine AS B ONA.SponsorID = B.ConsultantID )SELECT A.DownLineLevel-- ,C.BumpUpDate,CAST(CONVERT(NVARCHAR(MAX),A.ConsultantID)AS INT) as ConsultantID,UPPER(RTRIM(A.ConsultantName)) AS ConsultantName,C.EmailAddress,D.Title AS AchievedTitle, CONVERT(NVARCHAR(MAX),A.SponsorID) AS SponsorID,A.SponsorName ,A.ConsultantXID,A.SponsorXID,AdjustedPartOneTotalFROM DownLine AS ALEFT OUTER JOIN dbo.consultant AS C ONA.ConsultantID = C.ConsultantIDLEFT OUTER JOIN SharedDimension.dbo.DimConsultantTitle AS D ONC.CurrentLevelXID = D.XIDWHERE DownLineLevel <= 3GROUP BY A.ConsultantID,A.ConsultantName,A.SponsorID,A.SponsorName,DownLineLevel,C.BumpUpDate,C.EmailAddress,D.Title,A.ConsultantXID,A.SponsorXID,A.AdjustedPartOneTotalGOMy issue is I need to sum the AdjustedPartOneTotal column per consultant. But this way I get individual amount per consultant. I tried adding the following code but it repeats the first amount over and over although it is rolling up the consultants appropriately:SELECT A.DownLineLevel-- ,C.BumpUpDate,CAST(CONVERT(NVARCHAR(MAX),A.ConsultantID)AS INT) as ConsultantID,UPPER(RTRIM(A.ConsultantName)) AS ConsultantName,C.EmailAddress,D.Title AS AchievedTitle, CONVERT(NVARCHAR(MAX),A.SponsorID) AS SponsorID,A.SponsorName ,A.ConsultantXID,A.SponsorXID,SUM(AdjustedPartOneTotal) AS TotalFROM DownLine AS ALEFT OUTER JOIN dbo.consultant AS C ONA.ConsultantID = C.ConsultantIDLEFT OUTER JOIN SharedDimension.dbo.DimConsultantTitle AS D ONC.CurrentLevelXID = D.XIDWHERE DownLineLevel <= 3GROUP BY A.ConsultantID,A.ConsultantName,A.SponsorID,A.SponsorName,DownLineLevel,C.BumpUpDate,C.EmailAddress,D.Title,A.ConsultantXID,A.SponsorXIDcan some one shed some light on this? My resultset looks like this:I don't know if this will help but this is what I am getting back:DownlineLevel, ConsultantID, Sum(Total)0 3 104.801 23 104.801 30 104.802 40 104.801 65 104.801 73 104.802 89 104.801 102 104.801 112 104.802 127 104.803 131 104.801 142 104.802 244 104.80The issue is the Sum value should be differnt amounts and actually a majority should be 0.hope this sheds a little bit of light. |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-11-26 : 19:20:30
|
| If I'm understanding you properly, the error looks like it lies inB.AdjustedPartOneTotalThe way you wrote the code it simply will keep repeating whatever o.AdjustedPartOneTotal was, because that's all you are telling it to do.Is this supposed to be a running total? Please explain what you want that field to actually do, and it should be a easy fix. |
 |
|
|
alorenzini
Starting Member
3 Posts |
Posted - 2007-11-27 : 08:48:47
|
| There are 38 AdjustedPartOneTotal amounts that come from the uvw_DownlistOrder view and there are like 64 Consultants. The 38 AdjustedPartOneTotal amounts should appear with the appropriate consultant based on the consultant id which are in both the consultant table and the view. The remaining consultants should have 0s for the AdjustedPartOneTotal amount.Did that help? |
 |
|
|
anonymous1
Posting Yak Master
185 Posts |
Posted - 2007-11-27 : 11:09:36
|
| the code is a little overwhelming but assuming a basic understanding you need a query similar to this...select dbo.consultant.consultantname, ISNULL(SUM(downline.AdjustedPartOneTotal), 0.0) AS Totalfrom dbo.consultantleft join downline on downline.consultantid = dbo.consultant.consultantidthere is a chance changing your "LEFT JOIN" to "RIGHT JOIN" will also get you closer |
 |
|
|
alorenzini
Starting Member
3 Posts |
Posted - 2007-11-27 : 11:26:55
|
| This gives me the same issue. It's repeating the SUM value.ThanksArtDatabase AnalystTastefully Simple, Inc.Alexandria, MN |
 |
|
|
anonymous1
Posting Yak Master
185 Posts |
Posted - 2007-11-27 : 15:56:06
|
| can you please post some sample data and expected resultset? |
 |
|
|
|
|
|
|
|