我正在使用 SQL Server 管理器 2008。
我有一个查询如下:
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
所以解释一下,我想获得产品表中所有产品的最近 6 个月的销售额。
问题是某些产品不会有任何销售。我仍然需要他们展示。所以我要求的是显示所有产品最近 6 个月的销售额,包括 0 销售额。
“iLines”是导致问题的表,因为零件号仅在有销售时才存在。
我知道可能有一种方法可以进行多个查询等。但我只需要在 1 个查询中完成所有操作。
我尝试让 null 通过,但什么也没做,而且使用 null 真的很可怕,呵呵。
添加到此查询的任何代码片段都会非常棒!
非常感谢!
另外,如果您需要更多信息,请询问!
更新!是的,抱歉 Datetime 仅存在于 Ilines 中。
Ilines = 销售表 Product = 只是我们所有产品的表格
这是主要查询,它的意思是拉出过去 6 个月内销量最高的“n”个零件。除了我说它不显示未售出的零件外,它可以正常工作。
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
将帕累托视为最佳部分的联盟。希望这会有所帮助,我尽量避免添加它,因为它可能会让一些人不发表评论。
好的新更新,这个开放查询有效!!!
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#
但是当我加入我的帕累托表时,如下所示:
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'
结果返回相同,所以这意味着问题是当我去加入时,但旧的帕累托确实包含零件号,我也尝试过更改加入....我希望这缩小了对问题的搜索范围我希望有人知道为什么会这样!
最终更新!哇,这很长,但最后我发现了问题,我不得不使用产品表 PG 字段重新检查!!!!!!因为它不会为空!!这是代码!
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