Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table is

HTML
projecttaskid    status      projectid resid
-----------------------------------------------
1                on hold        9       43
2                in progress    9       44
3                in progress    9       43
4                draft          9       43
5                on hold        10      43
----------------------------------------------

Now I want

taskcount (having status!=draft) of resid 43 as [assignedTaskcount] and total taskcount (having status!=draft) as[Totaltaskcount]
where projectid=9 in a single query

I want the output as

HTML
assignedTaskcount     Totaltaskcount
----------------------------------------
    2                    3
----------------------------------------


Please help by giving the query.I can't use resid as direct in query,I've to fetch it by joining with other table.
But for your easy understanding,just help me on this table.
Posted
Updated 5-Jun-15 1:41am
v2

The simplest solution is to combine a SUM with a CASE statement:
SQL
SELECT
    SUM(CASE WHEN resid = 43 THEN 1 ELSE 0 END) As assignedTaskcount,
    COUNT(1) As Totaltaskcount
FROM
    YourTable
WHERE
    projectid = 9
And
    status != 'draft'
;
 
Share this answer
 
Comments
souvikcode 5-Jun-15 13:22pm    
many thanks.
I was stupid to think use status != 'draft' in count(case).
I should have think I can count on resid and use status in where clause as status!=draft required for both assigned and total.
Thanks and can you please name sites in which I can take challenge of sql query with solution?
a quick solution could be

SQL
SELECT (SELECT  COUNT(projecttaskid) FROM yourTable WHERE status != 'draft' AND projectid=9 and  resid = 43) assignedTaskcount,
        (SELECT  COUNT(projecttaskid) FROM yourTable WHERE status != 'draft' AND projectid=9)  Totaltaskcount



for best performance you can create temp table and insert all your data in that table and then make these queries on that table

SQL
DECLARE @temp TABLE
(
	projecttaskid   BIGINT,
	status      VARCHAR(100),
	projectid BIGINT,
	resid BIGINT
)

INSERT INTO @temp ( projecttaskid ,status ,projectid ,resid)
SELECT * FROM yourTable WHERE projectid = 9 and status != 'draft'

SELECT (SELECT  COUNT(projecttaskid) FROM @temp WHERE  resid = 43) assignedTaskcount,
        (SELECT  COUNT(projecttaskid) FROM @temp)  Totaltaskcount
 
Share this answer
 
v5
Comments
souvikcode 6-Jun-15 14:01pm    
Thanks for support

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