我有一个有效的查询,但必须有更好的方法来做到这一点。 这是工作查询
SELECT gameid FROM(
SELECT gameid, SUM(count) as total FROM (
SELECT IF(vanguard = 1, 30, gameid) as gameid, count FROM (
SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid) as m
) as l
GROUP BY gameid
ORDER BY total DESC LIMIT 1) as k
主要查询
SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid
gameid vanguard 所以这是棘手的部分。我需要它来分组并计算所有值,但是如果gameid = 30或vanguard = 1那么它们应该全部计算在一起。
所以使用上面的数据,我唯一需要的是在这种情况下返回的gameid gameid 30.它将拥有最多的数量,即13。
必须有比我更好的方式。
编辑:到目前为止,这一点非常感谢Ed Gibbs。但是仍然返回两个值,我猜测只有另一个子查询?得到只是gameid。
SELECT
CASE WHEN vanguard = 1 THEN 30 ELSE gameid END AS gameid,
SUM(count) AS total
FROM (SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid) as l
GROUP BY CASE WHEN vanguard = 1 THEN 30 ELSE gameid END
ORDER BY total DESC
LIMIT 1;
答案 0 :(得分:1)
如果你的意思是底线&#34; 1&#34;和&#34; 30&#34;必须计算在一起然后这应该工作:
(require '[clojure.tools.reader :as r])
(require '[clojure.tools.reader.reader-types :as rt])
(def reader (rt/indexing-push-back-reader
"(+ \n;; comment here \n 1 \n a)"))
(def s-expr (r/read reader)) ;; => (+ 1 a)
(meta s-expr) ;; => {:line 1, :column 1, :end-line 4, :end-column 4}
(nth s-expr 0) ;; => +
(meta (nth s-expr 0)) ;; => {:line 1, :column 2, :end-line 1, :end-column 3}
(nth s-expr 1) ;; => 1
(meta (nth s-expr 1)) ;; => nil (no metadata on literals)
(nth s-expr 2) ;; => a
(meta (nth s-expr 2)) ;; => {:line 4, :column 2, :end-line 4, :end-column 3}