Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi ,
I need to count Pending Proposal's from Proposal table and show their record count.This is the table structure.


ProposalNO    ApplicationNo       Status  Locked

        ABC           1              A      Y
        ABC           2              A      Y
        ABC           3              A      Y
        ABC           4              R      N
        ABC           5
        DEF           6              A      Y
        DEF           7              R      N
        DEF           8              A      Y
        IJK           9              A      Y
        IJK          10


Now Proposal ABC has 5 applications out of which only 4 have entry in locked column so ABC is pending where as DEF is Completed and IJK is also pending. Now i need to get data for pending applications only. Locked column here is the status of application not the proposal. If the locked status of any application inside a proposal is null then that proposal is pending.

ProposalNo    NoOfApplications
  ABC                 5
  IJK                 2


i can always get no of applications using group by but i am not able to create a suitable query for finding weather the proposal is pending or not. Any sorf of help will be much appreciated .
Posted
Updated 30-Aug-14 2:23am
v2
Comments
Mehdi Gholam 29-Aug-14 9:21am    
Start by defining how "status" and "locked" should be combined together (AND OR NOT) for the count you want.
Aakash Sharma 30-Aug-14 8:15am    
Status column is of little importance here. Proposal is pending if even 1 entry in the lock column again that proposal no in any tuple is null. Here locked column is the status of application not the proposal itself.

I think you're looking for:
SQL
SELECT ProposalNo, 
    COUNT(ProposalNo) AS ProposalCount
WHERE Status = 'R'
GROUP BY ProposalNo
HAVING ProposalCount > 0
 
Share this answer
 
v2
Comments
Aakash Sharma 30-Aug-14 8:22am    
Actually the Locked column here is the status of the application under the proposal not the status of the proposal.I m sorry that i was not clear the first time.
Yvan Rodrigues 30-Aug-14 13:15pm    
Changed WHERE to Status = 'R'.
Does that work?
Lets consider you have table
SQL
ProposalDetails(ProposalNO, ApplicationNo, Status, Locked)

having above data. Try following query it will make your picture clear.
SQL
 SELECT	ProposalNO,
	SUM(1) [Applications],
	SUM(CASE WHEN Locked = 'Y' THEN 1 ELSE 0 END) [Locked],
	SUM(CASE WHEN Locked = 'N' THEN 1 ELSE 0 END) [UnLocked],
	SUM(CASE WHEN Locked <> 'Y' AND Locked <> 'N' THEN 1 ELSE 0 END) [UnKnown]
FROM	ProposalDetails
GROUP BY ProposalNO

Now you can add following condition to your query if you think its valid.
SQL
HAVING SUM(CASE WHEN Locked = 'N' THEN 1 ELSE 0 END) > 0
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900