2

这是我的 sqlite 声明:

select code ,  
case  when  (max(close)-min(close))/min(close)<0.1 then "grade1" 
when  (max(close)-min(close))/min(close) <0.2  then  "grade2"
when  (max(close)-min(close))/min(close) <0.3  then  "grade3"
else "grade4"  end  as  type;

使用 (max(close)-min(close))/min(close) 了 3 次,我怎样才能使我的查询语句更简单?

4

1 回答 1

1

将计算放入子查询中:

SELECT code,
       CASE WHEN CloseRatio < 0.1 THEN 'grade1'
            WHEN CloseRatio < 0.2 THEN 'grade2'
            WHEN CloseRatio < 0.3 THEN 'grade3'
            ELSE                       'grade4'
       END AS type
FROM (SELECT code,
             (MAX(close) - MIN(close)) / MIN(close) AS CloseRatio
      FROM MyTable);
于 2012-11-10T11:30:37.243 回答