【问题标题】:How to select min month and year?如何选择最小月份和年份?
【发布时间】:2017-10-13 16:07:32
【问题描述】:

我有一张这样的桌子:

cuota_abono(**cod_abono**, **mes**, **anio**, valor, pago_acumulado, estado)

where:
-cod_abono: VARCHAR (PK)  
-mes: INT (month -- values from 1 to 12) (PK)  
-anio: INT (year) (PK)  
-valor: DECIMAL(10,2)  
-pago_acumulado: DECIMAL(10,2)  
-estado(TINYINT -- 0 or 1)  

问题是我需要选择最旧的未付款 cuota_abono(其中 estado=0)。

例如:

cod_abono mes   anio     valor  pago_acumulado  estado
  Z000     9    2015    315.00      100.00         0
  Z000     5    2016    315.00      315.00         1
  Z000     6    2016    315.00      200.00         0
  Z000     7    2016    315.00        0.00         0
  Z001     5    2016    315.00        0.00         0
  Z001     6    2016    315.00      315.00         1

鉴于我正在寻找最古老的 cuota_abono,我希望得到:

(Z000, 9, 2015, 315.00, 100.00, 0) --> 1° row

我试过这个查询:

select cod_abono, min(mes), min(anio), valor, pago_acumulado, estado
from cuota_abono 
where cod_abono="Z000" and cuota_paga=0

但是,我得到:

(Z000, *6*, 2015, 315.00, 100.00, 0)

从数字上讲,我实际上得到了 cod_abono="Z000" 和 estado=0 的最小月份,但它不正确。

有什么建议吗?

【问题讨论】:

  • 只标记您实际使用的一个 dbms。

标签: mysql sql select min


【解决方案1】:

使用order bylimit

select ca.*
from cuota_abono ca
where cod_abono = 'Z000' and cuota_paga = 0
order by anio, mes
limit 1;

【讨论】:

  • 是的,我刚刚意识到这个选项。谢谢!
猜你喜欢
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
相关资源
最近更新 更多