SQL加入了麻烦

时间:2012-04-03 21:51:37

标签: sql sql-server sql-server-2008 join

使用Microsoft SQL Server 2008我有一个需要注意的查询。 我对为了正确执行查询所需的连接感到困惑。

USE [ShaftData]
GO
/****** Object:  StoredProcedure [dbo].[MyPareto]    Script Date: 04/03/2012 19:32:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[MyPareto]
as

SELECT i.pg,
   dbo.OldParetoAnalysis.Pareto,
   i.part,
   i.sumofqty,
   a.sumofqty,
   dbo.NewParetoAnalysis.Pareto

FROM
   OPENQUERY(SACBAUTO, 'SELECT dbo.iLines.Part,
                            dbo.iLines.Pg,
                            SUM(dbo.iLines.Qty) as sumofqty,
                            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 sumofqty desc') i
RIGHT JOIN
dbo.OldParetoAnalysis
on
i.part collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part

LEFT JOIN
   OPENQUERY(SACBAUTO, 'SELECT dbo.aLines.Part,
                            dbo.aLines.Pg,
                            SUM(dbo.aLines.Qty) as sumofqty,
                            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 sumofqty desc') a
ON
dbo.OldParetoAnalysis.Part collate SQL_Latin1_General_CP1_CI_AS = a.part
FULL JOIN
dbo.NewParetoAnalysis
ON
a.part collate SQL_Latin1_General_CP1_CI_AS = dbo.NewParetoAnalysis.Part
WHERE
i.pg = '31'
GROUP BY
   i.pg,
   dbo.OldParetoAnalysis.Pareto,
   i.part,
   i.sumofqty,
   a.sumofqty,
   dbo.NewParetoAnalysis.Pareto
ORDER BY
dbo.OldParetoAnalysis.Pareto desc

好的解释,我需要显示这些字段。目前我需要从旧帕累托显示的所有行。新帕累托是旧的帕累托用于更新的克隆(这就是计划的下一部分)。

问题在于目前我的帕累托正在显示正确的1-5780。但是应该匹配row for row的新pareto列显示空白,这表明并非显示所有行(可能与Nulls有关)。

我希望它与我的连接有关,因为你可以看到它可能是一个过于复杂的查询,但它适用于我需要的,问题是时间,它匆忙因为我有24小时完成此查询没有事先计划(是的,我也面临着责任)。

哦,帕累托只是一个分配给最畅销零件的数字,比如联盟。

如果您需要更多信息,请询问并编辑错误。

非常感谢

编辑: 好的解决了问题,答案将会更新。

我很困惑,有人能告诉我现在如何更新我的新旧帕累托表吗?因为有很多人加入

1 个答案:

答案 0 :(得分:1)

继承守则:

USE [ShaftData]
GO
/****** Object:  StoredProcedure [dbo].[MyPareto]    Script Date: 04/04/2012 08:50:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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
/*FULL OUTER JOIN
dbo.NewParetoAnalysis
ON
a.part collate SQL_Latin1_General_CP1_CI_AS = dbo.NewParetoAnalysis.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

现在我有了一个新问题。 使用c sharp,我将如何更新旧的和新的帕累托表?