Adventure Works SQL Server 2014 Enquiry - sql

I am writing a SQL query using the AdventureWorks 2014 database. I want to show which employee has sold for the highest order value.
I tried to write each select statement by itself (see below), but I'd like to be able to combine both queries into one:
select
s.SalesOrderID, s.SalesPersonID, COUNT(sd.SalesOrderID) as count
from
Sales.SalesOrderHeader s, Sales.SalesOrderDetail sd
where
s.SalesOrderID = sd.SalesOrderID
group by
sd.SalesOrderID, s.SalesOrderID, s.SalesPersonID
order by
sd.SalesOrderID
select
sd.SalesOrderID, sd.LineTotal, count (sd.SalesOrderID) as count
from
Sales.SalesOrderDetail sd
group by
sd.SalesOrderID, sd.LineTotal
order by
sd.SalesOrderID

are you looking for something like this:
select top 1
s.SalesPersonID
,sum(sd.LineTotal ) as orderTotal
s.salesorderid
from
Sales.SalesOrderHeader s
inner join Sales.SalesOrderDetail sd
on s.SalesOrderID = sd.SalesOrerID
group by
s.SalesPersonID
s.salesorderid
order by
orderTotal desc
in SQL server you can just ask for a limited number of rows with the top function (this can give you the highest order value when sorted correctly). this can be used with a group by that adds all the line totals together that have the same values in the columns being grouped by.

This is what I would do, to get the total by each Sales Person. Order by the Sum(sd.LineTotal) descending to get the Highest Value.
select s.SalesPersonID ,COUNT(sd.SalesOrderID) as count,sum(sd.LineTotal ) as orderTotal
from Sales.SalesOrderHeader s
Inner Join Sales.SalesOrderDetail sd ON s.SalesOrderID=sd.SalesOrderID
group by s.SalesPersonID
order by 3 Desc

We appreciate your effort. However, there is a simpler way to get your answer.
SELECT
-- TOP 1 //to get highest order total
s.SalesPersonID
,COUNT(sd.SalesOrderID) as Total_Count
,sum(sd.LineTotal) as orderTotal
FROM
Sales.SalesOrderHeader s Inner Join Sales.SalesOrderDetail sd
ON s.SalesOrderID=sd.SalesOrderID
GROUP BY s.SalesPersonID
HAVING s.SalesPersonID IS NOT NULL
ORDER BY sum(sd.LineTotal) desc
What I am doing is, joining SalesOrderHeader with SalesOrderDetail through SalesOrderId and using aggregate functions to get desired result.
Order by is use to get highest value first. Top 1 is use to get only desired output.

Related

Getting max value before given date

I am pretty new to using MS SQL 2012 and I am trying to create a query that will:
Report the order id, the order date and the employee id that processed the order
report the maximum shipping cost among the orders processed by the same employee prior to that order
This is the code that I've come up with, but it returns the freight of the particular order date. Whereas I am trying to get the maximum freight from all the orders before the particular order.
select o.employeeid, o.orderid, o.orderdate, t2.maxfreight
from orders o
inner join
(
select employeeid, orderdate, max(freight) as maxfreight
from orders
group by EmployeeID, OrderDate
) t2
on o.EmployeeID = t2.EmployeeID
inner join
(
select employeeid, max(orderdate) as mostRecentOrderDate
from Orders
group by EmployeeID
) t3
on t2.EmployeeID = t3.EmployeeID
where o.freight = t2.maxfreight and t2.orderdate < t3.mostRecentOrderDate
Step one is to read the order:
select o.employeeid, o.orderid, o.orderdate
from orders o
where o.orderid = #ParticularOrder;
That gives you everything you need to go out and get the previous orders from the same employee and join each one to the row you get from above.
select o.employeeid, o.orderid, o.orderdate, o2.freight
from orders o
join orders o2
on o2.employeeid = o.employeeid
and o2.orderdate < o.orderdate
where o.orderid = #ParticularOrder;
Now you have a whole bunch of rows with the first three values the same and the fourth is the freight cost of each previous order. So just group by the first three fields and select the maximum of the previous orders.
select o.employeeid, o.orderid, o.orderdate, max( o2.freight ) as maxfreight
from orders o
join orders o2
on o2.employeeid = o.employeeid
and o2.orderdate < o.orderdate
where o.orderid = #ParticularOrder
group by o.employeeid, o.orderid, o.orderdate;
Done. Build your query in stages and many times it will turn out to be much simpler than you at first thought.
It is unclear why you are using t3. From the question it doesn't sound like the employee's most recent order date is relevant at all, unless I am misunderstanding (which is absolutely possible).
I believe the issue lies in t2. You are grouping by orderdate, which will return the max freight for that date and employeeid, as you describe. You need to calculate a maximum total from all orders that occurred before the date that the order occurred on, for that employee, for every row you are returning.
It probably makes more sense to use a subquery for this.
SELECT o.employeeid, o.orderid, o.orderdate, m.maxfreight
FROM
orders o LEFT OUTER JOIN
(SELECT max(freight) as maxfreight
FROM orders AS f
WHERE f.orderdate <= o.orderdate AND f.employeeid = o.employeeid
) AS m
Hoping this is syntactically correct as I'm not in front of SSMS right now. I also included a left outer join as your previous query with an inner join would have excluded any rows where an employee had no previous orders (i.e. first order ever).
You can do what you want with a correlated subquery or apply. Here is one way:
select o.employeeid, o.orderid, o.orderdate, t2.maxfreight
from orders o outer apply
(select max(freight) as maxfreight
from orders o2
where o2.employeeid = o.employeid and
o2.orderdate < o.orderdate
) t2;
In SQL Server 2012+, you can also do this with a cumulative maximum:
select o.employeeid, o.orderid, o.orderdate,
max(freight) over (partition by employeeid
order by o.orderdate rows between unbounded preceding and 1 preceding
) as maxfreight
from orders o;

query with subquery with 1 result(max) for each year

I have to make a query where I show for each year wich shipper had the maximum total cost.
My query now show for each year the total cost of each shipper. So in the result i must have a list of the years, for each year the shipper and the total cost.
Thanks in advance.
select year(OrderDate), s.ShipperID, sum(freight)
from orders o
join shippers s on o.ShipVia = s.ShipperID
group by year(OrderDate),s.ShipperID
Select a.FreightYear, a,ShipperID, a.FreightValue
from
(
select year(OrderDate) FreightYear, s.ShipperID, sum(freight) FreightValue
from orders o
join shippers s on o.ShipVia = s.ShipperID
group by year(OrderDate),s.ShipperID
) a
inner join
(
select FreightYear, max(FrieghtTotal) MaxFreight
from
(
select year(OrderDate) FreightYear, s.ShipperID, sum(freight) FreightTotal
from orders o
join shippers s on o.ShipVia = s.ShipperID
group by year(OrderDate),s.ShipperID
) x
group by FreightYear
) max on max.FreightYear = a.FreightYear and max.MaxFreight = a.FreightValue
order by FreightYear
Inner query a is your original query, getting the value of freight by shipper.
Inner query max gets the max value for each year, and then query max is joined to query a, restricting the rows in a to be those with a value for a year = to the max value for the year.
Cheers -
It's marginally shorter if you use windowing functions.
select shippers_ranked.OrderYear as OrderYear,
shippers_ranked.ShipperId as ShipperId,
shippers_ranked.TotalFreight as TotalFreight
from
(
select shippers_freight.*, row_number() over (partition by shippers_freight.OrderYear order by shippers_freight.TotalFreight desc) as Ranking
from
(
select year(OrderDate) as OrderYear,
s.ShipperID as ShipperId,
sum(freight) as TotalFreight
from orders o
inner join shippers s on o.ShipVia = s.ShipperID
group by year(OrderDate), s.ShipperID
) shippers_freight
) shippers_ranked
where shippers_ranked.Ranking = 1
order by shippers_ranked.OrderYear
;
You need to decide what you would like to happen if two shippers have the same TotalFreight for a year - as the code above stands you will get one row (non-deterministically). If you would like one row, I would add ShipperId to the order by in the over() clause so that you always get the same row. If in the same TotalFreight case you would like multiple rows returned, use dense_rank() rather than row_number().

SQL List total orders in descending order

SELECT Customer.CID, Count(Order.OrderID) AS [# Orders]
FROM Customer INNER JOIN [Order] ON Customer.CID = Order.CID
Group By Customer.CID DESC;
Im trying to list the total number of orders by customer in descending order. I get a syntax error on the group by clause. Im not sure what the syntax error is.
You're missing your ORDER BY clause. Your query should look like:
SELECT Customer.CID, Count(Order.OrderID) AS [# Orders]
FROM Customer INNER JOIN [Order] ON Customer.CID = Order.CID
GROUP BY Customer.CID
ORDER BY Customer.CID DESC;
You should use order by instead of group by.
So your new query will be,
SELECT Customer.CID, Count(Order.OrderID) AS [# Orders]
FROM Customer INNER JOIN [Order] ON Customer.CID = Order.CID
Order By Customer.CID DESC;
I don't have tables. so please provide your remark.

Distinct across similar records in SQL Server 2008 database

I have a SQL Server 2008 database. This database has a table called Product, Order, and OrderProduct. These three tables look like the following:
Product
-------
ID
Name
Description
Order
-----
ID
OrderDate
Status
OrderProduct
------------
OrderID
ProductID
Quantity
I am trying to identify the last three unique products a person ordered. However, I also need to include the last date on which the product was ordered. My problem is I keep getting a result set like this:
Can of Beans (10/10/2011)
Soda (10/09/2011)
Can of Beans (10/08/2011)
The second "Can of Beans" should not be there because I already showed "Can of Beans". My query looks like this:
SELECT TOP 3 DISTINCT
p.[Name],
o.[OrderDate]
FROM
[Product] p,
[Order] o
[OrderProduct] l
WHERE
l.[ProductID]=p.[ID] and
l.[OrderID]=o.[ID]
ORDER BY
o.[OrderDate] DESC
I understand that the reason DISTINCT won't work is because of the order dates are different. However, I'm not sure how to remedy this. Can somebody tell me how to fix this?
WITH cteProducts AS (
SELECT p.Name, o.OrderDate,
ROW_NUMBER() OVER(PARTITION BY p.Name ORDER BY o.OrderDate DESC) as RowNum
FROM Product p
INNER JOIN OrderProduct op
ON p.ID = op.ProductID
INNER JOIN Order o
ON op.OrderID = o.ID
)
SELECT TOP 3 Name, OrderDate
FROM cteProducts
WHERE RowNum = 1
ORDER BY OrderDate DESC;
Have you tried GROUP BY?
SELECT TOP 3
p.[Name],
max(o.[OrderDate])
FROM
[Product] p,
[Order] o
[OrderProduct] l
WHERE
l.[ProductID]=p.[ID] and
l.[OrderID]=o.[ID]
GROUP BY p.[Name]
ORDER BY
max(o.[OrderDate]) DESC
Try grouping like :
SELECT TOP 3
p.[Name],
MAX(o.[OrderDate])
FROM
[Product] p,
[Order] o
[OrderProduct] l
WHERE
l.[ProductID]=p.[ID] and
l.[OrderID]=o.[ID]
GROUP BY p.[Name]
ORDER BY
MAX(o.[OrderDate]) DESC
Use GROUP BY... it's been a while since I've used SQL Server, but the query will look something like this:
SELECT TOP 3
p.[Name],
max(o.[OrderDate]) AS MostRecentOrderDate
FROM
[Product] p,
[Order] o
[OrderProduct] l
WHERE
l.[ProductID]=p.[ID] and
l.[OrderID]=o.[ID]
GROUP BY p.[Name]
ORDER BY
MostRecentOrderDate DESC
Or to show the first time they ordered that product, choose min() instead of max()

SQL Query to find the maximum of a set of averages

This is a query based on the Northwind Database in MS SQL Server 2005.
First I have to get the average of the UnitPrice from OrderDetails table, and group it by ProductID for that particular column alone and alias it as AveragePrice.
Then I need to find the maximum(AveragePrice) which is nothing but the max of previous column, how can I do it??? This is a kind of very tricky for me its taking me ages to think on it.
select
O.CustomerID,
E.EmployeeID,
E.FirstName+space(1)+E.LastName FullName,
OD.OrderID,
OD.ProductID,
(select avg(DO.UnitPrice) from OrderDetails
DO where OD.ProductID = DO.ProductID
group by DO.ProductID) AveragePrice ,
from OrderDetails OD
join Orders O
on OD.OrderID = O.OrderID
join Customers C
on C.CustomerID = O.CustomerID
join Employees E
on E.EmployeeID = O.EmployeeID
This is not a Homework question, am learning SQL, but am really stuck at this point, please help me.
It's 2 steps: "the ungrouped maximum of the grouped averages"
You can expand this as needed which shows how to apply an aggregate on top of an aggregate
SELECT
MAX(AveragePrice) AS MaxAveragePrice
FROM
(
select
avg(UnitPrice) AS AveragePrice, ProductID
from
OrderDetails
group by
ProductID
) foo
Or with CTE
;WITH AvgStuff AS
(
select
avg(UnitPrice) AS AveragePrice
from
OrderDetails
group by
ProductID
)
SELECT
MAX(AveragePrice) AS MaxAveragePrice
FROM
AvgStuff