sql - Select the customer who has the most different categories in his orders -


the question need answer following: find customer(or customers in case of tie),who has done order cointains different categories.

these tables:

products:

create table products ( prod_id number not null , "category" number not null references categories, title varchar (40) not null, actor varchar (40) not null, price varchar (40) not null, primary key (prod_id)); 

orderlines:

create table orderlines ( orderlineid number not null, orderid number not null references orders, prod_id number not null references products, quantity number not null, orderdate varchar2 (80) not null, primary key (orderlineid,orderid)); 

customers:

create table customers ( customerid number not null, firstname varchar2 (20) not null, lastname varchar2 (20) not null, address1 varchar2 (40) not null, address2 varchar2 (40), city varchar2 (20) not null, state varchar2 (20) not null, zip varchar2 (20) not null, country varchar2 (20) not null, region varchar2 (20) not null, email varchar2 (20) not null, phone varchar2 (20) not null, creditcardtype varchar2 (20) not null, creditcard varchar2 (20) not null, creditcardexpiration varchar (10) not null, username varchar2 (20) not null, "password" varchar2 (20) not null, age number not null, income number not null, gender varchar2 (8) not null, primary key (customerid)); 

orders:

create table orders ( orderid number not null, orderdate date not null, customerid number not null references customers, netamount number not null, tax number not null, totalamount number not null, primary key (orderid)); 

try:

select * ( select c.customerid, c.firstname , c.lastname, count(distinct p."category") categories customers c inner join orders o on o.customerid =c.customerid inner join orderlines ol on o.orderid =ol.orderid  inner join products p on p.prod_id =ol.prod_id  group c.customerid, c.firstname , c.lastname order count(p."category") desc)a rownum = 1; 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -