Home > Mobile >  When I create VIEW there is a wrong result, while simple sql query gives a correct result
When I create VIEW there is a wrong result, while simple sql query gives a correct result

Time:07-28

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?

CodePudding user response:

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
  • Related