Sql 在原始查询中输入ILines,然后执行以下操作: WHERE ( i.pg = '40' OR i.<ILinesKeyField> IS NULL ) 其中(i.pg='40' 或i.为空 )

Sql 在原始查询中输入ILines,然后执行以下操作: WHERE ( i.pg = '40' OR i.<ILinesKeyField> IS NULL ) 其中(i.pg='40' 或i.为空 ),sql,tsql,null,Sql,Tsql,Null,这样可以确保即使dbo.ILines.Pg中有一个NULL,并且NULL字段不是由于外部联接造成的,不需要的行仍然会被过滤。以下是代码,只需要从产品表和ILines中检查Pg ALTER PROCEDURE [dbo].[MyPareto32TEST] @pgParam varchar(255) AS SELECT i.pg, dbo.OldParetoAnalysis.Pareto, i.keycode, i.sales6months, a.LostSales6

这样可以确保即使dbo.ILines.Pg中有一个NULL,并且NULL字段不是由于外部联接造成的,不需要的行仍然会被过滤。

以下是代码,只需要从产品表和ILines中检查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


感谢您的回复,尝试将datetime添加到ON Join中,因为datetime是ilines的一部分,但它返回一行nullswont这仍然排除了尚未销售的部分吗?由于datetime导致ilines表出现问题,如果datetime源于ilines,则为是;我疯了,我尝试了这里的大多数答案,甚至没有改变,甚至没有说它错了,只是什么都没有发生,数据正是sameI根本没有想到的;现在我需要看一些样本数据。您能否发布ilines和product中一些没有显示值的行,以及您希望看到给定输入的内容?我能想到的另一件事是,当SUM(dboiLines.qty)发生时,SUM on NULL为NULL,因此整行为NULL。。。也许我们需要把它合并成0。。。天哪>。。NM您的选择需要从产品中提取。。。这就是问题所在。@Steven Smith好吧,有很多东西错了,我想1)如果你想看产品,需要从产品表中选择。2) null上的sum可能会出现问题,因此在遇到此问题时,我使用coalese替换0而不是null。我将group by改为group on product keycode,而不是ilines.prart。现在结果应该在那里了……这生成了多模块结果,尝试将其添加到我的主查询中,但问题仍然存在:(谢谢,尽管它不起作用:(0个结果出现,因为我添加了part=bk939,只是为了看看它是否会出现奇怪的结果,不是吗?开放式查询可以拉取数据,但当连接到pareto表时,它会再次完全相同…..非常非常奇怪OK,我想可能是where子句,在开放式查询中,出现了part,总和为0,但pg为NULL,因此当我筛选o时n请求pg=40的连接没有通过。唯一的问题是,如果我检查并允许null,它会抛出大量的其他null…如何绕过这个HMMM疯狂但值得一试,你能把它放到主查询中吗?我真的不想把事情搞砸,以前从未使用过临时表。我的意思是,我发布的存储过程不起作用,错误更新的回答:注意,如果group by/select包含part表中的一个元素,那么group by/select会有帮助,否则正确的连接不会有什么作用。更新了我的代码,almost那里!!!你也需要对pareto表使用
OUTER JOIN
JOIN。我会重新发布答案where子句如何工作,如何从打开的查询调用ilines keyfeild??要使用第二个版本,你需要在opequery语句中包装的查询的ilines表中添加一个不能为NULL的字段。我建议键,但任何NOTNULL字段都可以。我不知道Pg字段是否允许NULL。如果允许,则上面的第一个where子句将在Pg中接受NULL行,其中有一行。第二个只允许Pg为40的行,或者在ILINE中不允许任何匹配。
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'
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
dbo.ilines.part = 'BK939'
RIGHT JOIN
   dbo.product  
ON
   dbo.product.keycode = dbo.ilines.part                 
   and dbo.ilines.prefix = 'i'
   and dbo.ilines.part = 'BK939'
where  
   ([datetime] > dateadd(month, -6, getdate()))
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
union
SELECT
    dbo.iLines.Part,
    dbo.iLines.Pg,
    0 as sales6months,
    dbo.iLines.Prefix       
FROM Autopart.dbo.iLines
RIGHT JOIN dbo.product  
ON dbo.product.keycode = dbo.ilines.part
where  prefix = 'i'
and max([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
SELECT dbo.product.keycode, 
  dbo.iLines.Pg, 
  SUM(dbo.iLines.Qty) as sales6months, 
  dbo.iLines.Prefix        
FROM  
  dbo.product
  LEFT OUTER JOIN Autopart.dbo.iLines ON dbo.product.keycode = dbo.ilines.part                  
SELECT 
    dbo.product.keycode AS part,
    dbo.iLines.Pg,
    SUM(dbo.iLines.Qty) as sales6months,
    dbo.iLines.Prefix       
FROM 
   dbo.product 
LEFT JOIN dbo.ilines  
    ON dbo.product.keycode = dbo.ilines.part
    AND dbo.ilines.prefix = 'i'
    and dbo.ilines.part = 'BK939'                 

where
    ([datetime] > dateadd(month, -6, getdate()))

group by 
   dbo.ilines.pg,
   dbo.product.keycode,
   dbo.ilines.prefix

order by sales6months desc
LEFT JOIN dbo.ilines  
        ON dbo.product.keycode = dbo.ilines.part
        AND dbo.ilines.prefix = 'i'
        and dbo.ilines.part = 'BK939' 
        AND ([datetime] > dateadd(month, -6, getdate()))
LEFT JOIN
dbo.OldParetoAnalysis
on
i.keycode collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
FULL OUTER JOIN
dbo.OldParetoAnalysis
on
i.keycode collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
RIGHT JOIN
dbo.OldParetoAnalysis
on
i.keycode collate SQL_Latin1_General_CP1_CI_AS = dbo.OldParetoAnalysis.Part
CREATE TABLE #tmp 
(
    Keycode VARCHAR(100),
    Pg VARCHAR(100),
    sales6months INT,
    Prefix VARCHAR(100)
)
INSERT INTO #tmp(Keycode,Pg,sales6months,Prefix)
SELECT
    i.Keycode,
    i.pg,
    i.sales6months
    i.Prefix,
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
SELECT
    *
FROM
    #tmp AS 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'
DROP TABLE #tmp
ALTER PROCEDURE [dbo].[MyPareto]
@pgParam varchar(255)
AS

CREATE TABLE #tmp
(
    Keycode VARCHAR(100),--The type I don't know
    Pg VARCHAR(100),--The type I don't know
    sales6months INT,--The type I don't know
    Prefix VARCHAR(100)--The type I don't know
)
INSERT INTO #tmp(Keycode,Pg,sales6months,Prefix)
SELECT
    i.Keycode,
    i.pg,
    i.sales6months,
    i.Prefix
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

SELECT
   i.pg,
   dbo.OldParetoAnalysis.Pareto,
   i.part,
   i.sales6months,
   a.LostSales6Months,
   dbo.NewParetoAnalysis.Pareto

FROM
#tmp 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 #tmp 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

DROP TABLE #tmp
SELECT dbo.product.keycode as PART,
   dbo.iLines.Pg,
   SUM(coalesce(dbo.iLines.Qty,0)) as sales6months,
   dbo.iLines.Prefix       
FROM 
   Autopart.dbo.iLines
RIGHT JOIN
   dbo.product  
ON
   dbo.product.keycode = dbo.ilines.part                 

where  (prefix = 'i' or prefix is null)
   and (([datetime] > dateadd(month, -6, getdate())) OR [datetime] is null)
   and (dbo.ilines.part = 'BK939' or dbo.ilines.part is null)

group by 
   dbo.ilines.pg,
   dbo.product.keycode,
   dbo.ilines.prefix

order by sales6months desc
WHERE i.pg = '40'
WHERE ISNULL(i.pg, '40') = '40'
WHERE ( i.pg = '40'
        OR i.<ILinesKeyField> IS NULL
      )
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