Round #673
10 May 2026, 08:41· csharp· qwen2.5-coder-7b-instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 3 / 0 / 2
- Journey weight
- 250
- Lightshift ΔL
- —
Prompt
Find products in the top 10 percent by revenue using NTILE.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0022 | 2026-05-10T08:41:39Z
Find products in the top 10 percent by revenue using NTILE.
ZERO POINT — 3 entries (structural invariants)
● hsl(7.18, 100%, 50%) class [feature]
● hsl(0.00, 100%, 50%) public [feature]
● hsl(84.10, 100%, 50%) namespace [feature]
UNOBSERVED — 0 entries (implied truths)
OBSERVED — 2 entries (explicit in request)
◉ hsl(85.13, 100%, 50%) using [feature]
◉ hsl(234.87, 100%, 50%) using [feature]
COMPLEXITY │ 5 entries │ spread: 150° │ band: LOW
Baseline response (no ZPI)
To find products in the top 10 percent by revenue using the `NTILE` function in SQL, you can follow these steps:
1. Calculate the total revenue for each product.
2. Use the `NTILE` function to divide the products into deciles (10 equal groups).
3. Select the products that fall into the top decile.
Here's an example of how you might write this query in SQL:
```sql
WITH ProductRevenue AS (
SELECT
ProductID,
SUM(SalesAmount) AS TotalRevenue
FROM
Sales
GROUP BY
ProductID
),
RankedProducts AS (
SELECT
ProductID,
TotalRevenue,
NTILE(10) OVER (ORDER BY TotalRevenue DESC) AS RevenueRank
FROM
ProductRevenue
)
SELECT
ProductID,
TotalRevenue
FROM
RankedProducts
WHERE
RevenueRank = 1;
```
### Explanation:
1. **ProductRevenue CTE**: This common table expression (CTE) calculates the total revenue for each product by summing up the `SalesAmount` from the `Sales` table, grouped by `ProductID`.
2. **RankedProducts CTE**: This CTE uses the `NTILE(10)` function to divide the products into 10 deciles based on their total revenue in descending order.
3. **Final SELECT Statement**: The final query selects the product IDs and their total revenues from the `RankedProducts` CTE where the `RevenueRank` is 1, which corresponds to the top 10 percent of products by revenue.
### Notes:
- Ensure that your database supports window functions like `NTILE`.
- Adjust table and column names according to your actual schema.
- If you have multiple sales records for a single product, this query will sum up the sales amounts to get the total revenue per product.
Enhanced response (with ZPI)
To find products in the top 10 percent by revenue using NTILE, we can utilize SQL Server's `NTILE` function. This function divides a result set into a specified number of groups (tiles) and assigns each row to one of these tiles.
Here is an example implementation in C# using Entity Framework Core:
```csharp
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Revenue { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionStringHere");
}
}
public class ProductService
{
private readonly ApplicationDbContext _context;
public ProductService(ApplicationDbContext context)
{
_context = context;
}
public List<Product> GetTop10PercentByRevenue()
{
var totalProducts = _context.Products.Count();
var top10PercentCount = (int)Math.Ceiling(totalProducts * 0.1);
return _context.Products
.OrderByDescending(p => p.Revenue)
.Take(top10PercentCount)
.ToList();
}
}
```
### Explanation:
1. **Product Class**: Represents a product with an `Id`, `Name`, and `Revenue`.
2. **ApplicationDbContext Class**: Inherits from `DbContext` and includes a `DbSet<Product>` to interact with the database.
3. **ProductService Class**:
- **Constructor**: Accepts an instance of `ApplicationDbContext`.
- **GetTop10PercentByRevenue Method**:
- Calculates the total number of products.
- Determines the count of products that should be in the top 10 percent by dividing the total count by 10 and rounding up.
- Orders the products by revenue in descending order.
- Takes the top `top10PercentCount` products.
### SQL Server Implementation:
If you prefer to use raw SQL with Entity Framework Core, you can achieve the same result using a stored procedure or a direct SQL query. Here is an example of a direct SQL query:
```csharp
public List<Product> GetTop10PercentByRevenue()
{
var top10PercentCount = _context.Products.Count() * 0.1;
return _context.Products.FromSqlRaw(
"SELECT TOP (@top10PercentCount) * FROM Products ORDER BY Revenue DESC",
new SqlParameter("@top10PercentCount", (int)Math.Ceiling(top10PercentCount)))
.ToList();
}
```
### Explanation:
- **FromSqlRaw Method**: Executes a raw SQL query and maps the results to `Product` objects.
- **SqlParameter**: Ensures that the parameter is safely passed to the SQL query, preventing SQL injection.
This approach provides a straightforward way to find products in the top 10 percent by revenue using Entity Framework Core.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
BetterV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
BetterV7qwen2.5-coder-7b-instruct
BetterV8openai/gpt-4.1
BetterV9anthropic/claude-opus-4-7
WorseV12openai/gpt-4o
BetterComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.