Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need help to calculate remaining values

Author  Topic 

wkm1925
Posting Yak Master

207 Posts

Posted - 2012-04-30 : 09:56:21
I've tables and data as following,

declare @tStudent table (applicantIdx int, gender char(1), category char(1))
insert into @tStudent values(100, 'm', 'A')
insert into @tStudent values(102, 'm', 'B')
insert into @tStudent values(101, 'f', 'C')
insert into @tStudent values(104, 'm', 'B')
insert into @tStudent values(103, 'f', 'B')
insert into @tStudent values(109, 'm', 'D')
/*
applicantIdx is a primary key
gender only contain values m or f
category only contain values A, B, C, or D
*/

declare @tQuota table (idx int, iptsIdx int, kursusIdx int, mQuota_A int, fQuota_A int,
mQuota_B int, fQuota_B int, mQuota_C int, fQuota_C int, mQuota_D int, fQuota_D int)
insert into @tQuota values(1999, 120, 300, 30, 44, 29, 77, 55, 39, 34, 89)
insert into @tQuota values(1923, 200, 300, 90, 23, 34, 45, 55, 90, 20, 50)
insert into @tQuota values(4488, 87, 38, 390, 229, 298, 23, 57, 58, 12, 8)

/*
idx is a primary key
iptsIdx, and kursusIdx is a unique

mQuota_A is a male quota for A category
fQuota_A is a female quota for A category
mQuota_B is a male quota for B category
fQuota_B is a female quota for B category
and so on ...
*/

declare @tAccepted table (applicantIdx int, tQuotaIdx int)
insert into @tAccepted values(104, 1923);
insert into @tAccepted values(102, 1999);
insert into @tAccepted values(101, 4488);
/*
@tAccepted save the quota was taken
@tAccepted(applicantIdx) is a foreign key to @tStudent(applicantIdx)
applicantIdx is a unique
*/


I want to built SQL to display the number of remaining quota

My expected result for the number of remaining quota as following,
the number of remaining quota

idx    |  mQuota_A   | fQuota_A  | mQuota_B  | fQuota_B | mQuota_C  | fQuota_C  | mQuota_D | fQuota_D
-------------------------------------------------------------------------------------------------------
1999 30 44 28 77 55 39 34 89
1923 90 23 33 45 55 90 20 50
4488 390 229 298 23 57 57 12 8


Need help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-30 : 10:07:02
can you explain how you got those values in red?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2012-04-30 : 10:11:53
insert into @tAccepted values(104, 1923) means, applicantIdx=104 from @tStudent has accepted the offer for @tQuota(idx). As a result, applicantIdx=104, gender=male, and category=B

So, in @tQuota(idx)=1923, we have mQuota_B - 1. Then the result was 34-1 = 33


and so on sir
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-30 : 10:22:08
ok. here you go

SELECT t.idx,
t.mQuota_A - COALESCE(q.mQuota_A,0) AS mQuota_A,
t.fQuota_A - COALESCE(q.fQuota_A,0) AS fQuota_A,
t.mQuota_B - COALESCE(q.mQuota_B,0) AS mQuota_B,
...
FROM @tQuota t
OUTER APPLY (SELECT SUM(CASE WHEN s.gender = 'm' AND category ='A' THEN 1 ELSE 0 END) AS mQuota_A,
SUM(CASE WHEN s.gender = 'f' AND category ='A' THEN 1 ELSE 0 END) AS fQuota_A,
SUM(CASE WHEN s.gender = 'm' AND category ='B' THEN 1 ELSE 0 END) AS mQuota_B,
.....
FROM @tAccepted a
INNER JOIN @tStudent s
ON s.applicantIdx = a.applicantIdx
WHERE a.tQuotaIdx = t.idx
)q


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

wkm1925
Posting Yak Master

207 Posts

Posted - 2012-04-30 : 10:35:40
tq Mr Visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-30 : 10:37:17
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -