| Author | 
                                
                                 Topic  | 
                            
                            
                                    | 
                                         danasegarane76 
                                        Posting Yak  Master 
                                         
                                        
                                        242 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2014-01-25 : 06:51:03
                                            
  | 
                                             
                                            
                                            Team I have table structure like Name	Age 	Type	Mark1	Mark2	Mark3	Mark4	Year	Region	Season	Group										Mark	15	Yearly	80	23	86	85	2001	South	Winter	PermanentSteve	16	Quaterly	70	66	56	95	2001	North	Summer	ConsultantPaul	21	Yearly	95	56	59	88	2012	West	Other	ContractPrem	20	Yearly	54	26	68	63	2013	South	Winter	ConsultantRam	20	Monthly	52	23	56	67	2012	North	Other	PermanentJohn	19	Others	65	56	46	97	2013	East	Other	Contractpapa	25	Monthly	54	55	55	48	2003	East	Winter	ConsultantRagul	24	Yearly	85	5	58	84	2006	North	Summer	Consultant And my query is likeSelect name, Age        case when [Type]='Yearly' Then Mark4 as Yearcount,       case when [Type]='Quaterly' Then Mark3 as QCount,       case when [Type] = 'Monthly' Then Mark1 as MonthCountGroup by Region, SeasonUnionSelect name, Age        case when [Type]='Yearly' Then Mark4 as Yearcount,       case when [Type]='Quaterly' Then Mark3 as QCount,       case when [Type] = 'Monthly' Then Mark1 as MonthCountGroup by Season, Group How this query can be optimized.Thanks in Advance. | 
                                             
                                         
                                     | 
                            
       
                            
                       
                          
                            
                                    | 
                                     khtan 
                                    In (Som, Ni, Yak) 
                                     
                                    
                                    17689 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 08:22:31
                                          
  | 
                                         
                                        
                                          Is that the full query ? What is the required result ? KH[spoiler]Time is always against us[/spoiler]  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     danasegarane76 
                                    Posting Yak  Master 
                                     
                                    
                                    242 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 09:36:35
                                          
  | 
                                         
                                        
                                          | Select name,age, sum(Mark4) as YearCount,sum(Mark3) as QuaterCount, sum(Mark1) as MonthCount from (Select name, Age        case when [Type]='Yearly' Then Mark4 as Yearcount,       case when [Type]='Quaterly' Then Mark3 as QCount,       case when [Type] = 'Monthly' Then Mark1 as MonthCountGroup by Region, SeasonUnionSelect name, Age        case when [Type]='Yearly' Then Mark4 as Yearcount,       case when [Type]='Quaterly' Then Mark3 as QCount,       case when [Type] = 'Monthly' Then Mark1 as MonthCountGroup by Season, Group) aIs there any way to change this part case when [Type]='Yearly' Then Mark4 as Yearcount,       case when [Type]='Quaterly' Then Mark3 as QCount,       case when [Type] = 'Monthly' Then Mark1 as MonthCountas sub and call from main query?  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     khtan 
                                    In (Som, Ni, Yak) 
                                     
                                    
                                    17689 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 10:08:58
                                          
  | 
                                         
                                        
                                          Your query will not execute. Maybe you can explain what are you trying to achieve and also post the what is the required result KH[spoiler]Time is always against us[/spoiler]  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     danasegarane76 
                                    Posting Yak  Master 
                                     
                                    
                                    242 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 10:41:41
                                          
  | 
                                         
                                        
                                          Basically I want to convert the rows into columns. As I cannot provide real query and real data, I am posting with some data.The table will be separted by few groups for example here(group 1 : Region, Season and group 2	: Season, Group).Then these grouped values are to summed using common columns.And the result will be something likeName	Age 	YearCount  Qcount   MonthCount										Mark	15	     85Steve	16	                 66Paul	21	    88Prem	20	    63Ram	    20	                          52John	19	    papa	25	                          54Ragul	24	   84 And then I need sum these two result set to get the total sum of Yearcount, Qcount and MonthI hope I explained about the requirement  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     khtan 
                                    In (Som, Ni, Yak) 
                                     
                                    
                                    17689 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 18:38:31
                                          
  | 
                                         
                                        
                                          [code]select Name, Age,       case when [Type] = 'Yearly'   Then Mark4 end as YearCount,       case when [Type] = 'Quaterly' Then Mark3 end as QCount,       case when [Type] = 'Monthly'  Then Mark1 end as MonthCountfrom   yourtable[/code] KH[spoiler]Time is always against us[/spoiler]  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     danasegarane76 
                                    Posting Yak  Master 
                                     
                                    
                                    242 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 18:56:09
                                          
  | 
                                         
                                        
                                          | I need to group the split the main table using few groups and then get the sum by joining them. Or like this with some some other tables namesselect Name, Age,       case when [Type] = 'Yearly'   Then Mark4 end as YearCount,       case when [Type] = 'Quaterly' Then Mark3 end as QCount,       case when [Type] = 'Monthly'  Then Mark1 end as MonthCountfrom   table1Unionselect Name, Age,       case when [Type] = 'Yearly'   Then Mark4 end as YearCount,       case when [Type] = 'Quaterly' Then Mark3 end as QCount,       case when [Type] = 'Monthly'  Then Mark1 end as MonthCountfrom   Table2Joinselect Name, Age,       case when [Type] = 'Yearly'   Then Mark4 end as YearCount,       case when [Type] = 'Quaterly' Then Mark3 end as QCount,       case when [Type] = 'Monthly'  Then Mark1 end as MonthCountfrom   Table3Can it be simplified.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     khtan 
                                    In (Som, Ni, Yak) 
                                     
                                    
                                    17689 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-25 : 20:16:57
                                          
  | 
                                         
                                        
                                          if you have data in 3 tables, you canselect Name, Age,sum(case when [Type] = 'Yearly' Then Mark4 end) as YearCount,sum(case when [Type] = 'Quaterly' Then Mark3 end) as QCount,sum(case when [Type] = 'Monthly' Then Mark1 end) as MonthCountfrom (    select * from table1union all    select * from table2union all    select * from table3) tgroup by Name, Age  KH[spoiler]Time is always against us[/spoiler]  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     danasegarane76 
                                    Posting Yak  Master 
                                     
                                    
                                    242 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-26 : 04:51:36
                                          
  | 
                                         
                                        
                                          | This is great. Thanks. I have another question, If the grouping across the the tables, How can be this done.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     khtan 
                                    In (Som, Ni, Yak) 
                                     
                                    
                                    17689 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-26 : 05:24:08
                                          
  | 
                                         
                                        
                                          yes. group it before union it KH[spoiler]Time is always against us[/spoiler]  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                       
                          
                            
                                    | 
                                     danasegarane76 
                                    Posting Yak  Master 
                                     
                                    
                                    242 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-01-26 : 05:37:06
                                          
  | 
                                         
                                        
                                          | Thanks let me try the original query and will print the results here  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                  
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 |