使SQL选择最后的非零金额

时间:2013-11-06 11:58:10

标签: mysql sql

我目前有一个目前采用此格式的表

id | year | jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec
A    2001  0.00   1.00  0.00  0.00  1.00  1.00  0.00  0.00  0.00  0.00  0.00  0.00
A    2003  0.00   1.00  0.00  0.00  1.00  1.00  0.00  0.00  0.00  0.00  0.00  0.00
A    2005  0.00   0.00  0.00  0.00  0.00  0.00  0.00  0.00  3.00  0.00  0.00  0.00

我想要得到的结果是

id | firstyear | lastyear | lastpaidamount
A      2001       2005           3.00

我尝试使用此代码,但我得到的是lastpaidamount

的NULL
    SELECT 
  a.id,
  MIN(YEAR) AS firstyear,
  MAX(YEAR) AS lastyear,
  b.lastpaidadmount 
FROM
  table1 a 
  LEFT JOIN 
    (SELECT 
      id,
      CASE
        WHEN dec > 0 
        THEN dec 
        WHEN nov > 0 
        THEN nov 
        WHEN oct > 0 
        THEN oct 
        WHEN sep > 0 
        THEN sep 
        WHEN aug > 0 
        THEN aug 
        WHEN jul > 0 
        THEN jul 
        WHEN jun > 0 
        THEN jun 
        WHEN may > 0 
        THEN may 
        WHEN apr > 0 
        THEN apr 
        WHEN mar > 0 
        THEN mar 
        WHEN feb > 0 
        THEN feb 
        WHEN jan > 0 
        THEN jan 
      END AS lastpaidamount 
    FROM
      table1 
    WHERE YEAR = 'a.lastyear' 
    GROUP BY id) b 
    ON a.id = b.id 
WHERE jan > 0 
  OR feb > 0 
  OR mar > 0 
  OR apr > 0 
  OR may > 0 
  OR jun > 0 
  OR jul > 0 
  OR aug > 0 
  OR aug > 0 
  OR sep > 0 
  OR oct > 0 
  OR nov > 0 
  OR dec > 0 
GROUP BY id ;

感谢您的任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我很想用一个视图将你当前的表分成更合乎逻辑的东西: -

CREATE VIEW `vw_extract` AS 
SELECT id, `year` AS aYear, 12 AS aMonth, `dec` AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 11 AS aMonth, nov AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 10 AS aMonth, oct AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 9 AS aMonth, sep AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 8 AS aMonth, aug AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 7 AS aMonth, jul AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 6 AS aMonth, jun AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 5 AS aMonth, may AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 4 AS aMonth, apr AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 3 AS aMonth, mar AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 2 AS aMonth, feb AS aAmount FROM table1 UNION
SELECT id, `year` AS aYear, 1 AS aMonth, jan AS aAmount FROM table1 ;

然后你的结果可以使用这个(相当长时间由相当标准的兼容)查询: -

SELECT Sub1.id, Sub1.firstyear, Sub1.lastyear, Sub3.aAmount
FROM
(
    SELECT id, MIN(aYear) AS firstyear, MAX(aYear) AS lastyear
    FROM vw_extract
    WHERE aAmount > 0
    GROUP BY id
) Sub1
INNER JOIN
(
    SELECT id, aYear, MAX(aMonth) AS lastmonthofyear
    FROM vw_extract
    WHERE aAmount > 0
    GROUP BY id, aYear
) Sub2
ON Sub1.id = Sub2.id
AND Sub1.lastyear = Sub2.aYear
INNER JOIN
(
    SELECT id, aYear, aMonth, aAmount
    FROM vw_extract
    WHERE aAmount > 0
) Sub3
ON Sub2.id = Sub3.id
AND Sub2.aYear = Sub3.aYear
AND Sub2.lastmonthofyear = Sub3.aMonth

或者使用MySQL GROUP_CONCAT函数做一点小事

SELECT id, MIN(aYear) AS firstyear, MAX(aYear) AS lastyear, SUBSTRING_INDEX(GROUP_CONCAT(aAmount ORDER BY aYear DESC, aMonth DESC), ',', 1) AS lastpaidamount
FROM vw_extract
WHERE aAmount > 0
GROUP BY id