473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count # of unioned values

Suppose I have a table with 2 VARCHAR2(1)columns C1 and C2 and the
following 3 rows:
A B
A C
B A

For each value in columns C1 and/or C2, I need to report the count of
that value in each column such as:

Value #C1s #C2s
----- ----- -----
A 2 1
B 1 1
C 0 1
----- ----- -----
Total 3 3

I have written a PL/SQL procedure to do this. With Oracle 9.2 what is
the best method to do this with a single SQL query?
Jul 19 '05 #1
2 2455
Hi Joe,
this should work

compute sum label total of out1 out2 on report
break on report

SELECT COL, SUM(C1) as #1,SUM(C2) as #2
FROM
(SELECT COL1 AS COL,COUNT (*) AS C1,0 AS C2
FROM TEST
GROUP BY COL1
UNION
SELECT COL2 AS COL,0 AS C1,COUNT (*) AS C2
FROM TEST
GROUP BY COL2)
GROUP BY COL
/

HTH
Conan

Joe Powell wrote in message ...
Suppose I have a table with 2 VARCHAR2(1)columns C1 and C2 and the
following 3 rows:
A B
A C
B A

For each value in columns C1 and/or C2, I need to report the count of
that value in each column such as:

Value #C1s #C2s
----- ----- -----
A 2 1
B 1 1
C 0 1
----- ----- -----
Total 3 3

I have written a PL/SQL procedure to do this. With Oracle 9.2 what is
the best method to do this with a single SQL query?

Jul 19 '05 #2
jo********@lmco.com (Joe Powell) wrote in message news:<de**************************@posting.google. com>...
For each value in columns C1 and/or C2, I need to report the count of
that value in each column such as:

Value #C1s #C2s
----- ----- -----
A 2 1
B 1 1
C 0 1
----- ----- -----
Total 3 3

This should do it :

select
value,
sum(decode(col,'C1',cnt,0)) cnt_c1,
sum(decode(col,'C2',cnt,0)) cnt_c2
from
(
select 'C1' col, c1 value, count(*) cnt
from table1 group by c1
union all
select 'C2' col, c2 value, count(*) cnt
from table1 group by c2
) s1
group by value
KiBeHa
Jul 19 '05 #3

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

Similar topics

4
22690
by: Spark | last post by:
Hi, Situation: Need a query to return number of ticket records by month of open in a log table where the ticket open record is older than 24 hours then the ticket pending or ticket closed...
0
2126
by: kovac | last post by:
The System.directoryservices.dll has an error, and this error was described in http://support.microsoft.com/default.aspx?scid=kb;en-us;839424 At the moment we have Framework version v1.0.3705 and...
22
3169
by: Joseph Shraibman | last post by:
On a 7.3.4 database: explain analyse select count(*) from elog; Aggregate (cost=223764.05..223764.05 rows=1 width=0) (actual time=81372.11..81372.11 rows=1 loops=1) -> Seq Scan on elog ...
5
18237
by: Cro | last post by:
Hello Access Developers, I'd like to know if it is possible to perform a count in an expression that defines a control source. My report is based on a query. In my report, I want a text box to...
9
10807
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
4
1684
by: Danielle | last post by:
I have data like this in a two column temporary table - ID Age 23586 3 23586 3 23586 2 23586 2 23586 1 23586 1
1
1826
by: sammy | last post by:
If you have a select with 2 attributes where you group by one attribute and do a count() for the second attribute, if the count() is 0 then that row is never displayed. How would you instead see...
22
12425
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
2
177
by: Joe Powell | last post by:
Suppose I have a table with 2 VARCHAR2(1)columns C1 and C2 and the following 3 rows: A B A C B A For each value in columns C1 and/or C2, I need to report the count of that value in each...
0
7224
marktang
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,...
0
7323
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,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7494
tracyyun
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...
0
5626
agi2029
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,...
1
5050
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...
0
4706
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...
0
1553
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 ...
0
415
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...

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.