0% found this document useful (0 votes)
32 views1 page

SQL Last Year Query

The JSON contains SQL queries that select data from tables to summarize sales and receipts quantities over a 3 year period by item, calculating values for the current and previous years.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views1 page

SQL Last Year Query

The JSON contains SQL queries that select data from tables to summarize sales and receipts quantities over a 3 year period by item, calculating values for the current and previous years.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

--SELECT * FROM TABLE WHERE CreatedDate >= DATEADD(MONTH,-3,getdate())

--WHERE CreatedDate >= CAST(GETDATE() - 90 AS DATE)


--AND CreatedDate <= CAST(GETDATE() AS DATE);

SELECT distinct sum(dbo.Phar_SalesDetail1.quantity)-


SUM(ISNULL(dbo.Phar_SalesDetail1.qtyreturned,0)) as qty--,Year(GetDate()) as
CurrentYear,
--Year(DateAdd(Year,-3,GetDate())) as PreviousYear
FROM dbo.Phar_SalesHdr INNER JOIN
dbo.Phar_SalesDetail1 ON dbo.Phar_SalesHdr.salesid =
dbo.Phar_SalesDetail1.slshdrid
where --month(dbo.Phar_SalesHdr.billdate)=@Bln and
year(dbo.Phar_SalesHdr.billdate)=@Thn)
dbo.Phar_SalesHdr.billdate >= DateAdd(Year,-3,GetDate()) and
dbo.Phar_SalesHdr.billdate <= DateAdd(Year,0,GetDate())
and dbo.Phar_SalesDetail1.itemid='PTN00047LAP'

---------------------------------------------

DECLARE @yr as int


DECLARE @yr1 as int
set @yr = Year(GetDate())-1
set @yr1 = Year(GetDate())-3
SELECT distinct sum(dbo.Phar_SalesDetail1.quantity)-
SUM(ISNULL(dbo.Phar_SalesDetail1.qtyreturned,0)) as qty,@yr1 as gg,
Year(GetDate())-1 as CurrentYear
--Year(DateAdd(Year,-3,GetDate())) as PreviousYear
FROM dbo.Phar_SalesHdr INNER JOIN
dbo.Phar_SalesDetail1 ON dbo.Phar_SalesHdr.salesid =
dbo.Phar_SalesDetail1.slshdrid
where --month(dbo.Phar_SalesHdr.billdate)=@Bln and
year(dbo.Phar_SalesHdr.billdate)=@Thn)
--YEAR(dbo.Phar_SalesHdr.billdate(GetDate())-1)

YEAR(dbo.Phar_SalesHdr.billdate) >= @yr1 and


YEAR(dbo.Phar_SalesHdr.billdate) <= @yr --2018 AND YEAR(dbo.Phar_SalesHdr.billdate)
< 2021 --->= DateAdd(Year,-3,GetDate())
and dbo.Phar_SalesDetail1.itemid='PTN00047LAP'

-----------------------------------------------------

DECLARE @yr as int


DECLARE @yr1 as int
set @yr = Year(GetDate())-1
set @yr1 = Year(GetDate())-3

SELECT sum(dbo.MM_ReceivingDet.QtyRecv) - sum(dbo.MM_ReceivingDet.QtyReturn) AS


qty, @yr1 as gg,
Year(GetDate())-1 as CurrentYear

FROM dbo.MM_Receivings INNER JOIN


dbo.MM_ReceivingDet ON dbo.MM_Receivings.RecvID =
dbo.MM_ReceivingDet.RecvID
WHERE YEAR(dbo.MM_Receivings.DlvDate) >= @yr1 and YEAR(dbo.MM_Receivings.DlvDate)
<= @yr
AND dbo.MM_ReceivingDet.ItemID = 'VIT00026SAN'

You might also like