Solved

SQL Query --- counting by groups

Posted on 2014-11-10
5
126 Views
Last Modified: 2014-11-13
I have a  table with data that looks something like this.

Project ID   DocumenID

1             1
1             2
1             3
2            4      
3             5
3            6


             
I need to create a sql statement that will give me a count of ALL the documents as well as ammount of documents
per project.  Any clue how?
0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40433593
Something like:

SELECT ProjectID, COUNT(*)
FROM Projects
GROUP BY ProjectID WITH ROLLUP
0
 

Author Comment

by:vbnetcoder
ID: 40433675
This is the exact query:

SELECT  P.[Project Name],   Count(DP.Document_ID) AS DocumentCount
  FROM [MyTableReports].[dbo].[Document_Project] DP
   INNER JOIN  [MyTableReports].[dbo].[Document_Files] DF
    ON DF.Document_ID = dp.Document_ID
      INNER JOIN [MyTableReports].[dbo].Project P
         ON P.ID = dp.Project_ID  
         Group By P.[Project Name]  

And it returns this:


Project Name      DocumentCount
AA Project      1
BB project      2
CC roject      2
ttttttt              1


I need it to return:

Project Name      DocumentCount  Total Documents
AA Project               1                                5
BB project               2                                5
CC roject               2                                5
ttttttt                       1                                5

How?
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40434474
I don't know why you need to have the TotalDocuments in all rows since it will be always the same value but here's one solution that I personally don't like:
SELECT  P.[Project Name],   Count(DP.Document_ID) AS DocumentCount , 
           (SELECT COUNT(1) FROM [MyTableReports].[dbo].[Document_Project]) AS TotalDocuments
   FROM [MyTableReports].[dbo].[Document_Project] DP
    INNER JOIN  [MyTableReports].[dbo].[Document_Files] DF
     ON DF.Document_ID = dp.Document_ID
       INNER JOIN [MyTableReports].[dbo].Project P
          ON P.ID = dp.Project_ID  
          Group By P.[Project Name]  

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 40438076
>>I need it to return:
Project Name      DocumentCount  Total Documents
AA Project               1                                5
BB project               2                                5
CC roject               2                                5
ttttttt                       1                                5 <<
Can you post the data that should return those results with some explanation as to why?
0
 

Author Closing Comment

by:vbnetcoder
ID: 40439571
ty
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
the way to learn Microsoft BI 13 70
SQL query 7 49
TSQL Assignining CTE column in to a variable 3 26
SQL Server set parent recort in select with row number 8 27
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question