I want to get a column count several times in one query using different
filters but can't work out how to do it - can anyone point me in the right
direction?
For example, how would combine these two selects into one query that will
list the total and filtered actions:
SELECT COUNT(actions) as actioncount, location
FROM mytable
GROUP BY location
SELECT COUNT(actions) as actioncount, location
FROM mytable
WHERE mycondition IS NULL
GROUP BY location 6 52774
You can use subqueries like this:
select location,
actioncount1 = (select count(actions)
from mytable T1
where T1.location = mytable.location
),
actioncount2 = (select count(actions)
from mytable T2
where mycondition is null
and T2.location = mytable.location
)
from mytable
group by location
Shervin
"JackT" <tu***********@ntlworld.com> wrote in message
news:Fp***************@newsfep1-gui.server.ntli.net... I want to get a column count several times in one query using different filters but can't work out how to do it - can anyone point me in the right direction?
For example, how would combine these two selects into one query that will list the total and filtered actions:
SELECT COUNT(actions) as actioncount, location FROM mytable GROUP BY location
SELECT COUNT(actions) as actioncount, location FROM mytable WHERE mycondition IS NULL GROUP BY location
SELECT COUNT(CASE WHEN mycondition IS NULL THEN actions END) AS
actioncount1,
COUNT(actions) AS actioncount2,
location
FROM mytable
GROUP BY location
--
David Portas
------------
Please reply only to the newsgroup
Nice! :-)
"David Portas" <RE****************************@acm.org> wrote in message
news:l7********************@giganews.com... SELECT COUNT(CASE WHEN mycondition IS NULL THEN actions END) AS actioncount1, COUNT(actions) AS actioncount2, location FROM mytable GROUP BY location
-- David Portas ------------ Please reply only to the newsgroup
Thanks Shervin,
Works exactly as I need, I am indebted!
Cheers,
Jack
"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:vn************@corp.supernews.com... You can use subqueries like this:
select location, actioncount1 = (select count(actions) from mytable T1 where T1.location = mytable.location ), actioncount2 = (select count(actions) from mytable T2 where mycondition is null and T2.location = mytable.location ) from mytable group by location
Shervin
"JackT" <tu***********@ntlworld.com> wrote in message news:Fp***************@newsfep1-gui.server.ntli.net... I want to get a column count several times in one query using different filters but can't work out how to do it - can anyone point me in the
right direction?
For example, how would combine these two selects into one query that
will list the total and filtered actions:
SELECT COUNT(actions) as actioncount, location FROM mytable GROUP BY location
SELECT COUNT(actions) as actioncount, location FROM mytable WHERE mycondition IS NULL GROUP BY location
You're welcome my friend, but take a look at David's code. I prefer his way
:-) Subqueries are not the most efficient way to do thing in most cases. But
they are so easy to use. They are for lazy guys like me ;-)
Shervin
"JackT" <tu***********@ntlworld.com> wrote in message
news:vp**************@newsfep1-gui.server.ntli.net... Thanks Shervin, Works exactly as I need, I am indebted! Cheers, Jack "Shervin Shapourian" <Sh**********@hotmail.com> wrote in message news:vn************@corp.supernews.com... You can use subqueries like this:
select location, actioncount1 = (select count(actions) from mytable T1 where T1.location = mytable.location ), actioncount2 = (select count(actions) from mytable T2 where mycondition is null and T2.location = mytable.location ) from mytable group by location
Shervin
"JackT" <tu***********@ntlworld.com> wrote in message news:Fp***************@newsfep1-gui.server.ntli.net... I want to get a column count several times in one query using
different filters but can't work out how to do it - can anyone point me in the right direction?
For example, how would combine these two selects into one query that will list the total and filtered actions:
SELECT COUNT(actions) as actioncount, location FROM mytable GROUP BY location
SELECT COUNT(actions) as actioncount, location FROM mytable WHERE mycondition IS NULL GROUP BY location
Wow!
Cheers,
Jack
"David Portas" <RE****************************@acm.org> wrote in message
news:l7********************@giganews.com... SELECT COUNT(CASE WHEN mycondition IS NULL THEN actions END) AS actioncount1, COUNT(actions) AS actioncount2, location FROM mytable GROUP BY location
-- David Portas ------------ Please reply only to the newsgroup
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven An |
last post by:
Howdy,
I need to write an update query with multiple aggregate functions.
Here is an example:
UPDATE t
SET
t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ),
t.b = ( select sum(f.q)...
|
by: Rick Caborn |
last post by:
Does anyone know of a way to execute sql code from a dynamically built
text field?
Before beginning, let me state that I know this db architecture is
built solely for frustration and I hope to...
|
by: pb648174 |
last post by:
I have a single update statement that updates the same column multiple
times in the same update statement. Basically i have a column that
looks like .1.2.3.4. which are id references that need to...
|
by: Keith B via SQLMonster.com |
last post by:
Hi!
I want to return a derived table along with 4 simple tables in a stored
procedure as follows:
Input parameter: @FtNum (==Order Number, selects one Order and all
associated data)...
|
by: Peter Proost |
last post by:
Hi group,
has anyone got any suggestions fot the best way to handle this problem, I've
got 3 tables
for example table A, B, and C
table A looks like name, value
table B looks like name, value...
|
by: rdemyan via AccessMonster.com |
last post by:
I have a need to add another field to all of my tables (over 150). Not data,
but an actual field.
Can I code this somehow. So the code presumabley would loop through all the
tables, open each...
|
by: =?Utf-8?B?QVRT?= |
last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g.
Please help,
I'm trying to write an ASP page to use ADO to run a long query against an
Oracle 10g database, to create tables,...
|
by: =?Utf-8?B?TG9zdEluTUQ=?= |
last post by:
Hi All :)
I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView.
True DBGrid has a MultipleLines property that controls whether individual
records span multiple lines. Is...
|
by: KrazyKasper |
last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items
I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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 project—planning, coding, testing,...
|
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...
| | |