Solved

Using ISNULL with GROUP BY

Posted on 2008-06-18
8
3,800 Views
Last Modified: 2008-06-25
Why does thist first bit of code return zeros when there is no data, but the second bit returns nothing?

SELECT       ''
      , COUNT([Region]) AS [Tot No of Incidents]
        , ISNULL(SUM([On Site < 2 hrs]), 0) AS [On site < 2 hrs]
        , ISNULL(SUM([Fixed < 3.75 hrs]), 0) AS [Fixed < 3.75 hrs]
        FROM tblFireLinkReportsTemp
        WHERE fldPriorityUID = 42

SELECT       Region
        , COUNT([Region]) AS [Tot No of Incidents]
        , ISNULL(SUM([On Site < 2 hrs]), 0) AS [On site < 2 hrs]
        , ISNULL(SUM([Fixed < 3.75 hrs]), 0) AS [Fixed < 3.75 hrs]
        FROM tblFireLinkReportsTemp
        WHERE fldPriorityUID = 42
        GROUP BY Region
0
Comment
Question by:looper8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21811078
are u sure there are records matching the second criteria


SELECT region,[Region])
FROM tblFireLinkReportsTemp
WHERE fldPriorityUID = 42
GROUP BY Region
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21811084
or did u forget to remove the WHERE  condition, cuz, there is logically no difference b/w the first and second
0
 
LVL 2

Expert Comment

by:howyue
ID: 21811089
SELECT '' will always give u a row even ur WHERE clause returns no result, '' is more like a hardcode value
SELECT [column] will not guarantee u a row because if ur WHERE clause returns no result, the [column] will be null
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Author Comment

by:looper8
ID: 21811292
aneeshattingal: I'm trying to code for times when there is no data, so I'm running both bits against an empty table.

howye: I thought that too, but the following still returns no results:

SELECT    ''
      ,   Region
        , COUNT([Region]) AS [Tot No of Incidents]
        , ISNULL(SUM([On Site < 2 hrs]), 0) AS [On site < 2 hrs]
        , ISNULL(SUM([Fixed < 3.75 hrs]), 0) AS [Fixed < 3.75 hrs]
        FROM tblFireLinkReportsTemp
        WHERE fldPriorityUID = 42
        GROUP BY Region
0
 
LVL 2

Expert Comment

by:howyue
ID: 21811321
the only difference in ur query is GROUP BY
with GROUP BY it giv u NULL, without GROUO BY is giv u 0.
however i'm stil tryin to figure out why is it so
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 21813352
In the first query, you are asking SQL for an overall COUNT, SUM, etc..  Even if there are no rows in the table, the COUNT() is still a *relevant* result, of 0.

In the second query, you are asking SQL for a count **by region**.  This could easily produce many rows as a result, obviously: one row **per region**.  But, if there are no regions, then there is no **relevant** result, since you explicilty asked for a COUNT, SUM, etc., **only** by region.
0
 
LVL 1

Author Comment

by:looper8
ID: 21814298
Thanks Scott.  Is there then a way I can force the second query to return results (of zero), even though there is no Region in the table?
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 21814360
Not directly really.  But could check first and if the table has no matching rows, issue a different query.  For example:

IF EXISTS(SELECT TOP 1 Region FROM tblFireLinkReportsTemp WHERE fldPriorityUID = 42)
BEGIN
-- normal query goes here
SELECT       Region
        , COUNT([Region]) AS [Tot No of Incidents]
        , ISNULL(SUM([On Site < 2 hrs]), 0) AS [On site < 2 hrs]
        , ISNULL(SUM([Fixed < 3.75 hrs]), 0) AS [Fixed < 3.75 hrs]
        FROM tblFireLinkReportsTemp
        WHERE fldPriorityUID = 42
        GROUP BY Region
END --IF
ELSE
BEGIN
-- "special table is empty query" goes here
SELECT       ''
        , 0 AS [Tot No of Incidents]
        , 0 AS [On site < 2 hrs]
        , 0) AS [Fixed < 3.75 hrs]
END --ELSE
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question