Tuesday, April 8, 2014

Lunch Time HW 4-8 As planned

Here is my lunch time project...

/*USE TSQL2012
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid
--
WITH cte AS(
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid)
SELECT p.productid, p.productname
FROM Production.Products AS p
INNER JOIN cte AS c
ON c.categoryid = p.categoryid
AND p.unitprice = c.mn
--WHERE p.unitprice = c.mn
--
SELECT s.supplierid, p.productname, p.productid, p.unitprice
FROM production.Suppliers AS s
INNER JOIN production.Products AS p
ON p.supplierid = s.supplierid
WHERE s.country LIKE 'Japan'
--
SELECT c.custid, c.companyname, o.orderdate
FROM sales.Customers AS c
RIGHT OUTER JOIN sales.Orders AS o
ON o.custid = c.custid
WHERE o.orderdate = '20070212'
--
IF OBJECT_ID('anyname','if') IS NOT NULL DROP FUNCTION anyname
CREATE FUNCTION anyname (@empid, INT)
RETURN TABLE AS();
--
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice)
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC
*/
--
WITH cte AS(
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice) AS mn
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC)
SELECT s.country, s.supplierid, A.*
FROM production.Suppliers AS s
CROSS APPLY cte AS A
WHERE s.country = 'Japan'

No comments:

Post a Comment