Solved

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

Posted on 2011-03-02
6
220 Views
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)

0
Comment
Question by:Mr_Shaw
6 Comments
 
LVL 4

Assisted Solution

by:MarioAlcaide
MarioAlcaide earned 25 total points
Comment Utility
The second option looks better to me
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 190 total points
Comment Utility
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 1
UNION ALL SELECT NULL
UNION ALL SELECT NULL

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.
0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 95 total points
Comment Utility
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

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 95 total points
Comment Utility
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
 
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 95 total points
Comment Utility
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.
0
 

Author Closing Comment

by:Mr_Shaw
Comment Utility
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now