1

「customer_requests」というテーブルがあります。

[CustomerRequestID] 
[Title] 
[Description] 
[RFQDate] 
[Q2CDate] 
[QuantityRequested] 
[GST] 
[NetCost] 
[Markup] 
[NetSellPrice] 
[GrossSellPrice] 
[fkCustomerID] 
[Status]

そして、「ジョブ」と呼ばれるテーブル:

[JobID] 
[JobTitle] 
[Description] 
[fkCustomerRequestID] 
[fkSupplierID] 
[fkSupplierQuoteID] 
[Quantity] 
[Cost] 
[Status] 
[ETA] 
[LoggedBy] 
[DeliveryAddress] 
[ParentJobID]

両方のテーブルからすべてのエントリを選択し、次のテーブルに表示する選択クエリを作成したいと考えています。

[QuoteNumber] (refers to CustomerRequestID in customer_requests, and fkCustomerRequestID in job)

[JobNumber] (refers to JobID in job, and is blank if the entry is from customer_requests)

[CustomerName] (selected using fkCustomerID in customer_requests, selected using fkCustomerRequestID->fkCustomerID in job)

[SupplierName] (selected using fkSupplierID in job, blank if entry is from customer_requests)

[JobTitle] (refers to title in customer_requests, and JobTitle in job)

[Quantity] (refers to QuantityRequested in customer_requests, and Quantity in Job)

[Cost]  (refers to GrossSellPrice in customer_requests and Cost in Job)

[ETA] (refers to ETA in Job, and blank if the entry is from customer_requests)

[Status] (refers to Status in customer_requests, and Status in Job)

この 2 つを結合してこのテーブルを作成するにはどうすればよいでしょうか?

4

1 に答える 1

2

テーブルのサプライヤーと顧客も持っていると仮定しています:

INSERT INTO NEWTABLENAME
SELECT j.fkCustomerRequestID,
    j.JobID,
    c.CustomerName,
    s.supplierName,
    j.JobTitle,
    j.Quantity,
    j.Cost,
    j.ETA,
    j.STATUS
FROM Jobs j
INNER JOIN supplier s ON j.fkSupplierID = s.SupplierID
INNER JOIN customer c ON j.fkCustomerRequestID = c.customerID

編集:

両方のテーブルからすべての情報が必要な場合は、上記のクエリを実行してから、次のクエリで UINON ALL を実行できます。

SELECT cr.fkCustomerRequestID,
    "",
    c.CustomerName,
    "",
    cr.title,
    cr.QuantityRequested,
    cr.GrossSellPrice,
    "",
    cr.STATUS
FROM Jobs customer_requests
INNER JOIN customer c ON cr.fkCustomerID = c.customerID
于 2013-09-19T23:48:16.500 に答える