Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        
            
                
                    
                        
                            
                                | Author | 
                                
                                 Topic  | 
                             
                            
                                    | 
                                         kond.mohan 
                                        Posting Yak  Master 
                                         
                                        
                                        213 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2015-05-04 : 10:49:54
                                            
  | 
                                             
                                            
                                            | Hi i had created below querySELECT md.POLICYNO,'0-18' 'AGE WISE',CASE WHEN rs.id=2 THEN '01.Self' WHEN rs.id in(18,21) THEN '02.Spouse'                          WHEN rs.id in(4,5) THEN '03.Child' WHEN rs.id in(20,12,3,13) THEN '04.Parents'                            when rs.id not in (2,3,4,5,11,12,13,18,20,21) then '05.Others' end Relationship,                          case when COUNT(CASE WHEN m.age  BETWEEN 0 AND 18 THEN md.id END)<0 then 0                         else  COUNT(CASE WHEN m.age  BETWEEN 0 AND 18 THEN md.id END) end  Count        into #temp                            FROM  member m inner join memberdetails md on M.ID=MD.MemberID                               inner join RelationShip rs on RS.Id=MD.RelationShipID                               where m.deleted=0 and md.deleted=0 and rs.deleted=0                             GROUP BY md.POLICYNO,rs.id output of the query is :Policyno        agewise  relation     countGHS/Q0000004	0-18	03.Child	13GHS/Q0000004	0-18	02.Spouse	0GHS/Q0000004	0-18	02.Spouse	2GHS/Q0000004	0-18	03.Child	30GHS/Q0000004	0-18	01.Self	         0i had created Above query to analyse the Age wise report.output is correct.  we need to validate the below case to above query.if values are not Exist it should show '0' to repective columnsin the above output Relation column not displaying Parent,others.my need is  should show the all cases and count values needs to show 0If anyone knows pls guide the above validattion | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     gbritton 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    2780 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-05-04 : 11:14:37
                                          
  | 
                                         
                                        
                                          Check out poorsql.com for formatting your code, and remember to use  tags.  Here's your query reformatted (more readable):SELECT md.POLICYNO	,'0-18' 'AGE WISE'	,CASE 		WHEN rs.id = 2			THEN '01.Self'		WHEN rs.id IN (18, 21)			THEN '02.Spouse'		WHEN rs.id IN (4, 5)			THEN '03.Child'		WHEN rs.id IN (20, 12, 3, 13)			THEN '04.Parents'		WHEN rs.id NOT IN (2, 3, 4, 5, 11, 12, 13, 18, 20, 21)			THEN '05.Others'		END Relationship	,CASE 		WHEN COUNT(CASE 					WHEN m.age BETWEEN 0							AND 18						THEN md.id					END) < 0			THEN 0		ELSE COUNT(CASE 					WHEN m.age BETWEEN 0							AND 18						THEN md.id					END)		END CountINTO #tempFROM member mINNER JOIN memberdetails md ON M.ID = MD.MemberIDINNER JOIN RelationShip rs ON RS.Id = MD.RelationShipIDWHERE m.deleted = 0	AND md.deleted = 0	AND rs.deleted = 0GROUP BY md.POLICYNO	,rs.id Also, post some sample data and expected results. Its not really clear to me what you are trying to do.Gerald Britton, MCSAToronto PASS Chapter  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     kond.mohan 
                                    Posting Yak  Master 
                                     
                                    
                                    213 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-05-04 : 11:39:39
                                          
  | 
                                         
                                        
                                          | Thank you gbittonWe have 2 tables one is relationship table(relationshipid,name)Sampleid    name1	Un known2	Self3	Mother4	Son5	Daughter6	Sister in law7	Brother in law8	Grandmother9	Grandfather10	None11	Son in lawmember details  table has below formatpolicyno       relationshipidGHS/Q0000004	2GHS/Q0000004	4GHS/Q0000004	5GHS/Q0000004	18GHS/Q0000004	21 as per the above dataPolicyno does not contain the data fro relationship 3.but i have written my case logic  for id 3(mohter). but data is not there.my need is if data is not there for those case need to show the values as 0 and Relationship should show as respective onePls guide on the Samemohan  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     gbritton 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    2780 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-05-04 : 12:40:44
                                          
  | 
                                         
                                        
                                          | Ok, but along with the explanation, please post the desired results from the query, using the sample data you provided.Gerald Britton, MCSAToronto PASS Chapter  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     kond.mohan 
                                    Posting Yak  Master 
                                     
                                    
                                    213 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-05-05 : 00:03:22
                                          
  | 
                                         
                                        
                                          | hi,Here below policy no does not have data for mother ,others...All the Relationship(column) should show the Result irrespective of the Data.here 0-18 between all the relationships  has listed belowPolicyno agewise relation countGHS/Q0000004	0-18	03.Child	13GHS/Q0000004	0-18	02.Spouse	0GHS/Q0000004	0-18	02.Spouse	2GHS/Q0000004	0-18	03.Child	30GHS/Q0000004	0-18	01.Self	         0GHS/Q0000004	0-18	04.Parents       0GHS/Q0000004	0-18	05.others	 0  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |