1

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

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

我尝试使用此代码,但我得到的最后支付金额为 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 ;

感谢这里的任何帮助,谢谢!

4

1 回答 1

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
于 2013-11-06T13:49:05.447 回答