Question: I have below 2 tables and I have to, write a query to display the sum of the total amount received through the credit card and cheque. Give the alias names as 'total\_credit' and 'total\_cheque' respectively.

table\_payment  

id (PK)  

user\_id  

amount  

transaction\_type\_id (FK)

table\_transaction\_type  

id (PK)  

type 

Expected Output:

```
total_credit| total_cheque     
Value a     | Value B

```

I tried with below query but I getting values in different rows

```
select  sum(case when tt.type = 'Credit Card' then pt.amount end) as
"total_credit",  sum (case when tt.type = 'Cheque' then pt.amount end)
as "total_cheque" from payment pt join transaction_type tt on
pt.transaction_type_id = tt.id group by tt.type

```

Actual\_Output:

```
total_credit| total_cheque     
Value A     |     
            | Value B

```

not sure how to get Value B in first row along with Value A, please advice.

Answer: 
```
select user_id, sum(case when tt.type='Credit Card' the amount end) as total_credit, sum(case when tt.type='Cheque' the amount end) as total_credit 
from table_payment p
inner join table_transaction_type tt
on p.transaction_type_id = tt.id
group by user_id;

```
don't group by `tt.type` - that is why you get that result. If you need to group by `user_id` (as in one of the solutions) then do; if you need a result with exactly one row and two columns, aggregated (summed) over all users, don't group by ANYTHING, you don't need a GROUP BY clause. (I believe the SQL Standard doesn't allow that; Oracle definitely does, but if that bothers you, you can add something trivial, like GROUP BY NULL.)