I have a query:
SELECT round(addTime/qty, 0) AS col1,
sum(qty) AS col2,
cId AS Id
FROM MyTable
WHERE qty > 0
GROUP BY round(addTime/qty, 0), cId
ORDER BY round(addTime/qty, 0)
But I have to replace addTime with a
SUM(CASE WHEN qty = 1 THEN 1 ELSE DATEDIFF(second,startTime,endTime) END)/qty, 0)
And when I do this:
SELECT round(SUM(case when qty = 1 then 1 else DATEDIFF(second,startTime,endTime) end)/qty, 0) AS col1,
sum(qty) AS col2,
cId
FROM MyTable
WHERE qty > 0
GROUP BY round(SUM(case when qty = 1 then 1 else
DATEDIFF(second,startTime,endTime) end)/qty, 0)/qty, 0), cId
ORDER BY round(SUM(case when qty = 1 then 1 else DATEDIFF(second,startTime,endTime) end)/qty, 0)/qty, 0)
I get
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
My idea was that I can use that new query as variable and then use it where I need but then I ran into trouble with MySql syntax.
Big thanks in advance!
Try it with a sub query in the from clause:
Select * from (select round(...) as col1 from MyTable) as tmp where ... Group By tmp.col1
SELECT *
FROM (
SELECT
round(SUM(case when qty = 1 then 1 else DATEDIFF(second, startTime,endTime) end)/qty, 0) AS col1,
sum(qty) AS col2,
cId
FROM MyTable
WHERE qty > 0
) AS t
GROUP BY t.col1, t.cId
ORDER BY t.col1
Also I'm not sure, but your DIFFTIME
function if different from MySQL DIFFTIME
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.