SQL查询需要显示空值

lem*_*unk 3 sql t-sql null

我使用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
Run Code Online (Sandbox Code Playgroud)

因此,为了解释,我希望在产品表中的所有产品上获得最后6个月的销售额.

问题在于某些产品没有任何销售.我仍然需要它们来展示.所以我要问的是显示所有产品的最近6个月的销售额,包括0个销售额.

"iLines"是导致问题的表格,因为如果有销售,零件编号只存在于那里.

我知道可能有一种方法可以进行多个查询等.但我只需要1个查询就可以了.

我试过让null通过,但没有做任何加上它真的可怕使用空值呵呵.

要添加到此查询的任何代码段都非常棒!

非常感谢!

如果您还需要更多信息,请询问!

UPDATE!对不起日期只存在于Ilines.

Ilines =销售表产品=只是我们所有产品的表格

这是主要的问题,它意味着在过去的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
Run Code Online (Sandbox Code Playgroud)

把帕累托想象成最好的部分联盟.希望这会有所帮助,我试图避免添加它,因为它可能会让一些人不予评论.

好的新更新,这个打开的查询工作!

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#
Run Code Online (Sandbox Code Playgroud)

但是当我加入我的帕累托表时如下:

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'
Run Code Online (Sandbox Code Playgroud)

结果返回相同,所以这意味着问题是当我去加入时,但是老帕累托确实包含了部件号,并且我也尝试更改连接....我希望这已经缩小了对问题的搜索范围我希望有人知道为什么会这样!

最后更新!哇这是looong,但最后我想出了问题,我不得不重新检查产品表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
Run Code Online (Sandbox Code Playgroud)

And*_*mar 5

以下条件筛选出right join失败的任何行:

dbo.ilines.part = 'BK939'
Run Code Online (Sandbox Code Playgroud)

要修复它,请将条件移动到连接:

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()))
Run Code Online (Sandbox Code Playgroud)

假设[datetime]是一列product,您可以将其留在where子句中.