1

私は間違いなく 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 サーバー 2008 を使用しています。

4

1 に答える 1

1

distinctuseを使用する代わりにgroup by. distinctを aに変更し、日付group byを追加するだけの、クエリの 1 つのバージョンを次に示します。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]
于 2013-04-26T15:15:49.497 に答える