我今天早些时候收到了很多帮助,但是在我的查询中实现它比我想象的要困难得多。我的真实查询包括几个左连接。
我想添加两列,T3MOutput和T1GOutput,显示基于的10-1排名 T3M和T1G列。那里的最高值应该在排名范围内得到10,最低(负值)应该得到1.注意!赛马起跑时可少于15匹马。在10个最佳值恢复10-1之后,其余值也应该得到值1.
小提琴:http://sqlfiddle.com/#!2/9c5fc/4
我应该在哪里实现这行代码或其他解决方案?我只在整个过程中获得值1.
(SELECT @row:=11 r) rows
以下是所有代码:
SELECT
base.number as NR,
rank.rpo AS RPO,
rank.rsp AS RSP,
rank.rsv AS RSV,
timer.t3m AS T3M, CASE WHEN @row>1 THEN @row:=@row-1 ELSE 1 END T3MOutput,
timer.t1g AS T1G, CASE WHEN @row>1 THEN @row:=@row-1 ELSE 1 END T1GOutput
FROM round
LEFT JOIN base
ON round.id = base.round_id
LEFT JOIN rank
ON round.id = rank.round_id AND rank.number = base.number
LEFT JOIN timer
ON round.id = timer.round_id AND timer.number = base.number
ORDER BY nr
答案 0 :(得分:1)
第一
SELECT MIN(timer.t3m) AS MinT3M, MAX(timer.t3m) AS MaxT3M,
MIN(timer.t1g) AS MinT1G, MAX(timer.t1g) AS MaxT1G
FROM round INNER JOIN timer ON round.id = timer.round_id
WHERE round.round_date = '2013-03-22'
AND round.gameform = 'V65'
AND round.gameform_info = 'V65-1'
AND round.gameform not like "OSPEC";
然后你可以使用理查德在我输入时提出的计算。
请参阅使用您的数据的SQL小提琴here。
答案 1 :(得分:0)
假设您正在尝试对这些值进行排名,一种可能的方法是首先确定T3M和T1G的最小值和最大值,然后使用以下内容:
SELECT ...,
((T3M-@T3M_Min)/(@T3M_Max-@T3M_Min))*9+1 as T3MOutput,
((T1G-@T1G_Min)/(@T1G_Max-@T1G_Min))*9+1 as T1GOutput
可能必须转换为整数值
答案 2 :(得分:0)
这有点棘手,也许这就是你要找的东西?
SELECT
q2.*,
CASE WHEN @row>1 AND T1G is not NULL THEN @row:=@row-1 ELSE 1 END T1GOutput
FROM (
SELECT
q1.*,
CASE WHEN @row>1 AND T3M is not NULL THEN @row:=@row-1 ELSE 1 END T3MOutput
FROM (
SELECT
base.number as NR,
rank.rpo as RPO,
rank.rsp as RSP,
rank.rsv as RSV,
timer.t3m as T3M,
timer.t1g AS T1G
FROM
round LEFT JOIN base
ON round.id = base.round_id
LEFT JOIN rank
ON round.id = rank.round_id and rank.number = base.number
LEFT JOIN timer
ON round.id = timer.round_id and timer.number = base.number
order by T3M DESC
) q1, (SELECT @row:=11) rows
) q2, (SELECT @row:=11) rows
ORDER BY
T1G
请参阅小提琴here。