Should I include the same function which I have used in the select in the group by?

Posted on 2011-03-02
Last Modified: 2012-05-11
Should I include the same function which I have used in the select in the group by?

Select Coalesce(CountryID,1), Count(1)
From Tbl1
Group BY CountryID

Or should I script it as

Select Coalesce(CountryID,1), Count(1)
From Tbl1
Group BY (CountryID,1)

Question by:Mr_Shaw
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

Assisted Solution

MarioAlcaide earned 25 total points
ID: 35016110
The second option looks better to me
LVL 22

Accepted Solution

Thomasian earned 190 total points
ID: 35016124
Actually, they could return different results so it depends on what you need.

Here's an example
DECLARE @tbl1 table (CountryID int)
INSERT @tbl1

SELECT coalesce(CountryId,1) id, COUNT(1) [count]
from @tbl1
group by countryid
id	count
1	2
1	1

SELECT coalesce(CountryId,1) id, COUNT(1) [count]
from @tbl1
group by coalesce(CountryId,1)
id	count
1	3

Open in new window

Note that in the first query, there are 2 records with id=1, while it was combined on the second query.
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 95 total points
ID: 35016129
Yes second option
Select Coalesce(CountryID,1), Count(1)
From Tbl1
Group BY COALESCE(CountryID,1)

By using this any records with NULL value for CountryID will be given a 1 and the data will be grouped on this 1 as opposed to trying to group by NULL

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

LVL 50

Assisted Solution

Lowfatspread earned 95 total points
ID: 35016240
yes your group by should contain the coalesce...

but it does depend as thomasian demonstrates what your really trying to achieve...

Select coalesce(country,1) as country,thecount
from (
select country,count(*) as thecount
from tbl1
group by country
) as x
order by 1
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 95 total points
ID: 35027756
yes you should

the coalesce takes care of you null values.
AGG function diregard Null values.

if you dont use the Function then the results will be worng.

Author Closing Comment

ID: 35034853

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

726 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