The error is at END AS number OR snumber
. After you remove OR snumber
, there won't be any error but there won't be any result too; against the example data you've provided. Try changing WHERE x.totalrows = 1 AND wt >= 2.5
to WHERE wt >= 2.5
only because you're already doing a CASE
expression:
CASE
WHEN x.totalrows > 1
THEN stones.snumber
ELSE stones.number
END AS number
So adding WHERE x.totalrows = 1
won't yield anything from the CASE
.
Demo fiddle
Update:
Well, after long discussion in comment, I think I got it:
SELECT inv.ID,
CASE WHEN inv.snumber <> ""
THEN inv.snumber ELSE inv.number
END AS numberORSnumber,
inv.wt,
inv.qt,
COUNT(*) OVER() AS totalrows,
CASE WHEN inv.number=v.number
AND inv.snumber="" THEN 0 ELSE 1 END AS Checking
FROM stones inv
CROSS JOIN
(SELECT number FROM stones WHERE snumber <> "" group by number) v
WHERE wt >= 2.5
HAVING Checking <> 0
ORDER BY ID ASC;
I end up adding a CROSS JOIN
with a subquery that return number
value that have snumber
as per OP requirement. Then with that, I use CASE
to do the checking and finally use HAVING
to filter them out from the checking.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=40cb88da028a42dc147dc4224154ab05
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…