日期:2014-05-17  浏览次数:20560 次

sql行变列求最大最小
SQL code

859    ASX劲炫(进口)    19.80
859    ASX劲炫(进口)    22.30
859    ASX劲炫(进口)    23.30
859    ASX劲炫(进口)    25.50
859    ASX劲炫(进口)    18.38
859    ASX劲炫(进口)    19.80
859    ASX劲炫(进口)    20.80
859    ASX劲炫(进口)    21.80
859    ASX劲炫(进口)    23.98


行变列 最大 最小

------解决方案--------------------
SQL code
WITH test (id,NAME,number)
 AS (
 SELECT '859',    'ASX劲炫(进口)',    19.80
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)', 22.30
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)',23.30
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)',  25.50
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)',  18.38
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)', 19.80
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)',  20.80
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)', 21.80
 UNION ALL 
 SELECT '859',    'ASX劲炫(进口)', 23.98
 )
 SELECT id,name,MAX(number) [max],MIN(number)[min] FROM test
 GROUP BY id,name
 
 /*
 id   name          max                                     min
 ---- ------------- --------------------------------------- ---------------------------------------
 859  ASX劲炫(进口)     25.50                                   18.38
 
 (1 行受影响)
 
 */