Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL MySQL
Need to write Mysql query showing statistics,
 
Description:
Parent table 60 lac records contains unique records
Child table with 1 crore Contains multiple records
 Parent Table
 ID  Flag1 Flag2
 1    1      1
 2    NULL   1
 3    1      1 
 
Child Table
 ID acc Category
  1 101  Cate1
  1 102  Cate5
  2 103  Cate2
  2 104  Cate3
  3 105  Cate4
  3 106  Cate4 
One to many relationship between two tables.
 
I need to write group by queries to find categoriwise and flag based count in one query with MySQL Database
sample:
I have tried with following query but i dont want to write where condition for each category and Flag as Child table contains 30 categories and 12 Flags
select count(1) from Parent A,Child B where A.ID=B.ID AND B.Category='CATE1' and A.Flag1=1
Result expected
Category  Flag1_Count Flag2_Count
  Cate1       1           0 
  Cate2       0           1
  Cate3       1           1
Please help.
Posted 23-Feb-13 23:20pm
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The query will be like as follows
SELECT B.Category, Count(A.Flag1) AS Flag1_Count,  Count(A.Flag2) AS Flag2_Count FROM Parent A 
INNER JOIN Child B WHERE A.ID=B.ID 
GROUP BY B.Category;
  Permalink  
v2
Comments
ixashish at 27-Feb-13 22:08pm
   
Thanks Ahaasan,your query helped me to get right path.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 424
1 ridoy 400
2 DamithSL 230
3 OriginalGriff 179
4 Suvendu Shekhar Giri 150
0 OriginalGriff 8,033
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,538
3 Maciej Los 5,508
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100