Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help

Author  Topic 

dinosaurhead
Starting Member

10 Posts

Posted - 2011-04-15 : 10:37:40
Hello

I have to create a query to show details of customers who signed up in the same month regardless of year, as the customer who has spent the most money.

The tables I think will be needed are as follows:

CREATE TABLE CUSTOMER
(CUSTOMERID NUMBER(5) PRIMARY KEY NOT NULL,
FNAME VARCHAR2(20),
LNAME VARCHAR2(20),
ADDRESS1 VARCHAR2(50),
TOWNID NUMBER(4),
DATEJOIN DATE);

CREATE TABLE ORDERS2CUSTOMER
(ORDERS2CUSTOMERID NUMBER(5) PRIMARY KEY NOT NULL,
CUSTOMERID NUMBER(5),
ORDERID NUMBER(2));

CREATE TABLE ORDERS
(ORDERSID NUMBER(5) PRIMARY KEY NOT NULL,
DATEOFORDER DATE,
ORDERCOMPLETE VARCHAR2(1));

CREATE TABLE GARMENT2ORDER
(GARMENT2ORDERID NUMBER(5) PRIMARY KEY NOT NULL,
GARMENTID NUMBER(5),
ORDERID NUMBER(2));

CREATE TABLE GARMENT
(GARMENTID NUMBER(5) PRIMARY KEY NOT NULL,
CATALOGUE VARCHAR2(20),
TYPE VARCHAR2(20),
COST NUMBER(6, 2));


So I know I need to sum the cost values of the garments depending on how many times they are used in the garment2orders table. This then needs to link from the orders table to the orders2customer table where the customer who has spent the most (the most sum from the cost of the garments bought) will then have their details shown. All customers who also joined in the same month as the aforementioned customer will also have their details shown.

Any help or hints would be very helpful

NeilG
Aged Yak Warrior

530 Posts

Posted - 2011-04-15 : 10:53:47
Odd design, but happen this will help you get started

SELECT
C.CustomerID,
G.SUM(COST)
FROM
CUSTOMER C
INNER JOIN ORDERS2CUSTOMERS O2C
ON (C.CustomerID = O2C.CustomerID)
INNER JOIN ORDERS O
ON (O2C.OrderID = O.OrderID)
INNER JOIN GARMENT2ORDER G2O
ON (O.OrderID = G2O.OrderID
INNER JOIN GARMENT G
ON (G2O.GarmentID = G.GarmentID)
WHERE
C.MONTH(DATEJOIN) = [RequiredMonth]
GROUP BY
C.CustomerID
Go to Top of Page

dinosaurhead
Starting Member

10 Posts

Posted - 2011-04-15 : 10:56:51
heh yeh I guessed it would be, only just started learning it really and kinda dived straight in without studying properly :P

cheers tho
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-04-17 : 02:01:26
quote:
Originally posted by NeilG

Odd design, but happen this will help you get started

SELECT
C.CustomerID,
G.SUM(COST)
FROM
CUSTOMER C
INNER JOIN ORDERS2CUSTOMERS O2C
ON (C.CustomerID = O2C.CustomerID)
INNER JOIN ORDERS O
ON (O2C.OrderID = O.OrderID)
INNER JOIN GARMENT2ORDER G2O
ON (O.OrderID = G2O.OrderID
INNER JOIN GARMENT G
ON (G2O.GarmentID = G.GarmentID)
WHERE
C.MONTH(DATEJOIN) = [RequiredMonth]
GROUP BY
C.CustomerID




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-04-17 : 02:06:55
also if you've an index on DATEJOIN then you may use condition like below

WHERE DATEJOIN >= {start date of month}
AND DATEJOIN <{start date of month} +1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-04-17 : 18:44:43
>> The tables I think will be needed are as follows: <<

Why are you posting Oracle on a SQL Server website? Why so many null-able columns? Why not follow ISO-11179 naming rules? Why do you identify what looks like the same entity with different keys? The NOT NULL constraint goes after the data type, not after the PRIMARY KEY.

It looks like “order_complete” is a flag! And think about how silly VARCHAR(1) is!
A SQL programmer would have used a completion data and an assemble language flag.

Garments are identified by an EAN/UPC code; why would ever invent your own? How can an order_nbr change size from table to table? Why is there no DRI in this mess? You cannot be moew vague than “type” or “cost”

Those silly “x2y” table are how network database programmers mimic links. I think that was the convention used by IDMS programmers before we had RDBMS. There is a magical “garment2orderid”; that was a pointer into a link table in the old days. You have mimicked everything in CODASYL. And this thing has no REFERENCES to either Orders or Garments. You might not know that the reason for singular names for tables in CODASYL was that they used record-at-a-time processing; RDBMS uses sets and therefore uses collective or plural names.

I gather that you did no research and have no idea any industry standards. Replace (street_address VARCHAR(35) NOT NULL, town_id DECIMAL(4) NOT NULL) with a SAN (standard address number).

In short, your schema is useless. Let's do it right


CREATE TABLE Customers
(customer_id DECIMAL(5) NOT NULL PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
san CHAR(10) NOT NULL
REFERENCES Standard_Address_Numbers (san),
membership_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
..);

CREATE TABLE Orders
(order_nbr CHAR(10) NOT NULL PRIMARY KEY,
order_date DATE NOT NULL,
completion_DATE, --- null is still in progress
..);

CREATE TABLE Order_Details
(order_nbr CHAR(10) NOT NULL
REFERENCES Orders (order_nbr)
customer_id DECIMAL(5) NOT NULL
REFERENCES Customers (customer_id),
garment_ean CHAR(13) NOT NULL
REFERENCES Garments(garment_ean),
order_qty INTEGER DEFAULT 0 NOT NULL
CHECK (order_qty >= 0),
..);

Weird that you sell only garments, but okay.

CREATE TABLE Garments
(garment_ean CHAR(13) NOT NULL PRIMARY KEY,
garment_name VARCHAR(20) NOT NULL,
garment_type VARCHAR(20) NOT NULL,
unit_cost DECIMAL(6,2) DEFAULT 0.00 NOT NULL);

>> so I know I need to sum the cost of the garments depending on how many times they are used in the garment2orders table. <<

Unh? The cost of a garment has nothing to do with how many times it is ordered. I think you meant you want the sum of the sales by garment:

SELECT G.garment_ean, G.garment_name,
SUM (D.order_qty * G.unit_cost) AS garment_cost_tot
FROM Garments AS G, Order_Details AS D
WHERE G.garment_ean = D.garment_ean;

>> this then needs to link [sic] from the Orders table to the orders2customer table where the customers who has spent the most (the most sum from the cost of the garments bought) will then have their details shown. <<

LINK!? That is right out of the CODASYL network database model and does not appear in RDBMS. An order references a customer. Here is another difference: an SQL programer would not describe a step-by-step navigation like you did.

This is not clear, but I think you want the the biggest spender for each garment. First find what each customer spend in total on each garment in a CTE

WITH CustomerGarmentPurchases
(SELECT O.customer_id, G.garment_ean,
SUM (D.order_qty * G.unit_cost) AS garment_cost_tot
FROM Garments AS G, Order_Details AS D, Orders AS O
WHERE G.garment_ean = D.garment_ean
AND O.order_id = D.order_id
GROUP BY O.customer_id, G.garment_ean)

SELECT CGP1.customer_id, CGP1.garment_ean, CGP1.garment_cost_tot
FROM CustomerGarmentPurchases AS CGP1
WHERE CGP1.garment_cost_tot
= (SELECT MAX(garment_cost_tot)
FROM CustomerGarmentPurchases AS CGP2
WHERE CGP1.garment_ean = CGP2.garment_ean);

Untested.




>>all customers who also joined in the same month ..<<

SELECT C.customer_id,
DATEPART (C.membership_date) AS membership_date_month
FROM Customers AS C;



--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

dinosaurhead
Starting Member

10 Posts

Posted - 2011-04-18 : 13:39:36
Thanks for that

I agree, we have been taught wrong as this was supposed to be a module on SQL information retrieval but seems they themselves do not know the important basics. Thanks for the feedback and for the future I shall read up on what you have suggested
Go to Top of Page
   

- Advertisement -