1
SELECT (select count(u.ag_code) 
from table1 as u inner join table2 as tc 
on u.industry_id=tc.tempcatid 
where u.ag_code!=0) as agnt,
    (select count(u.ag_code) 
     from table1 as u inner join table2 as tc 
     on u.industry_id=tc.tempcatid where u.ag_code=0),as dircus,
tc.catename from table1 as u inner join table2 as tc 
where u.industry_id=tc.tempcatid 
group by tc.tempcatid

this query have error i need two count and category name in one query
this is the condition for count

  1. ag_code!=0
  2. ag_code=0

in table1 have column ag_code (this have 0 and nonzero value)

my result need like this

Full Texts
agent   customer    catename
11  3   Real Estate
15  1   Automobile
3   0   Medical
34  77  Business
1   45  Travel & Hotels
11  3   Construction & Engineering
3
  • 1
    Try this bytes.com/topic/mysql/answers/… Commented Mar 22, 2011 at 9:22
  • 1
    What is the question? What results are you getting with the query you posted? Commented Mar 22, 2011 at 9:27
  • I really tried to focus on what exactly your query is doing, but I really couldn't grasp it. You'll need to describe in a little more detail what you're trying to accomplish here. Commented Mar 22, 2011 at 9:36

2 Answers 2

1
SELECT tc.catename,
       count(case when u.ag_code!=0 then 1 end) agnt,
       count(case when u.ag_code =0 then 1 end) dircus
from table1 as u
inner join table2 as tc on u.industry_id=tc.tempcatid 
group by tc.tempcatid, tc.catename
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks its provided my expecting result exactly
@mohan - Great! Please tick next to the answer - you won't be able to do that yet for some more minutes
Is it better to explicitly state "else NULL", or beter to keep it short but implicit (and so not always understood)?
@Dems: There's a couple of middle ways, for example: keep it short and explain what it means or keep it short and let someone else explain what it means. :)
Hi again one query i need same before but the count want mcgross column based on 2types of condition 1.ag_code=0 2.ag_code!=0 SELECT tc.catename, count(case mcgross when u.ag_code!=0 then 1 end) agnt, count(case mcgross when u.ag_code =0 then 1 end) dircus from table1 as u inner join table2 as tc on u.industry_id=tc.tempcatid group by tc.tempcatid, tc.catename I need count for mcgross based on the 2 condition 1.ag_code=0 2.ag_code!=0
0

Hi There you might be able to use the below example to get back the result you require.

Select SUM(Inactive) Inactive ,SUM(Active) Active FROM
( 
    Select Count(t1.UserId) Inactive,0 Active 
    FROM
    (select * from users where inactive=1) as t1
    UNION 
    SELECT 0 Inactive,Count(t2.UserId) Active  FROM
    (select * from users where inactive=0) as t2  
) as result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.