Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Scripting Languages (http://www.programmingforums.org/forum39.html)
-   -   Counting with SQL (http://www.programmingforums.org/showthread.php?t=13284)

taporctv Jun 5th, 2007 2:00 PM

Counting with SQL
 
Ok here's my problem. I ran a query that joined two tables. I used a WHERE statement to sort out the data. My problem is, how do I count the row in the sorted data table?

DaWei Jun 5th, 2007 2:40 PM

I don't understand your question. Do you mean that you want to know how many rows you have in the result? How to go to a specific row in the result? How to number the rows as you process them?

taporctv Jun 5th, 2007 2:54 PM

I want to know how many rows are returned in the result. Im using Access. I know that it says how many records are in the table at the bottom of the table. What I want to do is use the count function to determine how many rows were returned.

kruptof Jun 5th, 2007 3:09 PM

I think you would have to use the Group By clause, something similar to this:

:

select col1,count(*)
from table1 t1, table2 t2
where t1.id=t2.id and t1.name='john'
group by col1;


taporctv Jun 5th, 2007 3:49 PM

Ok I can count now, but my problem is since im joining three tables, im counting from the wrong table. The field i want to count is in the result of my query and in another table from the join. My count function is counting the field from the table i used in the join. How do i count the field in my query results.

Infinite Recursion Jun 5th, 2007 3:59 PM

Perhaps showing your query would help us help you. :)

taporctv Jun 6th, 2007 12:11 AM

[PHP]SELECT DISTINCT [SV-5.NameA]
FROM ([SV-5] LEFT JOIN SFtoFD ON [SV-5].NAMEA = SFtoFD.NAMEB) LEFT JOIN SFtoSV4 ON [SV-5].NAMEA = SFtoSV4.NAMEB
WHERE SFtoSV4.NAMEB IS NULL AND SFtoFD.NAMEB IS NULL;[/PHP]

This returns 9 records, which is the right amount. Now when I use

[PHP]SELECT COUNT(DISTINCT [SV-5.NameA])[/PHP]

I get an error message. What's my problem. Without the DISTINCT I get a count of 48 records which is not right.

DaWei Jun 6th, 2007 6:07 AM

You have to apply COUNT to the same query that provides the correct results. Does Access not have a count function that you can merely pass the result to? Something like
:

result = SELECT DISTINCT  [SV-5.NameA]
FROM ([SV-5] LEFT JOIN SFtoFD ON [SV-5].NAMEA = SFtoFD.NAMEB) LEFT JOIN SFtoSV4 ON [SV-5].NAMEA = SFtoSV4.NAMEB
WHERE SFtoSV4.NAMEB IS NULL AND  SFtoFD.NAMEB IS NULL;

records = num_rows (result)

I don't have Access on this system, so I can't check for you. Look at your docs.

taporctv Jun 6th, 2007 8:04 AM

That didn't work. I know it probably an easy solution that I cant think about.

xavier Jun 6th, 2007 8:55 AM

Apparently Access it's just weird :). It doesn't support count(distinct .. etc).

You could do this : http://allenbrowne.com/ser-66.html.
Or, you could Have an inner select , and count that:

:

  1. SELECT count(*) FROM
  2. (SELECT DISTINCT  [SV-5.NameA]
  3. FROM ([SV-5] LEFT JOIN SFtoFD ON [SV-5].NAMEA = SFtoFD.NAMEB) LEFT JOIN SFtoSV4 ON [SV-5].NAMEA = SFtoSV4.NAMEB
  4. WHERE SFtoSV4.NAMEB IS NULL AND  SFtoFD.NAMEB IS NULL;) AS a



All times are GMT -5. The time now is 3:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC