If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > multiply some records in COUNT(*)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-10, 10:45
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
multiply some records in COUNT(*)

i have query like this:

Code:
SELECT COUNT(*), car_id FROM tb1 
WHERE value IN(1000,3000)
GROUP BY car_id
this query return number of cars that have value of 1000 or 3000$, is there any way to multiply on 2, cars with 3000$ value ?
for example if there are 3 of 1000$ car & 2 of 3000$ car, it return: 7.
i know i can use SUM with a little trick, but i wonder if there is any way to do it with count.
__________________
Lyrics Database: www.shermani.com

Last edited by bono56; 11-20-10 at 12:33.
Reply With Quote
  #2 (permalink)  
Old 11-20-10, 12:05
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
a lot depends on the primary key

what is the primary key of tb1?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-20-10, 12:37
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
why primary key? i think it not important, suppose something like `car_id`.

car_id cat_id value
1 a 1000
2 a 1000
3 a 1000
4 b 3000
5 b 3000

there are 3 of cat a & 2 of cat b.
count return 5, but i want to consider every category b record, double.
3+2*2=7
__________________
Lyrics Database: www.shermani.com
Reply With Quote
  #4 (permalink)  
Old 11-20-10, 12:44
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bono56 View Post
why primary key? i think it not important, suppose something like `car_id`.
which simply reveals that you are not telling us the correct information

with car_id in the GROUP BY, your COUNT(*) will be exactly 1

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-22-10, 11:17
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Do you mean something like:

Code:
select sum(case when value = 1000 then 1
                when value = 3000 then 2
           end)
    , a.car_cnt
    , a.value
    SELECT COUNT(*) as car_cnt
         , value
       FROM tb1 
    WHERE value IN(1000,3000)
    GROUP BY value) as a
Dave
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On