0

I had the following table structure which is listed below

Budhol    COCODE    BEN      OBJ    SPARE2  SPARE1  TASKNO  Value   Field   Code
---------------------------------------------------------------------------------
362103    36        362101  991003  NULL    MA1001  NULL    4516    613030  001
362104    36        362104  991003  NULL    MA1001  NULL    9088    613030  002
362103    36        362101  991003  NULL    MA1001  NULL    3387    613030  003
362103    36        362101  991003  NULL    MA1001  NULL    4026    613030  004

This is the required output

Budhol  COCODE  BEN      OBJ    SPARE2  SPARE1  TASKNO  Value   Field   Code
---------------------------------------------------------------------------------
362103  36      362101  991003  NULL    MA1001  NULL    11929   613030  001,003,001
362104  36      362104  991003  NULL    MA1001  NULL    9088    613030  002

i.e By doing group by i need sum of all values and comma separated Codes

I had tried below query but output is not matching im getting all the codes for all the columns

SELECT 
    B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2,    
    SUM(B.value) AS Value ,
    Code = STUFF((SELECT ', ' + Code 
                  FROM #temp2 b 
                  WHERE b.BEN = B.BEN 
                    AND b.Budhol = B.Budhol 
                    AND b.COCODE = B.COCODE 
                    AND b.FIELD = B.FIELD  
                    AND b.SPARE1 = B.SPARE1 
                    AND b.SPARE2 = B.SPARE2 
                  FOR XML PATH('')), 1, 1, '')
FROM 
    #temp2 B
GROUP BY 
    B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73

3 Answers3

5

The problem appears to have to do with the NULL values in some of your columns. In particular, the SPARE1 and SPARE2 columns can take on NULL values, and your intention is that such NULL values should equate. In the query below, I wrapped them on both sides of the join with COALESCE, and I am getting the expected output now.

SELECT B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2,    
    SUM(B.value) AS Value,
    Code = STUFF((SELECT ', ' + Code FROM temp2 t
                  WHERE t.BEN = B.BEN AND t.Budhol = B.Budhol AND t.COCODE = B.COCODE AND
                        t.FIELD = B.FIELD AND
                        COALESCE(t.SPARE1, '') = COALESCE(B.SPARE1, '') AND
                        COALESCE(t.SPARE2, '') = COALESCE(B.SPARE2, '')
                 FOR XML PATH('')), 1,1, '')
FROM temp2 B
GROUP BY B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2;

enter image description here

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

You are using the same alias (B) for the inner and the outer query. You have to use different alias (B1 & B2) to differentiate them :

SELECT B1.BEN, B1.Budhol, B1.COCODE, B1.FIELD, B1.OBJ, B1.SPARE1, B1.SPARE2,    
       SUM(B1.value) as Value ,
       STUFF((SELECT ', ' + Code 
              FROM #temp2 b2 
              WHERE b2.BEN = B1.BEN and b2.Budhol = B1.Budhol and b2.COCODE = B1.COCODE and b2.FIELD = B1.FIELD and 
                    isnull(b2.SPARE1,'') = isnull(B1.SPARE1,'') and isnull(b2.SPARE2,'') = isnull(B1.SPARE2,'')
              FOR XML PATH('')), 1,1, '') as Code
FROM #temp2 B1
GROUP BY B1.BEN,B.Budhol, B1.COCODE, B1.FIELD, B1.OBJ, B1.SPARE1, B1.SPARE2;

Update: Additionally, as Tim Biegeleisen has seen, b2.spare2 = b1.spare2 will return false when both are null (null is an empty state, not a value), so you should convert those null states to some values that can be compared, using the coalesce or isnull functions : isnull(b2.spare2,'') = isnull(b1.spare2,'')

Marc Guillot
  • 6,090
  • 1
  • 15
  • 42
0
CREATE TABLE #Table1
    ([Budhol] int, [COCODE] int, [BEN] int, [OBJ] int, [SPARE2] varchar(4), [SPARE1] varchar(6), [TASKNO] varchar(4), [Value] int, [Field] int, [Code] varchar(100))
;

INSERT INTO #Table1
    ([Budhol], [COCODE], [BEN], [OBJ], [SPARE2], [SPARE1], [TASKNO], [Value], [Field], [Code])
VALUES
    (362103, 36, 362101, 991003, NULL, 'MA1001', NULL, 4516, 613030, 001),
    (362104, 36, 362104, 991003, NULL, 'MA1001', NULL, 9088, 613030, 002),
    (362103, 36, 362101, 991003, NULL, 'MA1001', NULL, 3387, 613030, 003),
    (362103, 36, 362101, 991003, NULL, 'MA1001', NULL, 4026, 613030, 004)

SELECT DISTINCT BUDHOL
    ,COCODE
    ,BEN
    ,OBJ
    ,SPARE2
    ,SPARE1
    ,TASKNO
    ,SUM(VALUE)VALUE
    ,FIELD
    ,STUFF((
            SELECT ','+'00' + CONVERT( VARCHAR(10),U.CODE)
            FROM #TABLE1 U
            WHERE B.BEN = U.BEN
                AND B.BUDHOL = U.BUDHOL
                AND B.COCODE = U.COCODE
                AND B.FIELD = U.FIELD
                AND B.SPARE1 = U.SPARE1


            FOR XML PATH('')
            ), 1, 1, '') AS USERLIST
FROM #TABLE1 B
GROUP BY [BUDHOL]
    ,[COCODE]
    ,[BEN]
    ,[OBJ]
    ,[SPARE2]
    ,[SPARE1]
    ,[TASKNO]
    ,[FIELD]

output

BUDHOL  COCODE  BEN OBJ SPARE2  SPARE1  TASKNO  VALUE   FIELD   USERLIST
362103  36  362101  991003  NULL    MA1001  NULL    11929   613030  001,003,004
362104  36  362104  991003  NULL    MA1001  NULL    9088    613030  002
Chanukya
  • 5,833
  • 1
  • 22
  • 36