I have table like this

* Acc, amount, x, Name, Time
* 1, 100, dep, bbb, 10
* 1, 200, dep, bbb, 10
* 1, 80, wit, bbb, 10
* 1, 90, wit, bbb, 10
* 2, 100, dep, bbb, 20
* 2, 101, wit, bbb, 20
* 1, 100, dep, ccc, 10
* 1, 150, wit, ccc, 10

From this I want to show a table like

* Name, Acc, Time, dep\_amt, wid\_amt, profit
* bbb, 1, 10, 300, 170, 130
* bbb, 2, 20, 100, 101, -1
* ccc, 1, 10, 100, 150, -50

So, I tried self join

```
SELECT a.Name, a.Acc, a.Time, a.Amount as dep_amt, new.Amount as wid_amt, (a.Amount-new.Amount) as profit
FROM all_data as a, all_data as new
where a.Time = new.Time
and a.name = new.name
and a.acc =new.acc
and a.x= "dep”
and new.x="wit"

```

But , I can't use Group by(error contains nonaggregated column) with it and this sql results a table with many rows. How to do???