sql-servercountno-data

select count(*) showing nothing (not null value, just nothing)


When I run the following query I get nothing. Not a null value but nothingthe image is not cutoff right after (no column name) there is nothing there (the image is not cutoff right after (no column name) there is nothing there).

If I get rid of the last AND clause I do get a number (even if that number is zero) and if I run the query inside the NOT IN parentheses I get the expected results. If I substitute the query inside the the NOT IN parentheses with the results I get from that query, it will give me the correct number.

I want it to say 0 instead of nothing. COALESCE and ISNULL do not work because there is no null value returned, seems like nothing is returned.

SELECT count(*)
FROM groups g  
    INNER JOIN item_group ig ON g.groupid = ig.groupid  
    INNER JOIN item_materiel im ON ig.itemid = im.item_id  
WHERE g.groupid = 4933  
     AND im.ItemMateriel_ID NOT IN (  
                         SELECT oim.ItemMaterielID  
                         FROM groups g  
                         INNER JOIN item_group ig ON g.groupid = ig.groupid  
                         INNER JOIN orderitem oi ON ig.itemid = oi.itemid  
                         INNER JOIN OrderItemMateriel oim ON oi.OrderItemID 
                                      = oim.OrderItemID  
                         WHERE g.groupid = 4933  
                                    )  
GROUP BY g.groupid 

Solution

  • You need to remove the GROUP BY g.groupid. Since you are only selecting one groupid, it makes no sense to group over it anyway.