Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Records for two different time periods...

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-11-24 : 16:28:47
Will writes "SELECT CUS.CUS_BillName, CUS.CUS_CustomerID, Sum(INV.INV_InvoiceAmt) AS CurrentAmt, CUS.CUS_CustomerType
FROM INV INNER JOIN CUS ON INV.INV_CustomerID = CUS.CUS_CustomerID
WHERE ((((INV.INV_InvoiceDate)>='1/1/2001' And (INV.INV_InvoiceDate)<='3/31/2001') AND (CUS.CUS_CustomerType='CoPack' OR CUS.CUS_CustomerType='Institutional'))AND INV.INV_ProdFam IS NOT NULL)
GROUP BY CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType
ORDER BY CUS.CUS_CustomerType, CUS.CUS_BillName

I'm sure this isn't the fast way to write the above select statment, but it works.

I ALSO need to return the data for 1 year prior, in the same select statement. Note there may not be current data, or there won't be any data for the prior period.

CustomerType BillName CustomerID CurrentAmt PriorYear
CoPack ABC Co. 1010 1000.00 0.00
CoPack DEF Co. 1011 0.00 1000.00
CoPack GHI Co. 1012 1000.00 100.00



Any incredible ideas?

Will"

mono
Starting Member

36 Posts

Posted - 2001-11-26 : 08:46:57
An alternative.
Not tested:
 
--Showing all customers, whether they have invoices or not for the current or past year:
SELECT CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType,
SUM(INVCur.INV_InvoiceAmt) AS CurrentAmt,
SUM(INVLastYear.INV_InvoiceAmt) AS LastYearAmt
FROM CUS left JOIN INV as INVCur ON INVCur.INV_CustomerID = CUS.CUS_CustomerID
left JOIN INV as INVLastYear ON INVLastYear.INV_CustomerID = CUS.CUS_CustomerID
WHERE
(CUS.CUS_CustomerType IN ('CoPack' , 'Institutional') AND
(((INVCur.INV_ProdFam IS NOT NULL) AND
(INVCur.INV_InvoiceDate >= '1/1/2001') AND
(INVCur.INV_InvoiceDate <= '3/31/2001'))
OR
((INVLastYear.INV_ProdFam IS NOT NULL) AND
(INVLastYear.INV_InvoiceDate >= '1/1/2001') AND
(INVLastYear.INV_InvoiceDate <= '3/31/2001'))
)
GROUP BY CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType
ORDER BY CUS.CUS_CustomerType, CUS.CUS_BillName

--Showing only customers who have at least one relevant in the current or past year:
SELECT CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType,
SUM(INVCur.INV_InvoiceAmt) AS CurrentAmt,
SUM(INVLastYear.INV_InvoiceAmt) AS LastYearAmt
FROM CUS left JOIN INV as INVCur ON INVCur.INV_CustomerID = CUS.CUS_CustomerID
left JOIN INV as INVLastYear ON INVLastYear.INV_CustomerID = CUS.CUS_CustomerID
WHERE
(CUS.CUS_CustomerType IN ('CoPack' , 'Institutional') AND
(((INVCur.INV_ProdFam IS NOT NULL) AND
(INVCur.INV_InvoiceDate >= '1/1/2001') AND
(INVCur.INV_InvoiceDate <= '3/31/2001'))
OR
((INVLastYear.INV_ProdFam IS NOT NULL) AND
(INVLastYear.INV_InvoiceDate >= '1/1/2001') AND
(INVLastYear.INV_InvoiceDate <= '3/31/2001'))
) AND
--at least one of the two years has data.
((INVCurYear IS NOT NULL) OR
(INVLastYear IS NOT NULL))

GROUP BY CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType
ORDER BY CUS.CUS_CustomerType, CUS.CUS_BillName


michael

Go to Top of Page

ToddV
Posting Yak Master

218 Posts

Posted - 2001-11-26 : 09:00:51
Try this:

SELECT CUS.CUS_BillName,
CUS.CUS_CustomerID,
Sum(CASE WHEN INV.INV_InvoiceDate>='1/1/2001' And INV.INV_InvoiceDate<='3/31/2001' THEN INV.INV_InvoiceAmt ELSE 0 END) AS CurrentAmt,
Sum(CASE WHEN INV.INV_InvoiceDate>='1/1/2000' And INV.INV_InvoiceDate<='3/31/2000' THEN INV.INV_InvoiceAmt ELSE 0 END) AS PriorYear,
CUS.CUS_CustomerType
FROM INV
INNER JOIN CUS ON INV.INV_CustomerID = CUS.CUS_CustomerID
WHERE (CUS.CUS_CustomerType='CoPack' OR CUS.CUS_CustomerType='Institutional'))AND INV.INV_ProdFam IS NOT NULL)
GROUP BY CUS.CUS_BillName, CUS.CUS_CustomerID, CUS.CUS_CustomerType
ORDER BY CUS.CUS_CustomerType, CUS.CUS_BillName

Go to Top of Page
   

- Advertisement -