GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2009, 15:33
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

COUNT() from multiple tables with conditions


Hello all,

I am semi-new to SQL and am trying to build a query. I have two tables:

[Routes]
RouteID: int
Status: int

[Meters]
MeterID: int
RouteID: int
Reading: real

I would like to select Routes.Status and a count for the total number of Meters and a count for the unread meters (Reading = NULL) where Meters.RouteID = Routes.RouteID. My Current SELECT statement looks like:

Code:
SELECT Routes.Status, COUNT(Meters.MeterID) AS TotalMeters, COUNT(Meters.Reading) AS UnreadMeters FROM Routes LEFT JOIN Meters ON Routes.RouteiD = Meters.RouteID GROUP BY Routes.Status
The problem is that I am getting the non-null count but I need the null count. Any ideas?

Thanks
-fakepoo
  #2  
Old 13-May-2009, 00:30
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 1,008
admin will become famous soon enough

Re: COUNT() from multiple tables with conditions


I suppose you could simply calculate the difference as `TotalUnread`. Maybe something like this:

Code:
SELECT `R`.`RouteID` ,`R`.`Status` ,COUNT(`M`.`MeterID`) AS `TotalMeters` ,COUNT(`M`.`Reading`) AS `TotalRead` ,COUNT(`M`.`MeterID`)-COUNT(`M`.`Reading`) AS `TotalUnread` FROM `Meters` AS `M` LEFT JOIN `Routes` AS `R` ON `R`.`RouteID`=`M`.`RouteID` GROUP BY `M`.`RouteID`

Of course I have no way of (really) testing this SQL but it gives you an idea to simply insert a simple subtraction to get the total unread meters.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #3  
Old 13-May-2009, 06:36
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: COUNT() from multiple tables with conditions


Thank you admin. Clearly, I have a long way to go but this helped me over a hump. I'm sure I'll have more questions along the way.
  #4  
Old 11-Jun-2009, 16:41
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: COUNT() from multiple tables with conditions


OK. Now I have another problem. I have decided that each Meter now belongs to an Address record instead of a Route record. Routes will now contain Address records.

[Routes]
RouteID: int
Status: int

[Addresses]
AddressID: int
RouteID: int
Address: nvarchar

[Meters]
MeterID: int
AddressID: int
Reading: real

Now, I would like to get the number of addresses per route, the number of meters per route, and the number of unread meters per route.

I am, instead, getting really large numbers like it is counting every meter that is equal to another meter or something.

Thanks,
~fakepoo
  #5  
Old 12-Jun-2009, 07:53
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: COUNT() from multiple tables with conditions


Sorry, but there was not a problem with my query. My database had duplicate AddressID values and that was causing weird results.
 
 

Recent GIDBlogConfiguring iptables for Webmin Servers Index Module by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find a mathematical formula for this recursion? transgalactic C Programming Language 8 12-Oct-2008 07:43
registering new users, inserting into multiple tables eb_webdeveloper MySQL / PHP Forum 4 22-Sep-2006 09:10
HELP!! New to C! diveboy866 C Programming Language 4 23-Mar-2006 08:41
Linker errors with multiple file progam nkhambal C Programming Language 2 24-Apr-2005 02:37
Count entries from 2 tables misunderstood MySQL / PHP Forum 6 31-May-2004 16:45

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:12.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.