문제

나는 하루 종일이 문제로 고투하고 있었고, 내가 알고있는 모든 방법을 시도해 보았습니다. 문제는 다음과 같습니다.

일하는 필드는 다음과 같은 형식을 가지고 있습니다.

xxx-연도 예제 : 001-2014 (문자열)

우리는 이러한 기록을 가지고 있다고 가정합니다.

  • 001-2014
  • 011-2014
  • 013-2013

내가 얻을 것으로 기대하는 결과는 011-2014 (즉, 가장 큰 값으로 가장 빠른 해)

PostgreSQL에서 잘 작동하는이 쿼리를 썼습니다.

select split_part(numero_bl, '-', 1) as part1, 
       split_part(numero_bl, '-', 2) as part2 
       from livraisons 
ORDER BY part2 desc,part1 desc limit 1
.

split_part 함수는 'numero_bl'열을 2 부분으로 분할합니다. 결과는 다음과 같습니다.

파트 1 | part2

001 | 2014

파이썬 코드 안에 도입하려고하면 오름차순 오더 을 사용하여 올바른 값을 반환하지만 ( 'none') 여기에 Python에서 내 기능의 간단한 버전입니다.

def set_numero_bl(self, cr, uid, ids,name,arg,context=None):
        res={}   
        cr.execute("""select split_part(numero_bl, '-', 1) as part1,
                    split_part(numero_bl, '-', 2) as part2 
                    from livraisons 
                    ORDER BY part2 desc,part1 desc limit 1""")
        execution = cr.fetchone()
        part1 = execution[0]
        part2 = execution[1]
        if part1:
              if part2:
                annee = int(part2)
                nombre = int(part1)+1
                valeur_final= str('%0*d' % (3, nombre))+"-"+str(annee)
                for livraisons in self.browse(cr, uid, ids):
                    res[livraisons.id]= valeur_final
        else: 
            valeur_final= str('%0*d' % (3, 1))+"-"+datetime.datetime.strftime(datetime.datetime.now(), '%Y')
            for livraisons in self.browse(cr, uid, ids):
                res[livraisons.id]= valeur_final
        return res
.

물론 'else 절'을 넣지 않을 때는 반환됩니다 ( '없음',)

해결을 위해 어쨌든 또는 다른 방법으로 해결할 수 있습니까?

고맙습니다

도움이 되었습니까?

해결책 2

나는 내 문제를 해결할 수있는 방법을 알았습니다. 나는 내 쿼리를이

로 변경했습니다.
cr.execute("""select max(numero_bl) as part from livraisons 
           where split_part(numero_bl, '-', 2) >= %s""",
        ((datetime.datetime.strftime(datetime.datetime.now(), '%Y')),))
.

첫째, 연도 파트가 현재 연도 보다 큰 기록 만 유지합니다.....

누구도 관심이있는 경우 전체 코드가 있습니다.

 def set_numero_bl(self, cr, uid, ids,name,arg,context=None):
    res={}   
    cr.execute("""select max(numero_bl) as part 
                 from livraisons 
        where split_part(numero_bl, '-', 2) >= %s""", 
        ((datetime.datetime.strftime(datetime.datetime.now(), '%Y')),)) 
    valeur = cr.fetchone()[0]
    if valeur:
        value = valeur.split('-'); 
        nombre = int(value[0])+1
        valeur_final= str('%0*d' % (3, nombre))+"-"+datetime.datetime.strftime(datetime.datetime.now(), '%Y')
        for livraisons in self.browse(cr, uid, ids):
            res[livraisons.id]= valeur_final
    else: 
        valeur_final= str('%0*d' % (3, 1))+"-"+datetime.datetime.strftime(datetime.datetime.now(), '%Y')
        for livraisons in self.browse(cr, uid, ids):
            res[livraisons.id]= valeur_final
    return res
.

답장을 위해 감사합니다

다른 팁

해당 컬럼에는 널이 있습니다.내림차순 주문을 요구할 때 널이 먼저 올 것입니다 :

select *
from (values (1), (null)) s(a)
order by a desc;
 a 
---

 1
(2 rows)
.

널을 유지하려면 NULLS를 지속하도록 알려줍니다.

select *
from (values (1), (null)) s(a)
order by a desc nulls last;
 a 
---
 1

(2 rows)
.

또는 NULLS

을 필터링하십시오.
select *
from (values (1), (null)) s(a)
where a is not null
order by a desc;
 a 
---
 1
(1 row)
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top