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.
| Author |
Topic |
|
mironline
Starting Member
4 Posts |
Posted - 2007-05-29 : 07:25:14
|
| Dear FriendsI have 3 form with these names : Clients <----- Projects <------- FilesI am using this query in my [ web ] page : select clients.id , (select count(*) from projects where projects.clients_id = clients.id) as TotalProjects from clientsI want to compute SUM for all relevant files for each client's projects.how i can add it to select select statement ??thank you.m.o |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-29 : 07:29:46
|
| please post some sample data and required output |
 |
|
|
mironline
Starting Member
4 Posts |
Posted - 2007-05-29 : 07:43:24
|
| Dear Friend.Thank you for your quick response.it's appreciatedmy table's data is listed here :clients Tableid -------- name1 -------- Client12 -------- Client2Projects Tableid ------client -- name1 -------- 1 -------- p12 -------- 1 -------- piu3 -------- 1 -------- p094 -------- 2 -------- p_Files Tableid ---- Projects -------- file1 -------- 1 -------- MyFiles12 -------- 2 -------- MyFiles23 -------- 2 -------- MyFiles34 -------- 2 -------- MyFiles45 -------- 4 -------- MyFiles5result :Clients.ID -------- ---total projects -------- -------- Total Files1 -------- -------- -------- 3 -------- -------- -------- 4( how to find this ?)2 -------- -------- -------- 1 -------- -------- -------- 1thank you |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-29 : 07:52:16
|
| [code]Select c.ID, count(p.name) as [Total Projects], count(f.File) as [Total Files]from Clients c join projects p on c.id = p.Client Join Files f on p.id = f.Projectsgroup by c.IDorder by c.ID[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-05-29 : 08:20:59
|
| declare @clients Table (id int, name varchar(50))insert @clientsselect 1,'Client1' unionselect 2,'Client2'declare @Projects Table(pid int, cid int, pname varchar(30))insert @projectsselect 1,1,'p1' unionselect 2,1,'piu' unionselect 3,1,'p09' unionselect 4,2,'p_'declare @Files Table(fid int, pid int, filen varchar(30))insert @filesselect 1,1,'MyFiles1' unionselect 2,2,'MyFiles2' unionselect 3,2,'MyFiles3' unionselect 4,2,'MyFiles4' unionselect 5 ,4,'MyFiles5'Select Client = min(name), count(pid) as [Total Projects], sum(files) as [Files Count] From(Select c.id, c.name,p.pid, 'Files' = (Select Count(*) from @files where pid = p.pid) from @clients c join @projects p on c.id = p.cid ) as bgroup by idorder by id |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-30 : 00:19:53
|
[code]select c.id, count(distinct p.pid), count(distinct f.fid)from clients c inner join projects p on c.id = p.cid left join files f on p.pid = f.pidgroup by c.id[/code] KH |
 |
|
|
|
|
|
|
|