Solved

T-sql

Posted on 2011-09-27
3
416 Views
Last Modified: 2013-11-29
Hello:
Hello:
I really need help on solving the following problems.  I appreciate any input.
Please  refer to schema in the attached file.
Customer with the highest order amount on orders with 5 or more different items
see attached

Get Items with Qty greater than 10 per order.  Select OrderID, OrderDate, CategoryName, ItemName  and Item Qty.


Get-Customer.xls
0
Comment
Question by:gloriagalvez
  • 2
3 Comments
 
LVL 9

Expert Comment

by:edlunad
ID: 36713837
try this for the items with order qty greater than 10..

Select O.OrderID, O.OrderDate, C.CategoryName, I.ItemName, OD.Qty
from [Order] O inner join OrderDetail OD on O.OrderId=OD.OrderId
inner join Item I on OD.ItemID=I.ItemId
inner join Category C on I.CategoryID=C.CategoryID
inner join (SELECT OrderId, sum(OD.Qty) AS TQty
            FROM   OrderDetail
            GROUP BY OrderId) AS OD2 ON OD.OrderId = OD2.OrderId
where  OD2.TQty>10

Open in new window

0
 
LVL 9

Accepted Solution

by:
edlunad earned 500 total points
ID: 36713885
this will be for the highest order amount ...

Select top 1 C.CustomerName, max(O.Amount) as MaxAmount
from [Order] O inner join OrderDetail OD on O.OrderId=OD.OrderId
inner join Customer C on O.CustomerID=C.CustomerID
inner join (SELECT OrderId, COUNT(DISTINCT ItemId) AS TItems
            FROM   OrderDetail
            GROUP BY OrderId) AS OD2 ON OD.OrderId = OD2.OrderId
where  OD2.TItems>5
group by C.CustomerName
order by max(O.Amount) desc

Open in new window

0
 

Author Closing Comment

by:gloriagalvez
ID: 36714288
I test it and it worked like a charm thank you:)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

776 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