Question: I have the following mumbers in column B

```
4025015659
4025015660
4025015661
4025015662
-266490.78
-266491.78
-266492.78
-266493.78
0
0
0

```

I need to the delete the all numbers except negative numbers. Is it possible to have a formula or vba code to do so.

Answer: 
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,'')**
```
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

```