如何使用可能或可能出现在外键列中的整数替换任何NULL值

时间:2013-12-02 03:42:42

标签: sql sql-server-2008

当我运行脚本时,来自5个维度表的5个外键(如ResourceKey,AusFunctionKey,IntFunctionKey,RegionViewKey和EntityKey)始终具有值。因为我将每个维度表中的5个主键数据类型设置为INT。但是,当我将所有维度链接到事实表时,存在具有NULL值的“一些”外键。

如何将一个case语句或我可以添加的任何内容用一个整数替换任何NULL值,例如“1”?

SELECT d.resourcekey, 
       CONVERT(INT, CONVERT(VARCHAR(8), Dateadd(dd, -Datepart(dw, Getdate()), 
                                        Getdate() 
                                        ), 112)) AS TimeKey, 
       A.ausfunctionkey, 
       I.intfunctionkey, 
       o.regionviewkey, 
       E.entitykey                               AS EmployingEntityKey, 
       r.resourceid, 
       r.subsubfunctioncode, 
       r.sublocationcode, 
       r.site, 
       t.sitedesc, 
       Sum(t.itemvalue1)                         AS HoursOutstanding, 
       Sum(t.itemvalue1 * Isnull(CASE 
                                   WHEN t.itemvalue2 = 0 THEN r.costrateregoper 
                                   ELSE r.costrateintoper 
                                 END, 0))        AS OperCostOutstanding, 
       Sum(t.itemvalue1 * Isnull(CASE 
                                   WHEN t.itemvalue2 = 0 THEN 
                                   r.chargerateregoper 
                                   ELSE r.chargerateintoper 
                                 END, 0))        AS OperRevenueOutstanding, 
   Sum(t.itemvalue1 * Isnull(CASE 
                               WHEN t.itemvalue2 = 0 THEN r.costratereghome 
                               ELSE r.costrateinthome 
                             END, 0))        AS HomeCostOutstanding, 
       Sum(t.itemvalue1 * Isnull(CASE 
                                   WHEN t.itemvalue2 = 0 THEN 
                                   r.chargeratereghome 
                                   ELSE r.chargerateinthome 
                                 END, 0))        AS HomeRevenueOutstanding, 
       r.homecurrcode, 
       Sum(t.itemvalue1 * Isnull(CASE 
                                   WHEN t.itemvalue2 = 0 THEN r.costrateregnat 
                                   ELSE r.costrateintnat 
                                 END, 0))        AS NatCostOutstanding, 
       Sum(t.itemvalue1 * Isnull(CASE 
                                   WHEN t.itemvalue2 = 0 THEN r.chargerateregnat 
                                   ELSE r.chargerateintnat 
                                 END, 0))        AS NatRevenueOutstanding, 
       r.natcurrcode 
FROM   ods_currentresource r 
       LEFT OUTER JOIN ods_staff_task t 
                    ON R.resourceid = T.resourceid 
       LEFT OUTER JOIN dwresourcetask.dbo.dimresource D 
                    ON T.resourceid = D.resourceid 
       LEFT OUTER JOIN dwresourcetask.dbo.dimaustralianfunction A 
                    ON R.subsubfunctioncode = A.subsubfunctioncode 
       LEFT OUTER JOIN dwresourcetask.dbo.diminternationalfunction I 
                    ON R.subsubfunctioncode = I.subsubfunctioncode 
       LEFT OUTER JOIN dwresourcetask.dbo.dimregion o 
                    ON R.sublocationcode = o.sublocationcode 
       LEFT OUTER JOIN dwresourcetask.dbo.dimentity E 
                    ON R.legalentity = E.entcode 
WHERE  r.resourcetypecode <> 'C' 
       AND t.sitedesc = r.resourcesite 
       AND t.tasktypeid IN ( 5, 6 ) --missing time 
GROUP  BY d.resourcekey, 
          A.ausfunctionkey, 
          I.intfunctionkey, 
          o.regionviewkey, 
          E.entitykey, 
          r.resourceid, 
          r.subsubfunctioncode, 
          r.sublocationcode, 
          r.site, 
          t.sitedesc, 
          t.resourceid, 
          r.homecurrcode, 
          r.natcurrcode 

2 个答案:

答案 0 :(得分:0)

CASE
    WHEN [myRow] IS NULL THEN 1234
END

或(正如其他人所说)使用ISNULL 例如:

SELECT Description, ISNULL(MaxQty, 0.00) AS 'Max Quantity' FROM Sales.SpecialOffer;

答案 1 :(得分:0)

查询时可以使用ISNULL(A.ausfunctionkey, 1) as ausfunctionkey。这将为您提供适当的值,如果为NULL,则输出为1