質問

Here is how I contructed the step-by-step:

M1. create a sub-query that will return CustomerId and total invoiced for that customer

M2. A second subquery that will give a list of distinct ProductIDs (along with product SKUs) and CustomerIDs.

M3. The M1 and M2 subqueries will be joined to make association between customer totals and products (for the same CustomerId).

M4. The query M3 will be fed to the final query that will just find the top 5 products.

I'm stuck on creating the distinct ProductID and customerID because they would have to be in aggregate functions in order to make them distinct.

Attached is an image that is the erwin diagram which helps understand the system.

If you can help me with M1-M4, I will greatly appreciate it. I'm not a programmer by trade but a business analyst.

Erwin Diagram

enter image description here

--M1--

select C.CustomerId, COUNT(I.InvoiceId) TotalNumInvoices 
from Customer C
JOIN Invoice I ON (I.CustomerId = C.CustomerId)
group by C.CustomerId

--M2: Incomplete--

select P.ProductID, P.SKU, C.CustomerID
from Product P
JOIN InvoiceLine IL ON (IL.ProductId = P.ProductId)
JOIN Invoice I ON (IL.InvoiceId = I.InvoiceId)
JOIN Customer C ON (C.CustomerId = I.CustomerId)
役に立ちましたか?

解決

you can also use the DISTINCT keyword your select clause in order to get unique values. Try this for m2:

select DISTINCT p.productID, p.sku, i.customerID
from invoice i INNER JOIN invoiceLine il
ON i.invoiceID = il.invoiceID
JOIN product p 
ON il.productID = p.productID;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top