Solved

SQL question

Posted on 2013-01-12
5
180 Views
Last Modified: 2013-01-13
I have  the following tables in the attachment.  I need to find  

Which parts need to be ordered from our supplier now?


 Who ordered the most value?

Who ordered the most quantity?
sqltables.doc
0
Comment
Question by:ocdc
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38771446
1) What parts need to be ordered ?  (Assuming that stockqty is your stock on hand).

You want to find all the parts where the SOH has reached the reorder point.  Your example doesn't hold a reorder quantity column so hard to say how much you're supposed to order.

select partid, description
from Inventory
where stockqty <= reorderpnt

Open in new window


2) Who ordered the most value ?

This query gives you a sorted list of suppliers and the sum of the orders they had placed, in descending order of value and you take the first one.

select top 1 o.custid, c.cname, sum(oi.qty * i.price) as OrderValue
from Orders o
join OrderItems oi on o.orderid = oi.orderid
join Inventory i on oi.partid = i.partid
join Customers c on o.custid = c.custid
group by o.custid, c.cname
order by OrderValue desc

Open in new window


3) Who ordered the most quantity

We can take the previous query and change it up just a little.

select top 1 o.custid, c.cname, sum(oi.qty) as OrderQty
from Orders o
join OrderItems oi on o.orderid = oi.orderid
join Customers c on o.custid = c.custid
group by o.custid, c.cname
order by OrderQty desc

Open in new window


I'm sure there are other ways to do it, but I believe this comes pretty close to it (I have a test system with close to this layout of data, so I checked the basis of these queries there and it seemed to give me the correct data - and then converted the column and table names to match your specification).  

Also won't promise that it's the most efficient way of doing things, I don't do a lot of development these days - but it seems to get the job done :)
0
 

Author Comment

by:ocdc
ID: 38772765
Thanks. they worked.    what about  

Which sales people have NOT sold anything?    Subquery version

Which sales people have NOT sold anything? JOIN version (explicit/named JOIN)
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38772946
Assuming you have an Employees table (that's not in your attachment):

Salesmen with no sales (Subquery)

select empid 
from Employees
where empid not in (select empid from Orders);

Open in new window


Salesmen with no sales (JOIN)

select empid
from
(
select e.empid, max(o.salesdate) as last_sale
from employees e
left outer join orders o
on e.empid = o.empid
group by e.empid
)
where last_sale is null;

Open in new window


Alternatively, you could use distinct:

select distinct empid
from
(
select e.empid, o.salesdate as last_sale
from employees e
left outer join orders o
on e.empid = o.empid
)
where last_sale is null;

Open in new window

0
 

Author Comment

by:ocdc
ID: 38773019
Thanks.   final one is  

 Who placed the most orders?
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38773065
select top 1 custid, count(*) as num_orders
from Orders
group by custid
order by num_orders desc
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with SQL joins 9 48
Caste datetime 2 57
My Query is not giving correct result. Please help 5 40
create insert script based on records in a table 4 21
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question