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


Consolidate rows based on criteria


Consolidate rows based on criteria

Author
Message
mic.con87
mic.con87
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 242
Hi,

here is some sample data


CREATE TABLE #TESTING (acc_no varchar(20), number INT, c_name varchar(20), R_Value decimal(10,2), time_spent decimal(10,2) )

INSERT INTO #TESTING VALUES ('C1232' ,4445, 'Tom', 345.7, 43.56)
INSERT INTO #TESTING VALUES ('C1232' ,3456, 'Tom', 3454.7, 553.556)
INSERT INTO #TESTING VALUES ('C1232',6789, 'Thomas', 1345.7, 463.556)
INSERT INTO #TESTING VALUES ('C125632',1234, 'Will', 423.64, 233.77)
INSERT INTO #TESTING VALUES ('C125632',2345, 'William', 56.76, 77.89)
INSERT INTO #TESTING VALUES ('C125632',1345, 'Will', 444.56, 234.54)
INSERT INTO #TESTING VALUES ('C125632',12634, 'Will', 34.27, 112.56)


select * from #TESTING



basically what I would like is to sum the last two columns based on acc_no and return the most featured 'c_name' per acc_no.

The result fro the above would be :


CREATE TABLE #Result1 (acc_no varchar(20), number INT, c_name varchar(20), R_Value decimal(10,2), time_spent decimal(10,2) )

INSERT INTO #Result1 VALUES ('C1232' ,4445, 'Tom', 5146.1, 1060.672)
INSERT INTO #Result1 VALUES ('C125632' ,1234, 'Will', 959.23, 658.76)



select * from #Result1




Thanks a million
Steve Jones
Steve Jones
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: Administrators
Points: 40504 Visits: 18846
Probably a more efficient way, but this is the idea:

; WITH MyCTE (acc_no, c_name, cnt)
AS
( SELECT acc_no
, c_name
, COUNT(c_name)
FROM #testing a
GROUP BY acc_no
, c_name
)
SELECT
t.acc_no
, c.c_name
, number_sum = SUM( t.number)
, r_value_sum = SUM( t.R_Value)
FROM #TESTING t
INNER JOIN mycte c
ON t.acc_no = c.acc_no
WHERE c.cnt = (SELECT MAX(d.cnt)
FROM MyCTE d
WHERE d.acc_no = c.acc_no
)
GROUP BY t.acc_no
, c.c_name



You need to count and get the max count of the names, by account and then join with the sum.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6311 Visits: 7192
Does the following help:
;WITH SumPerAcct AS
(SELECT acc_no, SUM(R_Value) AS Sum_R_Value, SUM(time_spent) AS Sum_time_spent
FROM #TESTING
GROUP BY acc_no),
OccurrenceCount AS
(SELECT acc_no, MAX(number) AS maxNumber, c_name, Count(c_name) NumerOfOccurence, ROW_NUMBER() OVER (PARTITION BY acc_no ORDER BY COUNT(c_name) DESC) Sequences
FROM #TESTING
GROUP BY acc_no, c_name),
MaxOccurrence AS
(SELECT acc_no, c_name
FROM OccurrenceCount
WHERE Sequences = 1)
SELECT SumPerAcct.acc_no, OccurrenceCount.maxNumber, MaxOccurrence.c_name, SumPerAcct.Sum_R_Value, SumPerAcct.Sum_time_spent
FROM SumPerAcct --ON SumPerAcct.acc_no = #TESTING.acc_no
JOIN OccurrenceCount ON OccurrenceCount.acc_no = SumPerAcct.acc_no
JOIN MaxOccurrence ON MaxOccurrence.acc_no = SumPerAcct.acc_no
AND MaxOccurrence.c_name = OccurrenceCount.c_name



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2268 Visits: 23066
with cte1 as (
select acc_no,number,c_name,
sum(R_Value) over(partition by acc_no) as R_Value,
sum(time_spent) over(partition by acc_no) as time_spent,
count(*) over(partition by acc_no,c_name) as cn
from #TESTING),
cte2 as (
select acc_no,number,c_name,R_Value,time_spent,
row_number() over(partition by acc_no order by cn desc,number desc) as rn
from cte1)
select acc_no,number,c_name,R_Value,time_spent
from cte2
where rn=1



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




mic.con87
mic.con87
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 242
Thanks a million for the help everyone! Really appreciate it Smile
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