Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need two column count from one field (one for value x, one for value y 1

Status
Not open for further replies.

FromAjo

Programmer
Nov 11, 1999
1
0
0
US
I am doing a select count valuex as x, count valuex as y. In other words I need to count different values from the same field and have them print out two separate counts. I'm stumped. Please, if anyone has any answers....
 
Please give a sample of your data and the desired output; it will make it easier to help you.
 
Hi,<br>
<br>
Like Carp, I'm not sure what you are trying to achieve. However, your SQL will look something like:<br>
<br>
SELECT COUNT(VAL1) AS V1, COUNT(VAL1) AS V2<br>
FROM TABLE<br>
WHERE ......;<br>
<br>
This will give you the same value twice. I can't see how you can get two differing values from the same column in the one SQL statement. The values are decided by your WHERE clause, this section restricts what you are selecting on.<br>
<br>
HTH,<br>
<br>
C
 
OK, let me guess at what you're after:<br>
<br>
I think what you are trying to do is count how many occurrences of certain values occur in a column. For instance, if the data in your column looks like:<br>
<br>
X<br>
---------<br>
3<br>
5<br>
5<br>
5<br>
3<br>
<br>
then you would like your output to look like:<br>
<br>
count_of_3 .............count_of_5<br>
..............2... ...................... 3<br>
(Ignore the periods - I just have to put something in to maintain the space between columns.)<br>
<br>
This can be done with the following query:<br>
<br>
SQL&gt; SELECT(DECODE(x,3,1,NULL)) count_of_3, COUNT(DECODE(x,5,1,NULL)) count_of_5<br>
2 FROM your_table;<br>
<br>
Hopefully you aren't testing for too many values.<br>
<br>
To make this dynamic (so that you don't need to know the range of values ahead of time), you would probably be better off creating a procedure so you can pull the values into a cursor and explicitly count them.<br>
<br>
Let us know if this answers the question.
 
One other thing that strikes me, is that if you are doing this using SQL server you will probably have to write a stored procedure in TSQL to get your required results back. Unfortunately SQL Server has no ORACLE DECODE equivalent. The same goes for Ingres (I think). <br>
<br>
DECODE is not supported by the SQL ANSI 92 standard. Which is a pity, as carp showed how useful it is!<br>
<br>

 
Well, the good news is that there's more than one way to skin this cat - and at least one of them should be doable via ANSI standard SQL:<br>
<br>
Using the same sample data as before (and the same periods to maintain some spacing), you could try:<br>
<br>
SQL&gt; SELECT count3.c3 count_of_3, count5.c5 count_of_5<br>
2 FROM (SELECT count(*) c3 FROM test WHERE x = 3) count3,<br>
3 (SELECT count(*) c5 FROM test WHERE x = 5) count5;<br>
<br>
COUNT_OF_3 ......COUNT_OF_5<br>
...........----------................ ----------<br>
...................2..........................3<br>
<br>
I don't believe this conflicts w/ ANSI standards.
 
Carp,<br>
<br>
Thats a great solution. I didn't know you could do that (you live and learn!). I'll stick that syntax in memeory as I'm sure that it will come in handy.<br>
<br>
Thanks,<br>
<br>
C
 
Carps solution with the sub select will get the desired result however the overhead of multiple passes against the database/table/s could depending on the results desired be large.<br>
There is another solution that can get the result with 1 pass against the table/s and that is a <br>
<br>
select count(Distinct VAL1) from table1<br>
where VAL1 in(3,5)<br>
<br>
if you don't care whatr the result values are leave off the where clause.<br>
Greg
 
Greg - you're absolutely right! Somehow I got the impression that FromAjo was looking for the results to be in two separate columns! Upon rereading the original request, this is a MUCH simpler problem.<br>
<br>
You can also get the different counts for an arbitrary number of different values and not have to know the specific values ahead of time with:<br>
<br>
SELECT x, count(x)<br>
FROM your_table<br>
GROUP BY x;<br>
<br>

 
Greg - On further examination, I don't think your query will return the desired results. Your query will return one number which represents how many different values are in the column. I believe what FromAjo wanted was the number of occurrences of different values.<br>
<br>
Of course, right now we're all guessing what FromAjo really wanted because he/she never responded to our earlier requests for clarification!
 
How about this?<br>
<br>
select x1=(select count(your_field) from your_table where your_field=1),<br>
x2=(select count(your_field) from your_table where your_field=2)<br>
from your_table <p>Doug Trocino<br><a href=mailto:dtrocino@tecumsehgroup.com>dtrocino@tecumsehgroup.com</a><br><a href= Forums</a><br>Web Application Developer<br>
Tecumseh Group, Inc.<br>
Sponsors of Tek-Tips Forums<br>
 
Carp, I was coding from memory without the avaliablity to run a simple test and you are correct!! <br>
My solution would only return the sum count of the 2 values. And as you stated so clearly we don't know what FromAjo was really looking for.<br>
Thanks Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top