1

我试图使用 Over Partition 创建基于 SupplierAccountNumber 的行号,然后按 DateTimeCreated 排序,然后只显示记录 1。我当前的脚本我收到一个错误,说 Invalid column name 'RowNum'??

我有一个包含多个地址的供应商的电子邮件地址列表,我只想选择最近的电子邮件地址。有更好的方法吗?

SELECT plsuppliercontact.plsuppliercontactid, 
       plsupplieraccount.supplieraccountnumber, 
       plsupplieraccount.supplieraccountname, 
       plsupplieraccount.supplieraccountshortname, 
       plsuppliercontactvalue.contactvalue, 
       syscontacttype.name, 
       Rownum = Row_number() 
                  OVER( 
                    partition BY plsupplieraccount.supplieraccountnumber 
                    ORDER BY plsuppliercontactvalue.datetimecreated DESC) 
FROM   alops.dbo.plsupplieraccount PLSupplierAccount, 
       alops.dbo.plsuppliercontact PLSupplierContact, 
       alops.dbo.plsuppliercontactvalue PLSupplierContactValue, 
       alops.dbo.syscontacttype SYSContactType 
WHERE  plsupplieraccount.plsupplieraccountid = 
       plsuppliercontact.plsupplieraccountid 
       AND plsuppliercontactvalue.plsuppliercontactid = 
           plsuppliercontact.plsuppliercontactid 
       AND syscontacttype.syscontacttypeid = 
           plsuppliercontactvalue.syscontacttypeid 
       AND (( syscontacttype.name = 'E-mail Address' )) 
       AND rownum = 1;
4

2 回答 2

2

您没有使用您正在使用的 RDBMS 指定,但它们中的大多数仅在执行查询后应用别名。一个技巧是将查询包装在另一个处理此条件的查询中。例如:

SELECT *
FROM   (
    SELECT plsuppliercontact.plsuppliercontactid, 
           plsupplieraccount.supplieraccountnumber, 
           plsupplieraccount.supplieraccountname, 
           plsupplieraccount.supplieraccountshortname, 
           plsuppliercontactvalue.contactvalue, 
           syscontacttype.name, 
           Rownum = Row_number() 
                      OVER( 
                        partition BY plsupplieraccount.supplieraccountnumber 
                        ORDER BY plsuppliercontactvalue.datetimecreated DESC) 
    FROM   alops.dbo.plsupplieraccount PLSupplierAccount, 
           alops.dbo.plsuppliercontact PLSupplierContact, 
           alops.dbo.plsuppliercontactvalue PLSupplierContactValue, 
           alops.dbo.syscontacttype SYSContactType 
    WHERE  plsupplieraccount.plsupplieraccountid = 
           plsuppliercontact.plsupplieraccountid 
           AND plsuppliercontactvalue.plsuppliercontactid = 
               plsuppliercontact.plsuppliercontactid 
           AND syscontacttype.syscontacttypeid = 
               plsuppliercontactvalue.syscontacttypeid 
           AND (( syscontacttype.name = 'E-mail Address' ))
)
WHERE rownum = 1;
于 2014-05-09T11:18:58.560 回答
0

使用 MAX 聚合

SELECT plsuppliercontact.plsuppliercontactid, 
       plsupplieraccount.supplieraccountnumber, 
       plsupplieraccount.supplieraccountname, 
       plsupplieraccount.supplieraccountshortname, 
       plsuppliercontactvalue.contactvalue, 
       syscontacttype.name, 
       MAX(plsuppliercontactvalue.datetimecreated)
FROM   alops.dbo.plsupplieraccount PLSupplierAccount, 
       alops.dbo.plsuppliercontact PLSupplierContact, 
       alops.dbo.plsuppliercontactvalue PLSupplierContactValue, 
       alops.dbo.syscontacttype SYSContactType 
WHERE  plsupplieraccount.plsupplieraccountid = 
       plsuppliercontact.plsupplieraccountid 
   AND plsuppliercontactvalue.plsuppliercontactid = 
       plsuppliercontact.plsuppliercontactid 
   AND syscontacttype.syscontacttypeid = 
       plsuppliercontactvalue.syscontacttypeid 
   AND (( syscontacttype.name = 'E-mail Address' )) 
GROUP BY plsuppliercontact.plsuppliercontactid, 
         plsupplieraccount.supplieraccountnumber, 
         plsupplieraccount.supplieraccountname, 
         plsupplieraccount.supplieraccountshortname, 
         plsuppliercontactvalue.contactvalue, 
         syscontacttype.name
于 2014-05-09T11:13:39.653 回答