use RMSmasterDB
select a.ItemLookupCode, a.Description, a.price AS CurrentPrice, CONVERT(DATE,a.DateCreated) AS DateCreated, CONVERT(DATE,a.LastReceived) AS LastRcvd, CONVERT(DATE,a.LastSold) AS LastSold, a.Quantity, QtyRcvd.QtyRcvd, QtySold.QtySold,QtySold.TotalProfit,SoldQ1.SoldQ1, SoldQ2.SoldQ2, SoldQ3.SoldQ3, soldq4.SoldQ4, soldq5.SoldQ5,
AVG(SoldQ1.SoldQ1 + SoldQ2.SoldQ2 + SoldQ3.SoldQ3 + soldq4.SoldQ4 + soldq5.SoldQ5) AS AVGQuaters, a.ReorderPoint
from dbo.item AS A
left join (
select b.ItemID, SUM(b.Quantity) AS QtySold, SUM(b.price * b.quantity) AS TotalProfit
from dbo.TransactionEntry AS B
group by b.ItemID
) AS QtySold on Qtysold.ItemID = a.ID
left join (
select c.ItemID, SUM(c.QuantityReceivedToDate) AS QtyRcvd
from dbo.PurchaseOrderEntry AS C
group by c.ItemID
) AS QtyRcvd on QtyRcvd.itemid = a.ID
left join (
select d.ItemID, ISNULL(SUM(d.quantity),0) AS SoldQ1
from dbo.TransactionEntry AS D
where d.TransactionTime >= dateadd(day,datediff(day,92,GETDATE()),0)
and
d.TransactionTime <= dateadd(day,datediff(day,0,GETDATE()),0)
group by d.ItemID
) SoldQ1 on SoldQ1.ItemID = a.ID
left join (
select e.ItemID, ISNULL(SUM(e.quantity),0) AS SoldQ2
from dbo.TransactionEntry AS e
where e.TransactionTime >= dateadd(day,datediff(day,184,GETDATE()),0)
and
e.TransactionTime <= dateadd(day,datediff(day,93,GETDATE()),0)
group by e.ItemID
) SoldQ2 on SoldQ2.ItemID = a.ID
left join (
select f.ItemID, ISNULL(SUM(f.quantity),0) AS SoldQ3
from dbo.TransactionEntry AS f
where f.TransactionTime >= dateadd(day,datediff(day,276,GETDATE()),0)
and
f.TransactionTime <= dateadd(day,datediff(day,185,GETDATE()),0)
group by f.ItemID
) SoldQ3 on SoldQ3.ItemID = a.ID
left join (
select g.ItemID, ISNULL(SUM(g.quantity),0) AS SoldQ4
from dbo.TransactionEntry AS g
where g.TransactionTime >= dateadd(day,datediff(day,368,GETDATE()),0)
and
g.TransactionTime <= dateadd(day,datediff(day,277,GETDATE()),0)
group by g.ItemID
) SoldQ4 on SoldQ4.ItemID = a.ID
left join (
select H.ItemID, ISNULL(SUM(H.quantity),0) AS SoldQ5
from dbo.TransactionEntry AS H
where H.TransactionTime >= dateadd(day,datediff(day,460,GETDATE()),0)
and
H.TransactionTime <= dateadd(day,datediff(day,369,GETDATE()),0)
group by H.ItemID
) SoldQ5 on SoldQ5.ItemID = a.ID
left join dbo.Department AS Q on q.id = a.DepartmentID
where q.code = '05'
group by a.ItemLookupCode, a.Description, a.price, a.DateCreated, a.LastReceived, a.LastSold, a.Quantity, QtyRcvd.QtyRcvd, QtySold.QtySold,SoldQ1.SoldQ1, SoldQ2.SoldQ2, SoldQ3.SoldQ3, soldq4.SoldQ4, soldq5.SoldQ5, QtySold.TotalProfit, a.ReorderPoint
order by QtySold.TotalProfit DESC
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE