Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Stored Procedure is, for the most part, returning the data I want. However, in a few isolated cases, it returns two records where there should just be one. Specifically, in the "bad" cases, there are two records with the same Description and MemberName. There should only be one row (max) for each Description and MemberName combination. Rather than list them seperately, the values of the two records should be combined into one.

IOW, the data should be like this:

PLATYPUSDESCRIPTION	CRITTERNAME	WEEK1VAL	WEEK2VAL
-----------------	-------------	--------	--------
Platypus		Bill		7		42
Duckbill		Pat		40		76


...but it contains some duplicated PLATYPUSDESCRIPTION + CRITTERNAME values like so:

PLATYPUSDESCRIPTION	CRITTERNAME	WEEK1VAL	WEEK2VAL
-----------------	-------------	--------	--------
Platypus		Bill		7		42
Duckbill		Pat		40		76
Duckbill		Pat		40		99


In the case above, row 2 should be:

Duckbill		Pat		80		175


...and there should be no row 3.

Here is the last part of the SP, where the assembled data is returned:

SQL
SELECT PLATYPUSDESCRIPTION, CRITTERNAME, TC.WEEK1USAGE, TC.WEEK2USAGE, TC.USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE, TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
ORDER BY PLATYPUSDESCRIPTION, CRITTERNAME;


In order to avoid PLATYPUSDESCRIPTION + CRITTERNAME duplicates, I tried concatenating PLATYPUSDESCRIPTION and CRITTERNAME in a GROUP BY:

SQL
SELECT PLATYPUSDESCRIPTION, CRITTERNAME, TC.WEEK1USAGE, TC.WEEK2USAGE, TC.USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE, TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
GROUP BY PLATYPUSDESCRIPTION + CRITTERNAME
ORDER BY PLATYPUSDESCRIPTION, CRITTERNAME;


...and listing PLATYPUSDESCRIPTION and CRITTERNAME singly in a GROUP BY:

SQL
SELECT PLATYPUSDESCRIPTION, CRITTERNAME, TC.WEEK1USAGE, TC.WEEK2USAGE, TC.USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE, TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
GROUP BY PLATYPUSDESCRIPTION, CRITTERNAME
ORDER BY PLATYPUSDESCRIPTION, CRITTERNAME;


...and tried to use DISTINCT on PLATYPUSDESCRIPTION and CRITTERNAME this way:

SQL
SELECT DISTINCT(PLATYPUSDESCRIPTION), DISTINCT(CRITTERNAME), TC.WEEK1USAGE, TC.WEEK2USAGE, TC.USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE, TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
GROUP BY PLATYPUSDESCRIPTION, CRITTERNAME
ORDER BY PLATYPUSDESCRIPTION, CRITTERNAME;


...and this way:

SQL
SELECT DISTINCT(PLATYPUSDESCRIPTION + CRITTERNAME), TC.WEEK1USAGE, TC.WEEK2USAGE, TC.USAGEVARIANCE,
TC.WEEK1PRICE, TC.WEEK2PRICE, TC.PRICEVARIANCE, TC.PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED TC
GROUP BY PLATYPUSDESCRIPTION, CRITTERNAME
ORDER BY PLATYPUSDESCRIPTION, CRITTERNAME;


...but none of them would even compile.

How can I consolidate rows that describe the same PLATYPUSDESCRIPTION + CRITTERNAME?
Posted
Updated 25-Jan-16 8:02am
v2

1 solution

From your input data and expected result given in the question, you can use SUM and GROUP BY to get the output you are looking for.

I created a table based on the columns and inserted three rows you provided in the question into the table.
C++
PLATYPUSDESCRIPTION    CRITTERNAME    WEEK1VAL    WEEK2VAL
-------------------    -----------    --------    --------
Platypus		       Bill	          7		      42
Duckbill		       Pat		      40		  76
Duckbill		       Pat		      40		  99

This query will produce your expected result:
SQL
select PLATYPUSDESCRIPTION, CRITTERNAME, sum(WEEK1VAL) as WEEK1VAL, sum(WEEK2VAL) as WEEK2VAL from 
dbo.TestTable group by PLATYPUSDESCRIPTION,CRITTERNAME

The output of this query is:

C#
PLATYPUSDESCRIPTION    CRITTERNAME    WEEK1VAL    WEEK2VAL
-------------------    -----------    --------    --------
Platypus	           Bill	          7	          42
Duckbill	           Pat	          80	      175


Hope this helps to point you in the right direction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900