Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Group by and Count
Message
 
 
À
13/09/2021 18:11:20
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01682285
Message ID:
01682287
Vues:
51
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform