0

如何列出不是 (null = 0) 的产品?

斯塔尔表

id        STHAR_GCKOD        STHAR_GCMIK
------------------------------------------------
1             NULL               NULL
99             G                  2
99             C                  2
1525           G                  3
1525           C                  2
------------------------------------------------

NOTE: G = (+)  /  C = (-)

结果

ID 1;     Not found in result, i want to found in result (TOTAL : 0)
ID 99;    TOTAL : 0
ID 1525;  TOTAL : 1

我的代码:

SELECT 
   B.STOK_KODU, B.STOK_ADI, B.URETICI_KODU, B.KOD_1,
   SUM(CASE A.STHAR_GCKOD 
         WHEN 'G' THEN STHAR_GCMIK 
         ELSE - STHAR_GCMIK 
       END) BAKIYE
FROM 
    LSTHAR A
RIGHT OUTER JOIN 
    TBLSTSABIT B ON B.STOK_KODU=A.STOK_KODU
GROUP BY 
    B.STOK_KODU, B.STOK_ADI, B.URETICI_KODU, B.KOD_1
HAVING
    SUM(CASE A.STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK 
                           ELSE - STHAR_GCMIK END) < B.KOD_6
ORDER BY 
    B.STOK_KODU ASC

找不到结果列表为空或空数据,

让我想要的结果列表

可能的 ?

谢谢 ..

4

1 回答 1

1

试试这个

SELECT 
   B.STOK_KODU, B.STOK_ADI, B.URETICI_KODU, B.KOD_1,
   SUM(CASE A.STHAR_GCKOD 
         WHEN 'G' THEN STHAR_GCMIK 
         ELSE ISNULL(- STHAR_GCMIK,0) END) 
       END) BAKIYE
FROM 
    LSTHAR A
RIGHT OUTER JOIN 
    TBLSTSABIT B ON B.STOK_KODU=A.STOK_KODU
GROUP BY 
    B.STOK_KODU, B.STOK_ADI, B.URETICI_KODU, B.KOD_1
HAVING
    SUM(CASE A.STHAR_GCKOD WHEN 'G' THEN STHAR_GCMIK 
                           ELSE ISNULL(- STHAR_GCMIK,0) END) < B.KOD_6
ORDER BY 
    B.STOK_KODU ASC

编辑: 不知道你为什么收到错误信息。但是,在原始查询中未获取 NULL 值的问题是下面的 CASE 状态

CASE A.STHAR_GCKOD 
         WHEN 'G' THEN STHAR_GCMIK 
         ELSE - STHAR_GCMIK 
       END

你也可以试试这个,更清楚

CASE WHEN A.STHAR_GCKOD = 'G' THEN  STHAR_GCMIK 
     WHEN A.STHAR_GCKOD = 'C' THEN - STHAR_GCMIK 
     WHEN A.STHAR_GCKOD IS NULL THEN 0
END 
于 2013-04-30T14:47:44.363 回答