Hi guys!
I've tried hard to produce the code myself but I'm struggling. I'm but a mere student of these dark arts!
I don't have access to Query Analyzer, SPs or other SQL Server 2000 functions - my only query tool is MS Query (which makes it a little difficult to write code in the SQL View box which is approx 50 chars x 10 lines visible ... I'm just glad I can use Scintilla Text Editor to write the code with, lol).
I have to produce a list of items (from our inventory-master table) for a particular range of products, and then cross reference this with our sales-order tables (detail and master/header table) in order to provide a count of sales orders by month for the last three months.
I can't think of any way other than 3 correlated subqueries, each focussing on the month for the sales order entry date... but I thought this would have quite an impact on database performance when the query is refreshed.
I have tried to use [tt]SUM(CASE...[/tt] and an [tt]LOJ InvMaster --> derived table [/tt] but, with such, I'm getting multiple instances of StockCode for those items which have sales orders in more than one of the months being queried... this I expected, but I can't turn the corner to get one instance of StockCode and then separate columns for a count of sales order numbers for each of the last three month's sales (April, May and June).
The best I can do at present is along the lines of this simple code (with highlighted section repeated for datepart(month,... ) values of 4, 5 & 6 (April, May & June).
I can post my actual code if it makes any difference, but there are other pieces of information which distract from my objective here.
Mark, HH Associates
I've tried hard to produce the code myself but I'm struggling. I'm but a mere student of these dark arts!
I don't have access to Query Analyzer, SPs or other SQL Server 2000 functions - my only query tool is MS Query (which makes it a little difficult to write code in the SQL View box which is approx 50 chars x 10 lines visible ... I'm just glad I can use Scintilla Text Editor to write the code with, lol).
I have to produce a list of items (from our inventory-master table) for a particular range of products, and then cross reference this with our sales-order tables (detail and master/header table) in order to provide a count of sales orders by month for the last three months.
I can't think of any way other than 3 correlated subqueries, each focussing on the month for the sales order entry date... but I thought this would have quite an impact on database performance when the query is refreshed.
I have tried to use [tt]SUM(CASE...[/tt] and an [tt]LOJ InvMaster --> derived table [/tt] but, with such, I'm getting multiple instances of StockCode for those items which have sales orders in more than one of the months being queried... this I expected, but I can't turn the corner to get one instance of StockCode and then separate columns for a count of sales order numbers for each of the last three month's sales (April, May and June).
The best I can do at present is along the lines of this simple code (with highlighted section repeated for datepart(month,... ) values of 4, 5 & 6 (April, May & June).
I can post my actual code if it makes any difference, but there are other pieces of information which distract from my objective here.
Code:
SELECT
StockCode,[COLOR=red yellow]
( SELECT
COUNT (DISTINCT SalesOrder)
FROM
SorDetail
INNER JOIN
SorMaster
ON
SorDetail.SalesOrder = SorMaster.SalesOrder
WHERE
SorDetail.MStockCode = InvMaster.StockCode AND
DATEPART( Month, SorMaster.EntryDate ) = 4 AND
DATEPART( Year, SorMaster.EntryDate ) = 2007
) AS 'NoOrdsApr'[/color]
FROM
InvMaster
WHERE
client = 'F'
Mark, HH Associates