Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SUM values only for distinct ID's


SUM values only for distinct ID's

Author
Message
Bahmudales
Bahmudales
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 41
Hello,

I have the following report:
ID Time
311793 3:12
312184 3:13
312184 3:13
312373 3:04


Is there a way to sum 'Time' values only for distinct ID's?
Tee Time
Tee Time
Mr or Mrs. 500
Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)

Group: General Forum Members
Points: 563 Visits: 465
What are you expecting the results of this to look like?
Bahmudales
Bahmudales
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 41
Without distinct the result is:
311793 3:12
312184 3:13
312184 3:13
312373 3:04
Total: 12:42

.. but I want only values for distinct ID's to be added:
311793 3:12
312184 3:13
312373 3:04
Total 9:29
Mark Fitzgerald-331224
Mark Fitzgerald-331224
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3692 Visits: 1513
----------
Without distinct the result is:
311793 3:12
312184 3:13
312184 3:13
312373 3:04
Total: 12:42

.. but I want only values for distinct ID's to be added:
311793 3:12
312184 3:13
312373 3:04
Total 9:29

----------

Normally you would run some kind of distinct function on the dataset before passing to SSRS, as this would return the report you require.

If you needed all values to be shown but not to be summed then use something like below. Both will return the original rows (4), but allow the second and subsequent values to be converted to 0.


declare @SourceData   table (OrderNumber int, Value money, Code char(1))

insert into @SourceData
   SELECT 311793 AS OrderNumber, 3.12 AS Value, 'A' AS Code
   UNION
   SELECT 312184 AS OrderNumber, 3.13 AS Value, 'A' AS Code
   UNION
   SELECT 312184 AS OrderNumber, 3.13 AS Value, 'D' AS Code
   UNION
   SELECT 312373 AS OrderNumber, 3.04 AS Value, 'A' AS Code

select * from @SourceData;

--- Option 1 : SQL 2005+
with ComparedValues as (
   select * , row_number() over (partition by OrderNumber order by Code) as RN
      from @SourceData
)
select OrderNumber, Value, Code, case when RN=1 then Value else 0 end as ConvertedValue
   from ComparedValues;

--- Option 1 : SQL 2012+
select OrderNumber, Value, Code,
   case when OrderNumber = lag(OrderNumber,1,NULL) over
         (partition by OrderNumber order by Code)
      then 0 else Value end as ConvertedValue
   from @SourceData;




Fitz
Tee Time
Tee Time
Mr or Mrs. 500
Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)Mr or Mrs. 500 (563 reputation)

Group: General Forum Members
Points: 563 Visits: 465
Agree with Fitz, SELECT DISTINCT should get you what you are looking for.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search