Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group by and Count
Message
 
 
To
13/09/2021 18:11:20
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01682285
Message ID:
01682287
Views:
50
Be careful with counts and multiple tables joins, you may end up with wrong counts. I usually try to separate things into subqueries in such situations.

https://blogs.lessthandot.com/index.php/datamgmt/datadesign/aggregates-with-multiple-tables/

>Apparently, adding a count with distinct seems to work :
>
>
>SELECT MAX(Program.Name) AS Program,MAX(Something.Name) AS Something,COUNT(*) AS 'Count of projects',COUNT(DISTINCT Provider.ID) AS Providers
> FROM MainTable
> INNER JOIN Program ON MainTable.NoProgram=Program.ID
> INNER JOIN Something ON MainTable.NoSomething=Something.ID
> INNER JOIN Project ON MainTable.ID=Projet.NoMainTable
> INNER JOIN Provider ON Projet.NoProvider=Provider.ID
> GROUP BY Program.Name,Something.Name
>
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform