Im using SQL Server manager 2008.
I have a query as follows:
SELECT dbo.iLines.Part,
dbo.iLines.Pg,
SUM(dbo.iLines.Qty) as sales6months,
dbo.iLines.Prefix
FROM
Autopart.dbo.iLines
RIGHT JOIN
dbo.product
ON
dbo.product.keycode = dbo.ilines.part
where prefix = 'i'
and ([datetime] > dateadd(month, -6, getdate()))
and dbo.ilines.part = 'BK939'
group by
dbo.ilines.pg,
dbo.ilines.part,
dbo.ilines.prefix
order by sales6months desc
So to explain, I want to get the last 6month sales on all the products in the product table.
The problem is the fact that some products wont have any sales. I still need them to show.
So what I’m asking is to show the last 6month sales on ALL products including 0 sales.
“iLines” is the table causing the problem since the part number only exists in there if there has been a sale.
I know there may be a way doing multiple query’s etc. But I need this all in 1 query ONLY.
I tried letting null through but does nothing plus its really scary using nulls hehe.
Any code snippet to add to this query would be super awesome!!
Many thanks!
Also if you need any more info just ask!
UPDATE!
Yes sorry Datetime only exists in Ilines.
Ilines = table of sales
Product = just a table of all our products
Heres the main query, its meant to pull the top “n” sold parts in the last 6 months.
It works except like I said it doesn’t show parts that have not sold.
ALTER PROCEDURE [dbo].[MyPareto]
@pgParam varchar(255)
AS
SELECT
i.pg,
dbo.OldParetoAnalysis.Pareto,
i.part,
i.sales6months,
a.LostSales6Months,
dbo.NewParetoAnalysis.Pareto
FROM
OPENQUERY(SACBAUTO, 'SELECT dbo.iLines.Part,
dbo.iLines.Pg,
SUM(dbo.iLines.Qty) as sales6months,
dbo.iLines.Prefix
FROM Autopart.dbo.iLines
where prefix = ''i''
and [datetime] > dateadd(month, -6, getdate())
group by
dbo.ilines.pg,
dbo.ilines.part,
dbo.ilines.prefix
order by sales6months desc') i
RIGHT JOIN
dbo.OldParetoAnalysis
on
i.part collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
INNER JOIN
dbo.NewParetoAnalysis
ON
dbo.OldParetoAnalysis.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.NewParetoAnalysis.Part
LEFT JOIN
OPENQUERY(SACBAUTO, 'SELECT dbo.aLines.Part,
dbo.aLines.Pg,
SUM(dbo.aLines.Qty) as LostSales6Months,
dbo.aLines.Prefix
FROM Autopart.dbo.aLines
where prefix = ''d''
and [datetime] > dateadd(month, -6, getdate())
group by
dbo.alines.pg,
dbo.alines.part,
dbo.alines.prefix
order by LostSales6Months desc') a
ON
dbo.NewParetoAnalysis.Part collate SQL_Latin1_General_CP1_CI_AS = a.part
WHERE
i.pg = @pgParam
GROUP BY
i.pg,
dbo.OldParetoAnalysis.Pareto,
i.part,
i.sales6months,
a.LostSales6Months,
dbo.NewParetoAnalysis.Pareto
ORDER BY
dbo.OldParetoAnalysis.Pareto asc
think of the pareto as a league of the best parts.
Hopefully this will help, I tried to avoid adding it as it may put some people off commenting.
Ok new update, this open query works!!!
SELECT
dbo.product.Keycode,
dbo.iLines.Pg,
SUM(COALESCE(dbo.iLines.Qty, 0)) as sales6months,
dbo.iLines.Prefix
FROM
Autopart.dbo.product
LEFT OUTER JOIN
Autopart.dbo.ilines
ON
dbo.product.keycode = dbo.ilines.part
AND ([datetime] > dateadd(month, -6, getdate()) OR [datetime] is null )
WHERE
(dbo.iLines.Prefix = 'i' OR dbo.iLines.Prefix is null)
AND dbo.product.keycode = 'BK939'
group by
dbo.ilines.pg,
dbo.product.keycode,
dbo.ilines.prefix
order by sales6months desc#
BUT when i then join with my pareto table as follows:
SELECT
i.pg,
dbo.OldParetoAnalysis.Pareto,
i.keycode,
i.sales6months
FROM
OPENQUERY(SACBAUTO, 'SELECT
dbo.product.Keycode,
dbo.iLines.Pg,
SUM(COALESCE(dbo.iLines.Qty, 0)) as sales6months,
dbo.iLines.Prefix
FROM
Autopart.dbo.product
LEFT OUTER JOIN
Autopart.dbo.ilines
ON
dbo.product.keycode = dbo.ilines.part
AND ([datetime] > dateadd(month, -6, getdate()) OR [datetime] is null )/* must be this*/
WHERE
(dbo.iLines.Prefix = ''i'' OR dbo.iLines.Prefix is null)
group by
dbo.ilines.pg,
dbo.product.keycode,
dbo.ilines.prefix
order by sales6months desc') i
LEFT JOIN
dbo.OldParetoAnalysis
on
i.keycode collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
WHERE i.pg = '40' AND i.keycode = 'BK939'
The results go back the same, So this means the problem is when i go to join, BUT old pareto does contain the part number, and ive also tried changing the joins….Im hoping this has narrowed down the search for the problem and im Hoping some one has an idea why this is happening!
FINAL UPDATE!
Wow this is looong, but finally i figured out the problem, I had to recheck using the product tables PG field!!!!! since it wont be null!! here’s the code!
ALTER PROCEDURE [dbo].[MyPareto32TEST]
@pgParam varchar(255)
AS
SELECT
i.pg,
dbo.OldParetoAnalysis.Pareto,
i.keycode,
i.sales6months,
a.LostSales6Months,
dbo.NewParetoAnalysis.Pareto
FROM
OPENQUERY(SACBAUTO, 'SELECT
dbo.product.Keycode,
dbo.iLines.Pg,
dbo.product.pg as ppg,
SUM(COALESCE(dbo.iLines.Qty, 0)) as sales6months,
dbo.iLines.Prefix
FROM
Autopart.dbo.product
LEFT OUTER JOIN
Autopart.dbo.ilines
ON
dbo.product.keycode = dbo.ilines.part
AND ([datetime] > dateadd(month, -6, getdate()) OR [datetime] is null )
WHERE
(dbo.iLines.Prefix = ''i'' OR dbo.iLines.Prefix is null)
group by
dbo.ilines.pg,
dbo.product.keycode,
dbo.ilines.prefix,
dbo.product.pg
order by sales6months desc') i
RIGHT JOIN
dbo.OldParetoAnalysis
on
i.keycode collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
AND (i.pg = @pgParam or (i.pg is null AND i.ppg = @pgParam))
INNER JOIN
dbo.NewParetoAnalysis
ON
dbo.OldParetoAnalysis.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.NewParetoAnalysis.Part
LEFT JOIN
OPENQUERY(SACBAUTO, 'SELECT
dbo.product.Keycode,
dbo.aLines.Pg,
SUM(COALESCE(dbo.aLines.Qty, 0)) as lostsales6months,
dbo.aLines.Prefix
FROM
Autopart.dbo.product
LEFT OUTER JOIN
Autopart.dbo.alines
ON
dbo.product.keycode = dbo.alines.part
AND ([datetime] > dateadd(month, -6, getdate()) OR [datetime] is null )
WHERE
(dbo.aLines.Prefix = ''d'' OR dbo.aLines.Prefix is null)
group by
dbo.alines.pg,
dbo.product.keycode,
dbo.alines.prefix
order by lostsales6months desc') a
ON
dbo.NewParetoAnalysis.Part collate SQL_Latin1_General_CP1_CI_AS = a.keycode
WHERE(i.pg = @pgParam or (i.pg is null AND i.ppg = @pgParam) AND dbo.NewParetoAnalysis.Pareto is not null)
GROUP BY
i.pg,
dbo.OldParetoAnalysis.Pareto,
i.keycode,
i.sales6months,
a.LostSales6Months,
dbo.NewParetoAnalysis.Pareto
ORDER BY
dbo.OldParetoAnalysis.Pareto asc
Heres the code, just needed to check for PG from the product table and the ilines!!