2

I am having trouble with a join I need to do - Here's what the problem is: I have a database of 17 salespeople, each salesperson has his own totals such as expenses and advances and commissions. In my database there are about 200 customers that can have up to 3 salespeople on them. That is the minority but it does exist. Each salesperson is paid on invoices for that account. Some of my salespeople are only the 2nd salesperson on an account, meaning they are never the 1st, so if I do a join of a salesperson to invoices - he doesn't have any invoices because he is never salesman 1. I can pull invoices for him as salesman 2 when I am only looking at invoices, but every 8 weeks I need to join his expenses where he is salesman 1 to his invoices where he is salesman 2. I cannot get it to work or come up with a solution. I have a salesperson query so every 8 weeks I can call salesman number 100 to see his expenses I have a invoices/payments query and see what he has in for payments - but my 3rd query where I am trying to bring the salesman together with his payments doesn't work because he is the 1st salesman in expenses and the 2nd salesman in the invoices/payments I have tried every which way in SQL and can't seem to get it right.

Is there a way I can join the salesman1 from the salesman query to the invoices/payments query on slmn1 or slmn2 or slmn3?

this example is too complex:

FROM qryFinalWeek
INNER JOIN QryFW ON qryFinalWeek.SLMN = QryFW.SLMN2 OR QryFW.SLMN3 OR QryFW.Salesrep1
WHERE (((QryFW.PDATE)=[FDATE]+4));

this example is only giving invoices where he's first salesman so i can't get any commissions if he is the second:

WHERE (((qryFinalWeek.SLMN)=[Forms]![frFWDATE]![Text0]) AND ((QryFW.PDATE)=[FDATE]+4)) OR (((QryFW.SLMN2)=[Forms]![frFWDATE]![Text0]) AND ((QryFW.PDATE)=[FDATE]+4)) OR (((QryFW.SLMN3)=[Forms]![frFWDATE]![Text0]) AND ((QryFW.PDATE)=[FDATE]+4))

any feedback is appreciated!

4

1 に答える 1

2

データ構造が問題を引き起こしました。私の提案は、Customers テーブルから営業担当者を削除し (関連する営業担当者よりも顧客ごとのデータが多いと仮定して)、それらを Salesmen_Customers などと呼ばれる新しいテーブルに追加することです。このテーブルには、セールスマンと顧客の間の多対多の関係が含まれます

これが現在の構造です

古いデータ構造

そしてこれが新しい構造です

新しいデータ構造

これにより、クエリ構造が大幅に簡素化されます。これは、販売スタッフが 3 人を超えた場合にも簡単に拡張できます。データベース設計のトピックについてさらに読むことに興味がある場合は、SQL アンチ パターンをお勧めします。あなたが本当にあなたのDB構造と結婚しているなら、あなたの方法を照会することができますが、私はそれをお勧めしません.

于 2012-04-17T13:04:53.433 に答える