1

使用 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。但是应该逐行匹配的新帕累托列显示空白,这表明并非所有行都被显示(可能与空值有关)。

我希望它与我的连接有关,因为您可以看到它可能是一个过于复杂的查询,但它可以满足我的需要,问题是时间,它很匆忙,因为我有 24 小时来完成这个查询而没有事先计划(是的我的脸也是手掌)。

哦,帕累托只是分配给最畅销部分的数字,比如联赛。

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

非常感谢

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

我很困惑,有人可以告诉我现在如何更新我的新旧帕累托表吗?因为有很多连接

4

1 回答 1

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,我将如何更新旧帕累托表和新帕累托表?

于 2012-04-04T08:57:38.493 回答