Round #665
10 May 2026, 08:37· csharp· qwen2.5-coder-7b-instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 3 / 0 / 2
- Journey weight
- 250
- Lightshift ΔL
- —
Prompt
Calculate a moving 7-day average of daily_sales using a window function.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0014 | 2026-05-10T08:37:34Z
Calculate a moving 7-day average of daily_sales using a window function.
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 calculate a moving 7-day average of `daily_sales` using a window function in SQL, you can use the `AVG()` function with the `OVER()` clause. Here's an example query that demonstrates how to achieve this:
```sql
SELECT
date,
daily_sales,
AVG(daily_sales) OVER (
ORDER BY date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS moving_7_day_avg
FROM
sales_data;
```
### Explanation:
- **`AVG(daily_sales)`**: This calculates the average of `daily_sales`.
- **`OVER()` Clause**:
- **`ORDER BY date`**: Orders the rows by the `date` column.
- **`ROWS BETWEEN 6 PRECEDING AND CURRENT ROW`**: Defines a window that includes the current row and the previous 6 rows. This effectively calculates the average over a 7-day period.
### Example Data:
Assume you have a table named `sales_data` with the following structure:
| date | daily_sales |
|------------|-------------|
| 2023-01-01 | 100 |
| 2023-01-02 | 150 |
| 2023-01-03 | 200 |
| 2023-01-04 | 250 |
| 2023-01-05 | 300 |
| 2023-01-06 | 350 |
| 2023-01-07 | 400 |
| 2023-01-08 | 450 |
### Result:
The query will produce the following result:
| date | daily_sales | moving_7_day_avg |
|------------|-------------|------------------|
| 2023-01-01 | 100 | NULL |
| 2023-01-02 | 150 | NULL |
| 2023-01-03 | 200 | NULL |
| 2023-01-04 | 250 | NULL |
| 2023-01-05 | 300 | NULL |
| 2023-01-06 | 350 | NULL |
| 2023-01-07 | 400 | 275 |
| 2023-01-08 | 450 | 325 |
### Notes:
- The first six rows will have `NULL` values for the moving average because there aren't enough preceding rows to calculate a 7-day average.
- If you want to include these initial rows with some default value (e.g., 0), you can use a `CASE` statement or a `COALESCE` function.
This query should work in most SQL databases that support window functions, such as PostgreSQL, MySQL 8.0+, SQL Server 2012+, and Oracle 11g+.
Enhanced response (with ZPI)
To calculate a moving 7-day average of `daily_sales` using a window function, we can use SQL with the `AVG()` function along with the `OVER()` clause. This approach is efficient and leverages the power of SQL's window functions to compute the desired averages.
Here's how you can implement this:
### SQL Implementation
```sql
SELECT
date,
daily_sales,
AVG(daily_sales) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS moving_7_day_avg
FROM
sales_data;
```
### Explanation
- **`AVG(daily_sales)`**: This calculates the average of `daily_sales`.
- **`OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)`**: This specifies a window that includes the current row and the previous six rows. The `ROWS BETWEEN 6 PRECEDING AND CURRENT ROW` part defines the sliding window.
### C# Implementation with Entity Framework Core
If you are using Entity Framework Core (EF Core) to interact with your database, you can execute this SQL query directly from your C# code.
```csharp
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
public class SalesData
{
public DateTime Date { get; set; }
public decimal DailySales { get; set; }
}
public class SalesContext : DbContext
{
public DbSet<SalesData> SalesData { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionStringHere");
}
}
public class Program
{
public static void Main()
{
using (var context = new SalesContext())
{
var movingAverages = context.SalesData
.FromSqlRaw("SELECT date, daily_sales, AVG(daily_sales) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS moving_7_day_avg FROM sales_data")
.ToList();
foreach (var data in movingAverages)
{
Console.WriteLine($"Date: {data.Date}, Daily Sales: {data.DailySales}, Moving 7-Day Avg: {data.moving_7_day_avg}");
}
}
}
}
```
### Explanation
- **`FromSqlRaw`**: This method allows you to execute raw SQL queries and map the results to your entity class.
- **`ToList`**: This converts the query result into a list of `SalesData` objects.
### Error Handling
In a production environment, you should add proper error handling around database operations. For example:
```csharp
try
{
var movingAverages = context.SalesData
.FromSqlRaw("SELECT date, daily_sales, AVG(daily_sales) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS moving_7_day_avg FROM sales_data")
.ToList();
foreach (var data in movingAverages)
{
Console.WriteLine($"Date: {data.Date}, Daily Sales: {data.DailySales}, Moving 7-Day Avg: {data.moving_7_day_avg}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
```
### Dependency Injection
If you are using dependency injection, you can register your `DbContext` in the service collection:
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SalesContext>(options =>
options.UseSqlServer("YourConnectionStringHere"));
}
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<SalesContext>();
// Your code here
}
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) => { });
}
```
This setup ensures that your `DbContext` is properly managed and injected into your application.
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
BetterV12openai/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.