문제

Another Question for Today :)

Wrote a query that works perfectly - except I want it to show NULL/0 values as 0 - and - that isn't happening. I tried approaching this two ways:

First I used isnull()

Select isnull(Count(*),0) as Total ,
       z.zname
From STable   s   ,
     SLTable  sl  ,
     ZTable   z   ,
     SETable  se  , 
     SEETable see ,
     SEGTable seg
Where s.sID     = sl.sID
  and sl.zID    = z.zID
  and s.sID     = se.sID
  and se.etID   = see.etID
  and see.segID = seg.segID
  and see.segID = 3
Group By z.zname
order by z.zname

Is doesn't seem to give me the Null/0 values

Then I tried using a sum/case approach

Select sum(case when see.segID <> 3 then 0 else 1 end) as Total ,
       z.zname
From STable    s   ,
     SLTable   sl  ,
      Table    z   ,
      SETable  se  ,
      SEETable see ,
      SEGTable seg
Where s.sID     = sl.sID
  and sl.zID    = z.zID
  and s.sID     = se.sID
  and se.etID   = see.etID
  and see.segID = seg.segID
  and see.segID = 3
Group By z.zname
order by z.zname

And still no 0 values - so now I'm stumped :(

도움이 되었습니까?

해결책

Well, it's unclear what you're actually trying to find. However, a big part of your problem is that you're using old-school, pre-ISO/ANSI join syntax. If you refactor your join to use modern join syntax, you'll get a query that looks somethinn (a lot, actually) like this:

select zName = z.zname  , 
       Total = count(*)
From ZTable   z   
join SLTable  sl  on sl.zID    = z.zID
join STable   s   on s.sID     = sl.sID
join SETable  se  on se.sID    = s.sID
join SEETable see on see.etID  = se.etID
                 and see.segID = 3
join SEGTable seg on seg.segID = see.segID
Group By z.zname
order by z.zname

I suspect that what you want to get is a list of all zNames and their respect counts of having segID = 3. Since you are using inner joins, you'll only ever see zNames that have a match. What you can do is something like this:

select zName = z.zname  , 
       Total = sum(case see.segID when 3 then 1 else 0 end)
from      ZTable   z   
left join SLTable  sl  on sl.zID   = z.zID
left join STable   s   on s.sID    = sl.sID
left join SETable  se  on se.sID   = s.sID
left join SEETable see on see.etID = se.etID
group By z.zname
order by z.zname

The above will return every row from zTable at least once, with null values for the columns of any table for which no match was found. Then we group it and count the rows where segID is 3.

다른 팁

Actually just to semi answer my own question - just realized that where - see.segID-3 - so it'll only turn out results with segID = 3 - so it cannot NOT have 3 - right?

But Im specifically looking for segIDs as 3 - just, if there is nothing there then display 0

Here's what I am assuming you need changed

Select 
Count(*) as Total,
z.zname
From STable s, SLTable sl, ZTable z, SETable se, SEETable see, SEGTable seg
Where s.sID=sl.sID
and sl.zID=z.zID
and s.sID=se.sID
and se.etID=see.etID
and see.segID=seg.segID
and see.segID=3
AND TABLE.YOURCOLUMN IS NOT NULL       -- THESE ARE 
AND TABLE.YOURCOLUMN <> 0              -- NEW
GROUP BY z.zname
ORDER BY z.zname

In your Query isnull(Count(*),0) as Total,

Count will never return null, it will be numeric values ranging from 0 to n.

So your isnull condition will never get satisfy.

You can simply write select count() as total .. instead of isnull(Count(),0) and it will return 0 when ever it will find no rows in table.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top