473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

IsNull and aggregate functions

Hi all

Firstly this my first time posting to technical groups - so any
mistakes I apologise for in advance.

I am trying to count records in several secondary tables for the same
run in a primary table. However, there might be no records in these
secondary tables for the specific run. Hence the sql below returns
nulls.

Select run, (select count(errors) from table2 where run = t1.run group
by run) as errors, (select count(user) as users from table3 where run =
t1.run and user = active group by run, dd)
from table1 t1

(Please note the different group bys. )

I do not want nulls to be returned but to be replaced with 0. I have
tried the isnull function but this does not work. eg

Select run, (select isNull(count(errors),0) from table2 where run =
t1.run group by run) as errors, (select isNull(count(user),0) as users
from table3 where run = t1.run and user = active group by run, user)
from table1 t1

Nor will isnull work if I put it around the select clause.

Any suggestions?

Thanks for the help!

Feb 14 '06 #1
4 10026
Paul Spratley је написао
Hi all

Firstly this my first time posting to technical groups - so any
mistakes I apologise for in advance.

I am trying to count records in several secondary tables for the same
run in a primary table. However, there might be no records in these
secondary tables for the specific run. Hence the sql below returns
nulls.

Select run, (select count(errors) from table2 where run = t1.run group
by run) as errors, (select count(user) as users from table3 where run =
t1.run and user = active group by run, dd)
from table1 t1

(Please note the different group bys. )

I do not want nulls to be returned but to be replaced with 0. I have
tried the isnull function but this does not work. eg

Select run, (select isNull(count(errors),0) from table2 where run =
t1.run group by run) as errors, (select isNull(count(user),0) as users
from table3 where run = t1.run and user = active group by run, user)
from table1 t1

Nor will isnull work if I put it around the select clause.

Any suggestions?

Thanks for the help!

hello!
try this:

Select run, (select count(isNull(errors,0)) from table2 where run =
t1.run group by run) as errors, (select count(isNull(user,0)) as users
from table3 where run = t1.run and user = active group by run, user)
from table1 t1

Feb 14 '06 #2
> Nor will isnull work if I put it around the select clause.

I would expect ISNULL or COALESCE around the scalar subquery to work. Try
the version below. Note that it's a good practice to specify table aliases
in subqueries and prefix columns accordingly in order to avoid ambiguity.
It is unclear what table(s) the 'active' column is in.

In addition to your query, it's a good practice to include DDL (CREATE TABLE
statements) and perhaps sample data (INSERTs) when posting here. Basically,
a (simplified) script that can be run in Query Analyzer. Many individuals
that answer questions here will take the time to develop and unit test a
working solution.

SELECT
t1.run,
ISNULL((SELECT
COUNT(t2.errors)
FROM table2 t2
WHERE t2.run = t1.run
GROUP BY t2.run), 0) as errors,
ISNULL((SELECT
COUNT(t3.user) AS users
FROM table3 t3
WHERE t3.run = t1.run AND t3.user = active
GROUP BY t3.run, t3.dd), 0)
FROM table1 t1

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Paul Spratley" <pa**********@yahoo.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all

Firstly this my first time posting to technical groups - so any
mistakes I apologise for in advance.

I am trying to count records in several secondary tables for the same
run in a primary table. However, there might be no records in these
secondary tables for the specific run. Hence the sql below returns
nulls.

Select run, (select count(errors) from table2 where run = t1.run group
by run) as errors, (select count(user) as users from table3 where run =
t1.run and user = active group by run, dd)
from table1 t1

(Please note the different group bys. )

I do not want nulls to be returned but to be replaced with 0. I have
tried the isnull function but this does not work. eg

Select run, (select isNull(count(errors),0) from table2 where run =
t1.run group by run) as errors, (select isNull(count(user),0) as users
from table3 where run = t1.run and user = active group by run, user)
from table1 t1

Nor will isnull work if I put it around the select clause.

Any suggestions?

Thanks for the help!

Feb 14 '06 #3
Dan
The above works great - thanks for the help!

Paul

Feb 14 '06 #4
On 14 Feb 2006 02:23:10 -0800, Paul Spratley wrote:
Hi all

Firstly this my first time posting to technical groups - so any
mistakes I apologise for in advance.

I am trying to count records in several secondary tables for the same
run in a primary table. However, there might be no records in these
secondary tables for the specific run. Hence the sql below returns
nulls.

Select run, (select count(errors) from table2 where run = t1.run group
by run) as errors, (select count(user) as users from table3 where run =
t1.run and user = active group by run, dd)
from table1 t1

(Please note the different group bys. )

I do not want nulls to be returned but to be replaced with 0. I have
tried the isnull function but this does not work. eg

Select run, (select isNull(count(errors),0) from table2 where run =
t1.run group by run) as errors, (select isNull(count(user),0) as users
from table3 where run = t1.run and user = active group by run, user)
from table1 t1

Nor will isnull work if I put it around the select clause.

Any suggestions?

Thanks for the help!


Hi Paul,

In the first subquery, I fail to see the reason for adding a GROUP BY
clause. Because of the WHERRE in the subquery, you'll never have more
than one value for table2.run anyway, so the grouping becomes a non-op.
And if no rows match, then a COUNT without GROUP BY will return 0
instead of NULL, so it solves your problem as well.

The same applies to the second subquery. Even though the GROUP BY is
different here, so is the WHERE. (I assume that "active" is a column in
the table1 table, even though you don't qualify it - if both active and
user are in table3, then this query has the risk of resulting in more
than one row, which will cause an error message!)

SELECT run,
(SELECT COUNT(errors)
FROM table2 AS t2
WHERE t2.run = t1.run),
(SELECT COUNT(user)
FROM table3 AS t3
WHERE t3.run = t1.run
AND t3.user = t1.active)
FROM table1 AS t1

(untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
Feb 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ariel Jakobovits | last post by:
I have a table with 2 primary keys, one is a foreign key, the other is produced by a sequence. I want to SELECT query for one record that has a list of the sequence-produced values for all...
1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
2
by: jc | last post by:
Hi. Just as we have AVG(COLUMN_NAME) and MAX(COLUMN_NAME) how can I write my own variation of a such a function. If I can appreciate how to do this, then I hopefully I can write a...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
5
by: David Garamond | last post by:
What do people think of adding some more aggregate functions. These are the ones that MySQL has and PG doesn't: - STD/STDDEV - VARIANCE - BIT_OR - BIT_AND - GROUP_CONCAT (for strings, added...
3
by: S P Arif Sahari Wibowo | last post by:
Hi! I would like to make an editable continous form, where most fields will be from table A and editable, except 1-3 fields are a glimpse into table B and uneditable. Table A relate to table B...
8
by: jefftyzzer | last post by:
The current issue of "Oracle Magazine" has an article on creating custom aggregate functions, which naturally got me thinking about how to do this in DB2. I found some articles on creating...
5
by: Dean | last post by:
Has anyone toiled with creating/using alternate domain aggregate functions? I have been messing with that a little. The one recordsource I have been working indicates I get 20 to 40% savings in...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.