SQL Select语句每个供应商子查询的最长付款日期

时间:2013-04-26 15:04:52

标签: sql subquery aggregate-functions max multiple-tables

我绝对是一个SQL新手,所以也许你们可以帮助我理解一些东西,并可能帮助解决我认为必须是一个简单的问题。

我运行此查询并返回我想要的结果并且感觉正确。

SELECT distinct 
   isnull(T3.[name],'') [Vendor Name]
  ,T1.[active] [Active]
  ,T1.[number] [Vendor Number]
  ,T1.[mnemonic] [Mnemonic]
  ,T1.[bank_acc] [Bank Account]
  ,isnull(T1.[our_acct_number],'') [Our Account Number]
  ,isnull(T1.[fed_id_num],'') [Fed ID]
  ,isnull((select  T2.[ap_contact_name] [AP Contact] from TABLE2 T2 
  where [ap_contact] = '1' and T2.[number] = T1.[number]),'') [AP Contact]
  ,isnull(T2.[ap_contact_phone],'') [Contact Phone Number]
  ,isnull(T1.[print_1099],'') [Print 1099?]
  ,isnull(T1.[expense_type],'') [Expense Type]
  ,isnull(T4.[remit_addr_1],'') [Remit Street Address]
  ,isnull(T4.[remit_addr_2],'') [Other Adress Info]
  ,isnull(T4.[remit_addr_3],'') [Remit City, State, Zip]
  ,isnull(T4.[remit_1099], '') [Remit 1099?] 


FROM 
TABLE1 T1
LEFT outer JOIN
TABLE2 T2
ON T1.[number] = T2.[number]
left outer join
TABLE3 T3
ON T1.[number] = T3.[number]
left outer join
TABLE4 T4
on T1.[number] = T4.[number]
left join
TABLE5 T5
ON T1.[number] = T5.[txn_vendor]
LEFT join
TABLE6 T6
ON T5.[txn_inv_num] = T6.[inv_num]


 WHERE
 T1.[active] ='Y'

 order by 
[Vendor Name]

但是,当我将此子查询添加到select语句以返回每个供应商的最后支付日期

 ,(SELECT
                 MAX(T6.[inv_date])
                 FROM
                Table6 T6
                WHERE

               T5.[txn_inv_num] = T6.[inv_num]
               group by T5.[txn_vendor],T5.[txn_inv_num]
                 )
                 [Last Pymt Date]

我得到了大约20行重复供应商和不同日期的结果。我正在尝试将每个供应商的最后付款日期添加到结果集中。

我也尝试过以其他方式使用虚拟表,但我没有运气。任何意见都表示赞赏。

使用SQL Server 2008。

1 个答案:

答案 0 :(得分:1)

而不是使用distinct使用group by。以下是您的查询的一个版本,只是将distinct更改为group by并为日期添加max()

SELECT distinct 
   isnull(T3.[name],'') [Vendor Name]
  ,T1.[active] [Active]
  ,T1.[number] [Vendor Number]
  ,T1.[mnemonic] [Mnemonic]
  ,T1.[bank_acc] [Bank Account]
  ,isnull(T1.[our_acct_number],'') [Our Account Number]
  ,isnull(T1.[fed_id_num],'') [Fed ID]
  ,isnull((select  T2.[ap_contact_name] [AP Contact] from TABLE2 T2 
  where [ap_contact] = '1' and T2.[number] = T1.[number]),'') [AP Contact]
  ,isnull(T2.[ap_contact_phone],'') [Contact Phone Number]
  ,isnull(T1.[print_1099],'') [Print 1099?]
  ,isnull(T1.[expense_type],'') [Expense Type]
  ,isnull(T4.[remit_addr_1],'') [Remit Street Address]
  ,isnull(T4.[remit_addr_2],'') [Other Adress Info]
  ,isnull(T4.[remit_addr_3],'') [Remit City, State, Zip]
  ,isnull(T4.[remit_1099], '') [Remit 1099?],
  max(t6.inv_date) as [Last Pymt Date]


FROM 
TABLE1 T1
LEFT outer JOIN
TABLE2 T2
ON T1.[number] = T2.[number]
left outer join
TABLE3 T3
ON T1.[number] = T3.[number]
left outer join
TABLE4 T4
on T1.[number] = T4.[number]
left join
TABLE5 T5
ON T1.[number] = T5.[txn_vendor]
LEFT join
TABLE6 T6
ON T5.[txn_inv_num] = T6.[inv_num]


 WHERE
 T1.[active] ='Y'

group by
   isnull(T3.[name],'') [Vendor Name]
  ,T1.[active] [Active]
  ,T1.[number] [Vendor Number]
  ,T1.[mnemonic] [Mnemonic]
  ,T1.[bank_acc] [Bank Account]
  ,isnull(T1.[our_acct_number],'') [Our Account Number]
  ,isnull(T1.[fed_id_num],'') [Fed ID]
  ,isnull((select  T2.[ap_contact_name] [AP Contact] from TABLE2 T2 
  where [ap_contact] = '1' and T2.[number] = T1.[number]),'') [AP Contact]
  ,isnull(T2.[ap_contact_phone],'') [Contact Phone Number]
  ,isnull(T1.[print_1099],'') [Print 1099?]
  ,isnull(T1.[expense_type],'') [Expense Type]
  ,isnull(T4.[remit_addr_1],'') [Remit Street Address]
  ,isnull(T4.[remit_addr_2],'') [Other Adress Info]
  ,isnull(T4.[remit_addr_3],'') [Remit City, State, Zip]
  ,isnull(T4.[remit_1099], '') [Remit 1099?] 


 order by 
[Vendor Name]