0
我有多個產品銷往全國各地。我將如何看到每個州的最暢銷產品是什麼?T-SQL選擇每個州最暢銷的產品
CREATE TABLE Customers (
CustomerID Int Not
, Name Varchar(75)
, UserName Varchar(75)
, Password Varchar(75)
, Email Varchar(75)
, PrimaryPhone Varchar(50)
, Primary Key (CustomerID)
)
CREATE TABLE ShippingAddress (
AddressID Int Primary Key
, CustomerID Int
, Street (75)
, State Varchar (75)
, Zip Varchar (75)
,Foreign Key (CustomerID) REFERENCES Customers
)
CREATE TABLE PaymentInfo (
CreditCardNumber Int Primary Key
, CardExpDate Date
, CVVCode Int
, CustomerID Int
, AddressID Int
, Foreign Key (CustomerID) REFERENCES Customers
, Foreign Key (AddressID) REFERENCES Addresses
)
CREATE TABLE Orders (
OrderID Int Primary Key
, OrderDate Date
, CustomerID Int
, TotalPrice Int
, Foreign Key (CustomerID) REFERENCES Customers
)
CREATE TABLE Shipping (
ShippingID Int Primary Key
, OrderID Int
, AddressID Int
, Foreign Key (OrderID) REFERENCES Orders
, Foreign Key (AddressID) REFERENCES Addresses
)
CREATE TABLE OrderPayment (
OrderID Int
, CreditCardNumber Int
, Primary Key (OrderID, CreditCardNumber)
, Foreign Key (OrderID) REFERENCES Orders
, Foreign Key (CreditCardNumber) REFERENCES PaymentInfo
)
CREATE TABLE Inventory (
ItemModelNumber Int Primary Key
, Brand Int
, ItemDescription Text
, StockQuantity Int
)
CREATE TABLE OrderItems (
OrderID Int
, ItemModelNumber Int
, Primary Key (OrderID, ItemModelNumber)
, Foreign Key (OrderID) REFERENCES Orders
, Foreign Key (ItemModelNumber) REFERENCES Inventory
)
CREATE TABLE Sources (
DistributorID Int Primary Key
, DistributorName Varchar(75)
)
CREATE TABLE InventorySource (
ItemModelNumber Int
, DistributorID Int
, RequestedDate Date
, DeliveredDate Date
, RequestedQuantity Int
, Primary key (ItemModelNumber, DistributorID)
, Foreign Key (ItemModelNumber) REFERENCES Inventory
, Foreign Key (DistributorID) REFERENCES Sources
)
GO
CREATE VIEW BestSellers AS(
所以我知道我想從ShippingAddress中選擇DISTINCT狀態。然後,我想我需要從OrderItems表中計數(ItemModelNumber),OrderID,並將這些列與ShippingAddress表中的Distinct State列進行連接。然後在狀態上只是一個GROUP BY聲明..到目前爲止這是否正確?
邏輯是正確的,但在哪裏查詢定義您的邏輯? – 2015-03-19 07:01:37
所有這些表都與您的查詢有關嗎?如果不是,那麼不要顯示它們,因爲它只會增加你所問問題的噪音。 – sr28 2015-03-19 11:47:36
在我看來,這裏可能有些遺漏。我在哪裏可以看到每件訂單訂購了多少件物品以及價格是多少。此外,我有點朦朧你的意思是「最暢銷」。是創造收入最多的項目,訂單最多的項目還是發貨量最大的項目? – Ralph 2015-03-20 15:38:36