sql查询可能的嵌套选择
问题描述:
我的老板今天有一个愿景,即完全改变我几乎完整的程序的设计。 因此,这里是我现在使用的显示产品组中的零件号码的查询,有pareto的位置(pareto是一个联盟,例如1-100,最畅销的零件)。sql查询可能的嵌套选择
我老板的愿望是用于帕雷托历史的额外列。
这里是当前的代码:
ALTER PROCEDURE [dbo].[MyParetoConfirmed]
@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
i.sales6months Desc
我需要的是一个新的表连接称为paretoMain,这个拥有帕累托peroid历史:
领域是:PG,部分,帕累托,PareoidID,日期。
目前有20K部件具有独特的peroid ID 1-6,我需要得到该perper的pareto和pg,就像我的代码已经做的那样(只是没有得到perpid),我需要做6次for每个ID为1-6。 我认为这可能是multipule select语句或嵌套,但说实话,我不能说我的头多大的查询将是多少,需要。
对此的帮助将会很棒!
非常感谢!
澄清每个选择将从新表中获得pareto。每个选择将得到帕雷托和别名,如pareto1,pareto2 .....等。
因此,查询需要选择和命名6次BU仍然使用我现在有的查询
答
好的解决办法是将表本身改为单独的表。只是通过意见加入他们。