FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
SQL ServerReporting & grouped calculations

Calculate each group’s integer percentage of the total

SQL Server: Multiply before division and protect against a zero denominator.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Calculate each group’s numeric spread

SQL Server: Subtract MIN from MAX to report the difference between extremes.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Compute an integer weighted average

SQL Server: Multiply before summing and guard a zero total weight with NULLIF.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Count distinct values within a result

SQL Server: COUNT DISTINCT counts distinct non-null values.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Count rows satisfying separate conditions

SQL Server: SUM over CASE expressions calculates several conditional counts in one scan.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Distinguish row count from non-null value count

SQL Server: COUNT(*) includes every row while COUNT(column) skips NULL values.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Filter rows before grouping

SQL Server: WHERE limits the rows contributing to the aggregate; it differs from filtering completed groups with HAVING.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Group by more than one field

SQL Server: Each distinct combination of grouping fields produces its own row.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Return zero for a sum over no rows

SQL Server: SUM produces NULL for an empty input; COALESCE supplies the desired reporting default.

SQL Server✓ Tested
SQL ServerReporting & grouped calculations

Turn known group values into columns

SQL Server: Conditional aggregation gives a portable pivot when the category set is known.

SQL Server✓ Tested