You are on page 1of 2

SELECT

'AverageCost' AS Cost_Sorted_By_Production_Days ,
[0], [5], [10], [15], [20],[25],[30]
FROM
(SELECT
ReorderLevel , unitprice
FROM [Products]
WHERE ReorderLevel = 5
)
AS SourceTable
PIVOT
(
AVG(unitprice)
FOR
ReorderLevel IN ([0], [5], [10], [15], [20],[25],[30])
) AS
PivotTable ;

SELECT CompanyName,P.UnitPrice

FROM Suppliers S
inner join Products P on P.SupplierID = s.SupplierID AND P.UnitPrice between 10 and 40
--where P.UnitPrice between 10 and 40

SELECT CompanyName,P.UnitPrice FROM Suppliers S


WHERE EXISTS (SELECT ProductName FROM Products P where SupplierID = S.SupplierID AND
UnitPrice between 10 and 40 )

----Procedimiento almacenado

ALTER PROCEDURE [DBO].[SP_SHOW_PRODUCT]


(
@i_operacion int,
@i_product int = null,
@i_categoria int = null
)
AS
--Operacion de consulta productos
BEGIN

IF @i_operacion = 1
BEGIN
SELECT ProductName,C.CategoryName,ProductID,C.CategoryID
FROM Products P
INNER JOIN Categories C on C.CategoryID = P.CategoryID
WHERE ProductID = ISNULL(@i_product,ProductID)
AND P.CategoryID = ISNULL(@i_categoria,P.CategoryID)
END
END

EXEC [SP_SHOW_PRODUCT] @i_operacion = 1,@i_product = 2,@i_categoria = 1


--TABLA PIVOTE
WITH ORDENES AS (
select Country AS PAIS,datepart(year,OrderDate) as ANIO,O.OrderID AS CONTEO_ORDENES
from Customers C
INNER JOIN Orders O on O.CustomerID = C.CustomerID
--INNER JOIN [Order Details] D ON D.OrderID = O.OrderID
GROUP BY Country ,datepart(year,OrderDate),O.OrderID)

SELECT * FROM ORDENES


PIVOT
(
COUNT(CONTEO_ORDENES)
FOR ANIO IN ([1996],[1997],[1998],[1999],[2000])) AS TABLAPIVOTE

You might also like