473,799 Members | 3,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimized statement for subquery using count(*)

Hello,

DB2 LUW V8 FixPack 13.

create table Table (ID varchar(20), USED char)

I need to find out the total row count per ID, as well as the row count
where USED=Y.

I could do this with a simple:

select X.ID, X.Total, Y.Total from
(select ID, COUNT(*) as Total from Table group by ID) as X
(select ID, COUNT(*) as Used_Total from Table where USED='Y' group by
ID) as Y
where X.ID=Y.ID

Depending on my table size, this query can be resource intensive and
take some time to finish.

Is there any other way to achieve the same result with an optimized
statement?

Thanks in advance, Michel

Nov 21 '06 #1
4 5633
Michel Esber wrote:
Hello,

DB2 LUW V8 FixPack 13.

create table Table (ID varchar(20), USED char)

I need to find out the total row count per ID, as well as the row count
where USED=Y.

I could do this with a simple:

select X.ID, X.Total, Y.Total from
(select ID, COUNT(*) as Total from Table group by ID) as X
(select ID, COUNT(*) as Used_Total from Table where USED='Y' group by
ID) as Y
where X.ID=Y.ID

Depending on my table size, this query can be resource intensive and
take some time to finish.

Is there any other way to achieve the same result with an optimized
statement?

Thanks in advance, Michel
Are you trying to find the ID's for which the exists a USED <'Y'?
SELECT DISTINCT ID FROM TABLE WHERE USED <'Y'
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Nov 21 '06 #2
Michel Esber wrote:
Hello,

DB2 LUW V8 FixPack 13.

create table Table (ID varchar(20), USED char)

I need to find out the total row count per ID, as well as the row count
where USED=Y.

I could do this with a simple:

select X.ID, X.Total, Y.Total from
(select ID, COUNT(*) as Total from Table group by ID) as X
(select ID, COUNT(*) as Used_Total from Table where USED='Y' group by
ID) as Y
where X.ID=Y.ID

Depending on my table size, this query can be resource intensive and
take some time to finish.

Is there any other way to achieve the same result with an optimized
statement?
SELECT id, COUNT(*) AS total,
SUM(CASE WHEN used = 'Y' THEN 1 ELSE 0 END) AS used_total
FROM table
GROUP BY id

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 21 '06 #3
Michel Esber wrote:
>
Hello,

DB2 LUW V8 FixPack 13.

create table Table (ID varchar(20), USED char)

I need to find out the total row count per ID, as well as the row count
where USED=Y.

I could do this with a simple:

select X.ID, X.Total, Y.Total from
(select ID, COUNT(*) as Total from Table group by ID) as X
(select ID, COUNT(*) as Used_Total from Table where USED='Y' group by
ID) as Y
where X.ID=Y.ID

Depending on my table size, this query can be resource intensive and
take some time to finish.

Is there any other way to achieve the same result with an optimized
statement?

SELECT id, COUNT(*) AS total,
SUM(CASE WHEN used = 'Y' THEN 1 ELSE 0 END) AS used_total
FROM table
GROUP BY id

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Knut and Serge, thanks. My access plan is now much more efficient with
a lower cost.

-M

Nov 21 '06 #4
This will also work.
SELECT id, COUNT(*) AS total,
COUNT(CASE WHEN used = 'Y' THEN 1 END) AS used_total
FROM table
GROUP BY id

Nov 22 '06 #5

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

Similar topics

3
2541
by: Igor Kryltsov | last post by:
Hi,I am using slightly modified example posted by Doug Younger and answered by Tom Lane :)(http://archives.postgresql.org/pgsql-sql/1999-08/msg00159.php) I have the following 2 tables: Table groups: g_id int4 g_name text Table users:
0
1457
by: darkdirk1 | last post by:
SQL Value: "COUNT(tbl__UserLog._UserLog_ID) AS Count_Logins " Returns as varchar which is killing me, because my user interaction is loaded w/ sort features. Unfortunately the count_logins var getting posted to the script is an integer. So I recieve : Syntax error converting the varchar value 'Count_Logins' to a column of data type int. When the post var gets compared to the AS Count_Logins var (in a Dynamic WHERE clause).... If I...
1
3222
by: sql_er | last post by:
Hi all, I am trying to convert an SQL statement into an XPath (or a sequence of XPath) statements. More specifically, I have the following: SELECT a FROM b WHERE c IN (SELECT d FROM e) I recently learned about XPath and know how to perform simple filtering
4
2692
by: AtCor | last post by:
I am trying to filter data using count. For a given day and patient, I would like to return all the valid tests. When I use a count this way, it only returns the patients with at least 4 tests. I want it to display those patients that have 1, 2, 3, or 4 valid tests, but not if 5, 6 or .... we taken on a given day. I only need the first four valid tests. Any suggesions? SELECT Sheet1$. FROM M_PWA INNER JOIN ...
1
5184
by: ntocher | last post by:
Is it possible in a single query to search up to 12 tables for a similar text string and count the number each time a duplicate is found? I have a table for each month with a string field that contains a referer string. table = stats_2007_01 table = stats_2007_02 table = stats_2007_03 and so on.... field = string (200) I can query a single table and return the result i expect with this statement
2
3768
by: Bam | last post by:
hey gang. i am trying to pull a count from the top 16 records in a db. select count(top 16 (username)) as cnt2 from bracketdb_"&tourney_id&" where username <'BYE' order by POS this is what i need to do, however, it doesn't work. i am getting a systax error near TOP. this is using SQLdb
3
10015
by: cmartin1986 | last post by:
I have written a sql query and I need it to return 0 when it doesn't find any matches to my criteria. I have tried adding iif statements, tried sum, and just Count, all of these methods work fine to return the values when it finds matches, but i need it also to return a 0 when there are no matches. Here is what I got. SELECT "CAL Recieved" as Tags, .,sum(iif(.,1,0)) AS FROM GROUP BY . HAVING (((.)=(Date()-1))); UNION ALL SELECT...
3
2513
by: Hasse1982 | last post by:
Hi I have a table KDOCUMENT with the columns , , , , , ,
5
7025
by: billelev | last post by:
I have a cross tab query with Date as a row heading and a series of Names as column headings. The Value for each Date/Name intersection can either be -1, 0 or 1. For example: Date, Name1, Name2, Name3, Name4 1/1/2000, -1, -1, 0, 1 1/2/2000, 1, 1, 0, 0I would like to have a column ("Members") that counts the non-zero values for each row. This would give something like the following: Date, Members, Name1, Name2, Name3, Name4...
0
9544
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,...
0
10490
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, 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...
1
7570
isladogs
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...
0
6809
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();...
0
5467
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...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
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...

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.