0

今天早些时候我得到了一些很大的帮助,但是当我应该在查询中实现它时比我想象的要困难得多。我的真实查询包括几个左连接。

我想添加两列,T3MOutput 和 T1GOutput,显示基于 T3M 和 T1G 列的 10-1 排名。在排名尺度中,最重要的值应该得到 10,最低(负值)应该得到 1。注意!一场赛马在开始时可以少于 15 匹马。在十个最佳值获得 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 
4

3 回答 3

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 Fiddle 。

于 2013-03-25T20:15:40.293 回答
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

在此处查看小提琴。

于 2013-03-26T16:29:03.183 回答
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

可能必须转换为整数值

于 2013-03-25T20:12:18.040 回答