Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61814

When I create VIEW there is a wrong result, while simple SQL query gives a correct result

This is how I define a rule for my VIEW:

SELECT `yearByWeek`, `week`, ( SELECT MIN(dolphin_day.date) ) AS 'start', ( SELECT SUM(dolphin_day.countHour)) AS 'countHours'

FROM `dolphin_day`
GROUP BY `yearByWeek`, `week`
ORDER BY `yearByWeek` DESC, `week` DESC

❌ wrong result for VIEW is the following:

enter image description here

✅ correct result for SQL query:

enter image description here

Why result for view is totally wrong?

Upvotes: 0

Views: 67

Answers (1)

Barmar
Barmar

Reputation: 781380

The aggregations shouldn't be in subqueries.

SELECT `yearByWeek`, `week`, MIN(date) AS 'start', SUM(countHour) AS 'countHours'
FROM `dolphin_day`
GROUP BY `yearByWeek`, `week`
ORDER BY `yearByWeek` DESC, `week` DESC

Upvotes: 1

Related Questions