Round #666
10 May 2026, 08:38· csharp· qwen2.5-coder-7b-instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 3 / 4 / 0
- Journey weight
- 402
- Lightshift ΔL
- —
Prompt
Write a SQL query that returns products and their cumulative sales over time.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0015 | 2026-05-10T08:38:12Z
Write a SQL query that returns products and their cumulative sales over time.
ZERO POINT — 3 entries (structural invariants)
● hsl(27.69, 100%, 50%) async [feature]
● hsl(145.64, 100%, 50%) Task of T [feature]
● hsl(348.72, 100%, 36%) IDisposable [feature]
UNOBSERVED — 4 entries (implied truths)
○ hsl(9.23, 100%, 50%) interface [feature]
○ hsl(180.92, 100%, 28%) Database health check [function]
○ hsl(144.00, 100%, 42%) Connection resilience [function]
○ hsl(83.08, 100%, 42%) ILogger of T injection [function]
OBSERVED — 0 entries (explicit in request)
COMPLEXITY │ 7 entries │ spread: 172° │ band: LOW
Baseline response (no ZPI)
To write an SQL query that returns products and their cumulative sales over time, we need to assume a few things about your database schema:
1. You have a `Products` table with columns like `ProductID`, `ProductName`.
2. You have a `Sales` table with columns like `SaleID`, `ProductID`, `SaleDate`, `Quantity`.
The goal is to get the cumulative sales for each product over time, which means we need to sum up the quantities sold for each product up to each sale date.
Here's an example SQL query that accomplishes this:
```sql
WITH CumulativeSales AS (
SELECT
p.ProductID,
p.ProductName,
s.SaleDate,
s.Quantity,
SUM(s.Quantity) OVER (PARTITION BY p.ProductID ORDER BY s.SaleDate) AS CumulativeQuantity
FROM
Products p
JOIN
Sales s ON p.ProductID = s.ProductID
)
SELECT
ProductID,
ProductName,
SaleDate,
CumulativeQuantity
FROM
CumulativeSales
ORDER BY
ProductID, SaleDate;
```
### Explanation:
1. **Common Table Expression (CTE) `CumulativeSales`:**
- We join the `Products` table with the `Sales` table on `ProductID`.
- For each row in the joined table, we calculate the cumulative quantity sold up to that sale date using the `SUM(s.Quantity) OVER (PARTITION BY p.ProductID ORDER BY s.SaleDate)` window function. This function partitions the data by `ProductID` and orders it by `SaleDate`, then sums up the quantities for each partition.
2. **Final SELECT:**
- We select the `ProductID`, `ProductName`, `SaleDate`, and `CumulativeQuantity` from the CTE.
- The results are ordered by `ProductID` and `SaleDate`.
### Example Output:
If your data looks like this:
**Products Table:**
| ProductID | ProductName |
|-----------|-------------|
| 1 | Apple |
| 2 | Banana |
**Sales Table:**
| SaleID | ProductID | SaleDate | Quantity |
|--------|-----------|------------|----------|
| 101 | 1 | 2023-01-01 | 5 |
| 102 | 1 | 2023-01-02 | 3 |
| 103 | 2 | 2023-01-01 | 7 |
| 104 | 1 | 2023-01-03 | 2 |
The query will return:
| ProductID | ProductName | SaleDate | CumulativeQuantity |
|-----------|-------------|------------|--------------------|
| 1 | Apple | 2023-01-01 | 5 |
| 1 | Apple | 2023-01-02 | 8 |
| 1 | Apple | 2023-01-03 | 10 |
| 2 | Banana | 2023-01-01 | 7 |
This output shows the cumulative sales for each product up to each sale date.
Enhanced response (with ZPI)
To implement the request, we'll need to write a SQL query that retrieves products along with their cumulative sales over time. This involves joining multiple tables and using aggregate functions to calculate the cumulative sales.
Here's an example implementation in C#:
```csharp
using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Logging;
public class ProductSalesRepository
{
private readonly string _connectionString;
private readonly ILogger<ProductSalesRepository> _logger;
public ProductSalesRepository(string connectionString, ILogger<ProductSalesRepository> logger)
{
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public DataTable GetProductsWithCumulativeSales()
{
using (var connection = new SqlConnection(_connectionString))
{
try
{
connection.Open();
var query = @"
SELECT
p.ProductID,
p.ProductName,
SUM(s.SalesAmount) AS CumulativeSales
FROM
Products p
JOIN
Sales s ON p.ProductID = s.ProductID
GROUP BY
p.ProductID, p.ProductName
ORDER BY
p.ProductID";
using (var command = new SqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
var dataTable = new DataTable();
dataTable.Load(reader);
return dataTable;
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error retrieving product sales data");
throw;
}
}
}
}
```
### Explanation:
1. **Dependency Injection**: The `ProductSalesRepository` class takes a connection string and an `ILogger<ProductSalesRepository>` as dependencies through its constructor. This follows the Dependency Injection (DI) pattern, which is a best practice for managing dependencies in C# applications.
2. **SQL Query**: The SQL query retrieves products along with their cumulative sales over time. It joins the `Products` table with the `Sales` table on the `ProductID` column and uses the `SUM` function to calculate the cumulative sales for each product.
3. **Error Handling**: The code includes basic error handling using a try-catch block. If an exception occurs during the database operation, it logs the error using the provided logger and rethrows the exception.
4. **Data Retrieval**: The query result is loaded into a `DataTable` and returned to the caller.
This implementation ensures that the SQL query is executed safely and efficiently, with proper error handling and dependency injection.
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
WorseV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
WorseV7qwen2.5-coder-7b-instruct
WorseV8openai/gpt-4.1
WorseV9anthropic/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.