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
 General SQL Server Forums
 New to SQL Server Programming
 SQL query "multiple wheres"

Author  Topic 

vafo
Starting Member

5 Posts

Posted - 2009-07-06 : 12:41:42
Hello sqlteam community!

I wanted to make a somewhat complex query that would return the number of pending tasks and the total number of tasks on a database. Each task is a row, and completed tasks have a column with a bit "1" for pending, "0" for complete. Tasks have an associated member responsible for them - the output I am looking for is:
__________________________________________
member id | completed tasks | total tasks

An example using two queries and showing the db structure:

X002548
Not Just a Number

15586 Posts

Posted - 2009-07-06 : 12:44:44
I get a broken link...
Can you post the ddl, sample data and expected results as shown in the link in my sig?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

vafo
Starting Member

5 Posts

Posted - 2009-07-06 : 13:13:32
link verified tho, sorry for the unusual posting.
DDL:
CREATE TABLE [dbo].[SinfRegister] (
[idNumber] [int] IDENTITY (1, 1) NOT NULL ,
[hora] [datetime] NULL ,
[requerente] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[requisitante] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[responsavel] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[titulo] [nvarchar] (100) COLLATE Latin1_General_CI_AS NULL ,
[descricao] [nvarchar] (500) COLLATE Latin1_General_CI_AS NULL ,
[categoria] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
[prazo] [datetime] NULL ,
[pendente] [bit] NULL ,
[data_conclusao] [datetime] NULL ,
[estado] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]


Example DML's

SELECT responsavel, COUNT(responsavel) AS ntarefas
FROM dbo.SinfRegister
WHERE (pendente = 1)
GROUP BY responsavel
(to count pending tasks per user)
SELECT     responsavel, COUNT(responsavel) AS ntarefas
FROM dbo.SinfRegister
GROUP BY responsavel
(to count all tasks per user)

Sample data (unDML'ed, sorry)

id |responsavel|pendente
1 jcanosa 0
18 voliveira 0
19 jmatias 0
20 voliveira 0
21 voliveira 0
22 jbarros 0
23 voliveira 0
25 jbarros 0
26 jmatias 1
27 jcanosa 0
28 voliveira 0
30 jcanosa 0
31 nadia 1
32 jmatias 0
33 jbarros 1


Expected output (as posted above)

responsavel|pending|total
__________________________
jmatias | 1 | 3
jbarros | 1 | 3
jcanosa | 0 | 3
nadia | 1 | 1
....

Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-07-06 : 13:24:12
EDIT: Formatted

SELECT   responsavel, 
Sum(CASE
WHEN pendente = 1
THEN 1
ELSE 0
END) AS [Pending],
Count(responsavel) AS [Total]
FROM Sinfregister
GROUP BY responsavel
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-07-06 : 13:25:27
How about


SELECT t.responsavel
, dt1.pending_tasks
, dt2.all_tasks
, dt2.all_tasks - dt1.pending_tasks AS completed_tasks
FROM
(SELECT DISTINCT responsavel t
FROM dbo.SinfRegister) AS t
LEFT JOIN (SELECT responsavel, COUNT(responsavel) AS pending_tasks
FROM dbo.SinfRegister
WHERE pendente = 1
GROUP BY responsavel) AS dt1
ON t.responsavel = dt1.responsavel
LEFT JOIN (SELECT responsavel, COUNT(responsavel) AS all_tasks
FROM dbo.SinfRegister
GROUP BY responsavel) AS dt2
ON t.responsavel = dt2.responsavel



EDIT: I like Vijay's better

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

vafo
Starting Member

5 Posts

Posted - 2009-07-07 : 09:49:50
Vijays answer does the trick. Thanks for your answer aswell - more material for me to study!
Go to Top of Page
   

- Advertisement -