如何组合有时没有值的文本列

时间:2016-12-22 15:58:54

标签: sql group-by

我试图合并有时没有价值的列文本数据,但有时会这样做。

SELECT RegWOSEGSmry.kserialnum, 
       RegWOSEGSmry.kmodel, 
       RegWOSEGSmry.kmfg, 
       RegWOSEGSmry.kcustnum, 
       RegWOSEGSmry.kbranch, 
       RegWOSEGSmry.kequipnum, 
       RegWOSEGSmry.kworkorder, 
       RegWOSEGSmry.oeshipname, 
       RegWOSEGSmry.oetypeord, 
       RegWOSEGSmry.SourceFlag, 
       RegWOSEGSmry.ardate, 
       RegWOSEGSmry.arinvno, 
       RegWOSEGSmry.aryear, 
       RegWOSEGSmry.CreationDate, 
       RegWOSEGSmry.custpcl, 
       RegWOSEGSmry.custsnum, 
       RegWOSEGSmry.FlatRateAll, 
       RegWOSEGSmry.CxOpenDate, 
       RegWOSEGSmry.DistributeFlatRates, 
       RegWOSEGSmry.FlatRateLabor,
       RegWOSEGSmry.FlatRateMisc,
       RegWOSEGSmry.FlatRateParts,

       MAX (RegWOSEGSmry.oeitemlong) as Oeitemlong,

       SUM (RegWOSEGSmry.FlatRateAllAmt), 
       SUM (RegWOSEGSmry.FlatRateLaborAmt), 
       SUM (RegWOSEGSmry.FlatRateMiscAmt),
       SUM (RegWOSEGSmry.FlatRatePartsAmt),
       SUM (RegWOSEGSmry.HardwareCost), 
       SUM (RegWOSEGSmry.HardwareStreetPrice), 
       SUM (RegWOSEGSmry.HardwareTMRPrice), 
       SUM (RegWOSEGSmry.LaborCost), 
       SUM (RegWOSEGSmry.LaborStreetPrice), 
       SUM (RegWOSEGSmry.LaborTMRPrice),
       SUM (RegWOSEGSmry.MiscCost),
       SUM (RegWOSEGSmry.MiscStreetPrice), 
       SUM (RegWOSEGSmry.MiscTMRPrice), 
       SUM (RegWOSEGSmry.PartsCost), 
       SUM (RegWOSEGSmry.PartsStreetPrice), 
       SUM (RegWOSEGSmry.PartsTMRPrice), 
       SUM (RegWOSEGSmry.TaxCost), 
       SUM (RegWOSEGSmry.TaxStreetPrice),
       SUM (RegWOSEGSmry.TaxTMRPrice),
       SUM (RegWOSEGSmry.LaborHours), 

       AVG (RegWOSEGSmry.artotal)

FROM WieseData.dbo.RegWOSEGSmry RegWOSEGSmry

WHERE (RegWOSEGSmry.kcustnum='165453')

GROUP BY RegWOSEGSmry.kserialnum, 
         RegWOSEGSmry.kmodel, 
         RegWOSEGSmry.kmfg, 
         RegWOSEGSmry.kcustnum, 
         RegWOSEGSmry.kbranch, 
         RegWOSEGSmry.kequipnum, 
         RegWOSEGSmry.kworkorder, 
         RegWOSEGSmry.oeshipname, 
         RegWOSEGSmry.oetypeord, 
         RegWOSEGSmry.SourceFlag, 
         RegWOSEGSmry.ardate, 
         RegWOSEGSmry.arinvno, 
         RegWOSEGSmry.aryear, 
         RegWOSEGSmry.CreationDate, 
         RegWOSEGSmry.custpcl, 
         RegWOSEGSmry.custsnum, 
         RegWOSEGSmry.FlatRateAll, 
         RegWOSEGSmry.CxOpenDate, 
         RegWOSEGSmry.DistributeFlatRates, 
         RegWOSEGSmry.FlatRateLabor,
         RegWOSEGSmry.FlatRateMisc,
         RegWOSEGSmry.FlatRateParts

产生这个:
image

我需要拥有" Kserialnum"," Kmodel"和" Kequipmnum"结合起来,但有时如图所示," Group By"没有价值。

3 个答案:

答案 0 :(得分:2)

以某种方式连接在一起?除非您在任何空输入上字符串连接返回null的平台上,否则空值不是问题。即SELECT 'A' + NULL + 'C'将返回NULL。在这种情况下:

COALESCE(RegWOSEGSmry.kserialnum, '')
+ COALESCE(RegWOSEGSmry.kmodel, '')
+ COALESCE(RegWOSEGSmry.kequipnum, '')

这是为SQL Server连接它们的有效方法,或者将+替换为||,将COALESCE替换为NVL for Oracle。这可以在select和group by子句中使用。

答案 1 :(得分:0)

FROM WieseData.dbo.RegWOSEGSmry RegWOSEGSmry

WHERE (RegWOSEGSmry.kcustnum='165453')
and ( "Kserialnum" is not null
and  "Kmodel" is not null
and "Kequipmnum" is not null)

看起来您只希望数据不为空。只需在where子句后添加上述条件。如果您正在寻找其他内容,请告诉我。

答案 2 :(得分:0)

在MySQL中,您可以使用IFNULL函数,使用IFNULL(columnName, defaultValue)将NULL值替换为默认值。

由于您在三个列中获取NULL值,因此请在这些列上应用该函数。考虑默认值123,您的查询将变为以下内容:

SELECT IFNULL(RegWOSEGSmry.kserialnum, 123), 
   IFNULL(RegWOSEGSmry.kmodel, 123), 
   RegWOSEGSmry.kmfg, 
   RegWOSEGSmry.kcustnum, 
   RegWOSEGSmry.kbranch, 
   IFNULL(RegWOSEGSmry.kequipnum, 123), 
   RegWOSEGSmry.kworkorder, 
   RegWOSEGSmry.oeshipname, 
   RegWOSEGSmry.oetypeord, 
   RegWOSEGSmry.SourceFlag, 
   RegWOSEGSmry.ardate, 
   RegWOSEGSmry.arinvno, 
   RegWOSEGSmry.aryear, 
   RegWOSEGSmry.CreationDate, 
   RegWOSEGSmry.custpcl, 
   RegWOSEGSmry.custsnum, 
   RegWOSEGSmry.FlatRateAll, 
   RegWOSEGSmry.CxOpenDate, 
   RegWOSEGSmry.DistributeFlatRates, 
   RegWOSEGSmry.FlatRateLabor,
   RegWOSEGSmry.FlatRateMisc,
   RegWOSEGSmry.FlatRateParts,

   MAX (RegWOSEGSmry.oeitemlong) as Oeitemlong,

   SUM (RegWOSEGSmry.FlatRateAllAmt), 
   SUM (RegWOSEGSmry.FlatRateLaborAmt), 
   SUM (RegWOSEGSmry.FlatRateMiscAmt),
   SUM (RegWOSEGSmry.FlatRatePartsAmt),
   SUM (RegWOSEGSmry.HardwareCost), 
   SUM (RegWOSEGSmry.HardwareStreetPrice), 
   SUM (RegWOSEGSmry.HardwareTMRPrice), 
   SUM (RegWOSEGSmry.LaborCost), 
   SUM (RegWOSEGSmry.LaborStreetPrice), 
   SUM (RegWOSEGSmry.LaborTMRPrice),
   SUM (RegWOSEGSmry.MiscCost),
   SUM (RegWOSEGSmry.MiscStreetPrice), 
   SUM (RegWOSEGSmry.MiscTMRPrice), 
   SUM (RegWOSEGSmry.PartsCost), 
   SUM (RegWOSEGSmry.PartsStreetPrice), 
   SUM (RegWOSEGSmry.PartsTMRPrice), 
   SUM (RegWOSEGSmry.TaxCost), 
   SUM (RegWOSEGSmry.TaxStreetPrice),
   SUM (RegWOSEGSmry.TaxTMRPrice),
   SUM (RegWOSEGSmry.LaborHours), 

   AVG (RegWOSEGSmry.artotal)

FROM WieseData.dbo.RegWOSEGSmry RegWOSEGSmry

WHERE (RegWOSEGSmry.kcustnum='165453')

GROUP BY IFNULL(RegWOSEGSmry.kserialnum, 123), 
     IFNULL(RegWOSEGSmry.kmodel, 123), 
     RegWOSEGSmry.kmfg, 
     RegWOSEGSmry.kcustnum, 
     RegWOSEGSmry.kbranch, 
     IFNULL(RegWOSEGSmry.kequipnum, 123), 
     RegWOSEGSmry.kworkorder, 
     RegWOSEGSmry.oeshipname, 
     RegWOSEGSmry.oetypeord, 
     RegWOSEGSmry.SourceFlag, 
     RegWOSEGSmry.ardate, 
     RegWOSEGSmry.arinvno, 
     RegWOSEGSmry.aryear, 
     RegWOSEGSmry.CreationDate, 
     RegWOSEGSmry.custpcl, 
     RegWOSEGSmry.custsnum, 
     RegWOSEGSmry.FlatRateAll, 
     RegWOSEGSmry.CxOpenDate, 
     RegWOSEGSmry.DistributeFlatRates, 
     RegWOSEGSmry.FlatRateLabor,
     RegWOSEGSmry.FlatRateMisc,
     RegWOSEGSmry.FlatRateParts

现在,只要列包含 NULL ,就会返回 123