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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count records within query

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
I have the following information in table 'Test1':

field1 field 2
000001 000000
000002 000001
000003 000000
000004 000000

I want to write a query that will only display the unique field 2 records and as well provide a count of how many records have that id.

output should look like

field2 recno
000000 3
000001 1

can some one help with the trick to make this work?

thx
 
are you kidding me! ;o) that easy.

sry...been coding all night and brain just not cooperating now. definately think its time for a break.

thx again.
 
ok....misfire....that did work but alas not as i expected. I completely supplied the wrong info.

Let me try this again (Same data as above):

Ouput should ACTUALLY read like this:

Only display records where field2 = 000000 and count those records where field2=field1

field1 records
000001 1
000003 0
000004 0

yes...that's how it should read. I thought it was a little more complicated than your solution. ;o)

thx again.


 
This is my sample data from table 'Test1':

field1 field2
-------------------
000001 000000
000002 000001
000003 000000
000004 000000

Here is where I believe it gets tricky:

Part1: Only display those records where field2='000000'
Part2: Count the corresponding records where field2=field1

Output:

field1 reccount
000001 1 <-- 1 for one found match in field2
000003 0
000004 0

I do believe that is how it should be.

thx again.
 
Hi

Got it ! I think...
Code:
[b]select[/b] field1,([b]select[/b] count(*) [b]from[/b] test1 [b]where[/b] field2=t.field1) reccount [b]from[/b] test1 t [b]where[/b] field2=[i]'000000'[/i];

Feherke.
 
Hi

And the faster version of the above :
Code:
[b]select[/b] t.field1,sum(if(t2.field2 [b]is not null[/b],1,0)) reccount [b]from[/b] test1 t [b]left join[/b] test1 t2 [b]on[/b] t.field1=t2.field2 [b]where[/b] t.field2=[i]'000000'[/i] [b]group by[/b] t.field1;

Feherke.
 
I tried the first one you posted and it worked until I added more records to the table. i stays at the same out put.

let me look at your second one.
 
feherke....just wanted to thank you again for your assistance on the code. I wound up using the second script. Works great! Back in the office now and the code is flowing once again.
 
i think this would work and be even a little more efficient:

select t.field1,count(*) reccount from test1 t join test1 t2 on t.field1=t2.field2 where t.field2='000000' group by t.field1;

the theory here is that you only need to count fields where the JOIN is succesful (ie, there is a field2 matching the given field1)... the previous solution did a left-join, which gets all matches, and then applies an IF to filter out those records in the SUM statement by passing it a 0.

Instead, let the query engine only feed records to count(*) that have already matched the JOIN ON clause successfully, and you get what you want, without having to do extra work to filter out that which you don't want.

result should be the same, but i believe may be quite a bit (relatively speaking) more efficient, and even more so as the data set grows (assuming indexes are maintained appropriately).
 
Hi

shadedecho, that does not return the result james0816 specified. You are right, is more efficient, but I do not understand why do you think is a solution.
Code:
[blue]mysql>[/blue] select t.field1,count(*) reccount from test1 t join test1 t2 on t.field1=t2.field2 where t.field2='000000' group by t.field1;
+--------+----------+
| field1 | reccount |
+--------+----------+
| 000001 |        1 |
+--------+----------+
1 row in set (0.01 sec)

Feherke.
 
ahh... so you need the 0 count rows? I guess I didn't pick up on that. Sorry. Then yes, the left-join method as specified would be the most efficient way to do it, since the JOIN will not include records for which there is no match (ie, the 0 rows). apologize for the confusion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top