繁体   English   中英

选择最大(字段)不起作用

[英]Select Max(Field) not working

我有以下问题:

SELECT MAX(SEQUENCE) FROM TABLE1
WHERE FIELD99= 'NUMBER'

该查询带来了Field1的MAX值,并且运行良好。 现在,我想将此信息与另一个表结合起来以收集其他数据,并且我正在使用它:

SELECT MAX(B.SEQUENCE), A.COUNTRY, A.COMPANY, 
A.DOCNUMBER, B.USERID, B.REASON
FROM TABLE1 A
INNER JOIN TABLE2 B
ON A.DOCNUMBER=B.DOCNUMBER
WHERE A.DOCNUMBER='NUMBER'
GROUP BY B.SEQUENCE, A.COUNTRY, A.COMPANY, A.DOCNUMBER, B.USERID, B.REASON

我知道问题出在分组,但是我不知道,我尝试了几种方法,但是我注意到,当我删除所有其他字段时,它只会带来我想要的数据。

如果运行给出的第一个示例,将得到结果:007,这是字段SEQUENCE的最大值。 如果我运行另一个查询,我将得到:

001 + all the other fields
002 + all the other fields
003 + all the other fields
004 + all the other fields
005 + all the other fields
006 + all the other fields
007 + all the other fields

我希望结果将与第一个查询相同的行带入选择中添加的数据

我能够执行此查询并解决问题

SELECT DISTINCT A.REFFSEQU, A.CTRYCODE, A.COMPCODE, A.INVONUMB, A.REFFERRS, A.REFFERBY              
FROM TABLE1 A, TABLE2 B                                    
WHERE A.CTRYCODE=B.CTRYCODE AND A.COMPCODE=B.COMPCODE AND A.INVONUMB=B.INVONUMB
AND B.INVOCRDT > '20180101'                                                    
AND (A.REFFSEQU = ANY                                                          
 (SELECT MAX(REFFSEQU)                                                         
 FROM TABLE1 AS D                                                    
 WHERE D.CTRYCODE=A.CTRYCODE AND D.COMPCODE=A.COMPCODE                         
 AND D.INVONUMB= A.INVONUMB))                                                  
AND A.REFFERRS = 'DUPLICATION, RECORD REJEC'

这就是结果,对不起如何在这里做表格

REFFSEQU    CTRYCODE    COMPCODE    INVONUMB    REFFERRS                       REFFERBY
001           631          01   number1         DUPLICATION, RECORD REJEC             id  
002           631          01   number2         DUPLICATION, RECORD REJEC             id
002           631          01   number3         DUPLICATION, RECORD REJEC             id
007           631          01   number4         DUPLICATION, RECORD REJEC             id
007           631          01   number5         DUPLICATION, RECORD REJEC             id

我会使用CTE来做到这一点。 它将简化分组。

with tmp as (
  select docnumber, 
         max(sequence) as sequence,
         country,
         company
    from table1
    group by docnumber)
select a.sequence, a.country, a.company, a.docnumber, b.userid, b.reason
  from tmp a
    join table2 b on a.docnumber = b.docnumber
    where a.docnumber = 'NUMBER'

注意,我使用的是table1而不是table2序列。

编辑:这是基于评论的初步猜测:

with tmp as (
  select docnumber, max(sequence) as sequence
    from table1
    group by docnumber),
tmp2 as (
  select distinct docnumber
    from table2
    where reason = 'Duplicated')
select a.sequence, a.country, a.company, a.docnumber, b.userid, b.reason
  from tmp a
    join table2 b on a.docnumber = b.docnumber
    join tmp2 c on a.docnumber = c.docnumber
    where a.docnumber = 'NUMBER'

如果每个文档编号的序列不超过一个,且原因为“重复”,则不需要tmp2定义中的非重复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM