User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 401,673 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,426 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 663 | Replies: 3
Reply
Join Date: May 2008
Posts: 1
Reputation: navneet1083 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
navneet1083 navneet1083 is offline Offline
Newbie Poster

get count on selected query of count ??

  #1  
May 21st, 2008
I've fired a query which result some data with the respective count from the table and after that i want to fire a query to get the total number of count on that count which i got it from my first query. Could anyone helps me out to get the total count of the query of a query.
Suppose i've query like :
  1. SELECT i, substring_index(source_ip, '.', i) source_ip_new, count(*) AS myCount
  2. FROM
  3. (SELECT (1) AS i union SELECT (2) union SELECT (3) union SELECT (4)) AS n
  4. JOIN userbase_copy
  5. GROUP BY i, source_ip_new
  6. HAVING i = 3 AND count(*) > 0
  7. ORDER BY myCount DESC

which will give me the count of ip(xxx.xxx.xxx.xxx) with equals ignoring the last ip index. After this i want to get count on the whole result. suppose i got result like :
=============================
3 203.88.10 4 4
3 122.166.12 3
3 203.88.8 1 1
3 117.97.13 1 1
3 117.97.37 1
==============================
After this reslut how to get count on the above result. The count should give me the value 10(4+3+1+1+1).
any suggestions/helps are welcome.
thanks in advance.
Last edited by peter_budo : May 23rd, 2008 at 11:13 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 10
Reputation: jemajoign is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jemajoign jemajoign is offline Offline
Newbie Poster

Re: get count on selected query of count ??

  #2  
May 21st, 2008
Is this what you need?

  1. SELECT i, substring_index(source_ip, '.', i) source_ip_new, count(*) AS myCount
  2. FROM (
  3. SELECT (1) AS i
  4. union SELECT (2)
  5. union SELECT (3)
  6. union SELECT (4)
  7. ) AS n
  8. JOIN userbase_copy
  9. GROUP BY i, source_ip_new WITH rollup
  10. HAVING i = 3 AND count(*) > 0;
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: get count on selected query of count ??

  #3  
May 21st, 2008
Hi navneet1083,

I am not that sure whether the following sql statement would work correctly for your problem, but I have similar problem tested with my database where I have created subtotals with with-clause first then I "unioned" this result with grand total. Maybe you can try this:
  1. WITH subtotals (n, ip, x, st) AS
  2. (
  3. SELECT cast(i AS char), substring_index(source_ip, '.', i),
  4. source_ip_new, count(*) AS myCount
  5. FROM
  6. (SELECT (1) AS i union SELECT (2) union SELECT (3) union SELECT (4)) AS n
  7. JOIN userbase_copy
  8. GROUP BY i, source_ip_new HAVING i = 3 AND count(*) > 0
  9. ORDER BY myCount DESC
  10. )
  11. SELECT * FROM subtotals
  12. union
  13. SELECT n, 'Total ', 'counts', sum(st) FROM subtotals
I simply copied your select statement and put it into (...) without checking it. Because columns of unions must be compatible, cast(i) might be necessary.

Ok, similar construct works fine on my tables. So I hope it also does it for your problem.
Tell me whether it works on your tables, maybe also the modifications you made.

krs,
tesu
Last edited by tesuji : May 21st, 2008 at 6:56 pm.
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: get count on selected query of count ??

  #4  
May 22nd, 2008
  1. ---- Line 13 of previous code listing:
  2.  
  3. SELECT n, 'Total ', 'counts', sum(st) FROM subtotals
  4.  
  5. ---- must be changed to:
  6.  
  7. SELECT n, 'Total ', 'counts', sum(st) FROM subtotals GROUP BY n

krs,
tesu
Last edited by tesuji : May 22nd, 2008 at 6:23 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 7:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC