Good day, please tell me on this issue:
There is a table of the form:
+-----------------------+ |id|user_id|point|status| ------------------------- |1 | 1 | 3 | 0 | ------------------------- |2 | 1 | 3 | 0 | ------------------------- |3 | 2 | 1 | 0 | ------------------------- |4 | 2 | 0 | 0 | ------------------------- |5 | 3 | 3 | 0 | ------------------------- |6 | 3 | 1 | 0 | ------------------------- |7 | 1 | 0 | 1 | +-----------------------+ What should be the SQL query to get a table of the form:
+-----------------------------------+ |user_id|total_point|point_3|point_1| ------------------------------------- | 1 | 6 | 2 | 0 | ------------------------------------- | 3 | 4 | 1 | 1 | ------------------------------------- | 2 | 1 | 0 | 1 | +-----------------------------------+ To select the lines where status = 0 and the sample was grouped by user_id, also for each user_id counted:
- total points (total_point)
- number of points = 3 (point_3)
- number of points = 1 (point_1)
And they are ordered first by point_1, then point_3 and total_point.
SELECT user_id, SUM(point) AS total_point, COUNT(point) AS point_3 GROUP BY point HAVING point = 3, COUNT(point) AS point_1 GROUP BY point HAVING point = 1 FROMtable` WHERE status = 0 GROUP BY user_id ORDER BY point_1, point_3, total_point` - Andrey