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(er rors),0) from table2 where run =
t1.run group by run) as errors, (select isNull(count(us er),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! 4 10042
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(er rors),0) from table2 where run = t1.run group by run) as errors, (select isNull(count(us er),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(er rors,0)) from table2 where run =
t1.run group by run) as errors, (select count(isNull(us er,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.
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**********@y ahoo.co.uk> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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(er rors),0) from table2 where run = t1.run group by run) as errors, (select isNull(count(us er),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!
Dan
The above works great - thanks for the help!
Paul
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(er rors),0) from table2 where run = t1.run group by run) as errors, (select isNull(count(us er),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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 records with a given foreign-key
value, essentially:
Create table table1 {
fk_id number,
seq_id number
|
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
1/1/2004 4000.00 40.00 4040.00
1/2/2004 1000.00 10.00 1010.00
1/2/2204 2000.00 20.00 2020.00
1/3/2004 1500.00 15.00 1515.00
I want my resultant DataTable to show in DataGrid as
|
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 MEDIAN(COLUMN_NAME) type function or a
more general function like a 10% percentile function with syntax such
as
PERCENTILE(COLUMN_NAME,25).
Regards JC......
|
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 post)
*I'm running a total query, of the form
|
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 in MySQL 4.x)
Particularly, I find GROUP_CONCAT practical to quickly display 1-many
| |
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
in one-to-many relation. I don't need to see all values in B
that relate to the particular record in A, just one value in
each field in B, preferably the last entered.
This is to ease a person that need to manually fix and encode
|
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 custom aggregate functions in DB2,
written close to three years ago by Knut Stolze:
http://www-128.ibm.com/developerworks/db2/library/techarticle/0309stolze/0309stolze.html
Mr. Stolze says in the first article that "urrently, there is no
|
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
processing time. I have not tried this with different recordsets yet.
I am curious what others might think.
What might be interesting if you could create temporary indexes. I did
try sorting the recordset on find first. It ran slower. I have...
|
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.:
SELECT
A,
sum(B) as Answer1,
Answer1 * 2 as DoubleAnswer
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |