Moiz
Moiz

Reputation: 2439

Sql Query to Filter the Result Set

I have this Cash Table created in SQL Server In which i need to Know that what is the Type of Currency Note, and what is the No of Currecny and total Number.

1 = Amount Came
2 = Amount Gone

I am expecting the result by grouping the SQL Query. means if i have 1000 currency and Total Number of Currency Note is 11 [Considered as Came/Given is "1"] which makes it 11000 as total

Same way i have a currency note of 20 [which is 1 In and 1 Given] so it should not be in the desired output as has No matching or i dont have any Notes of 20. as i have Got one Note of 20 and given back 20

Expected Output is

Currency No of Currency Total
1000 -- 11 - 11000
500  -- 1 -- 500
100 -- 1 - 100
50 -- 1 - 50

enter image description here

Upvotes: 0

Views: 129

Answers (1)

AnandPhadke
AnandPhadke

Reputation: 13506

create table currency(curr int,noc int,total int,cg int)

insert into currency 
Values(1000,10,10000,1),(1000,1,1000,1),(500,1,500,1),(100,1,100,1),(50,1,50,1),(20,1,20,1),(10,1,10,1),
(5,1,5,1),(2,1,2,1),(1,1,1,1),(20,1,20,2),(10,1,10,2),(5,1,5,2),(2,1,2,2),(1,1,1,2)


select a.curr,count(*),SUM(Total)
FROM
(
select x.curr curr,x.[no of currency],(x.curr*x.[no of currency]) as Total
From
(select a.curr,a.noc-isnull(b.noc,0) as [no of currency] from 

(select * from currency where cg=1) a

left join

(select * from currency where cg=2) b
on a.curr=b.curr
) x
) a
where a.[no of currency] <>0
group by curr

Upvotes: 1

Related Questions