| 
                
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 |  
                                    | Martin.BergiusStarting Member
 
 
                                        3 Posts | 
                                            
                                            |  Posted - 2014-08-21 : 02:56:36 
 |  
                                            | Hi,I'm trying to get customerno,salestotal and profittotal from each customer from same table in same query.I have no problem to get the values one by one like this(in two different queries):Query 1:SELECT AcTr.R4, SUM(AcAm*-1)FROM AcTrWHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='3999')GROUP BY AcTr.R4Result:CustId  SalesTotal(SUM(AcAm*-1)CustA   100.000CustB   200.000CustC   100.000Query 2:SELECT AcTr.R4, SUM(AcAm*-1)FROM AcTrWHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='8999')GROUP BY AcTr.R4Result:CustId  ProfitTotal(SUM(AcAm*-1)CustA   10.000CustB   20.000CustC   10.000As you can see the only difference is the accounts in the where clause.Is it possible to have everything in one query and get a result like this:Customer    SalesTotal   ProfitTotalCustA       x            y CustB       x            yCustC       x            yThanks in advance/Martin |  |  
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2014-08-21 : 03:03:04 
 |  
                                          | [code]SELECT AcTr.R4	, SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END) as SalesTotal	, SUM(AcAm*-1) asa ProfitTotalFROM AcTrWHERE (AcTr.AcYr='2013') AND (AcTr.AcPr='11') AND (AcTr.AcNo>='3000' And AcTr.AcNo<='8999')GROUP BY AcTr.R4[/code]sabinWeb MCP |  
                                          |  |  |  
                                    | Martin.BergiusStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2014-08-21 : 04:37:16 
 |  
                                          | ok, thanks.And if i want to include another table with conditions?I want to include customername ( Actor.Nm ) and want to get only the customers that are registrered/created 2013.(Actor.CreDt>='20130101') Tried with: SELECT AcTr.R4, Actor.Nm, SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END) , SUM(AcAm*-1) FROM F0001.dbo.AcTr AcTr, F0001.dbo.Actor ActorWHERE (Actor.CustNo = AcTr.R4) AND (Actor.CreDt>='20140101') AND (AcTr.AcYr='2013') AND (AcTr.AcNo>='3000' AND AcTr.AcNo<='8999')GROUP BY AcTr.R4But getting error msg: ( Wrong columnexpression('SUM(CASE WHEN AcTr.AcNo<='3999' THEN AcAm*-1 ELSE 0 END)' |  
                                          |  |  |  
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2014-08-21 : 04:43:04 
 |  
                                          | [code]SELECT AcTr.R4,Actor.Nm,SUM(CASE WHEN AcTr.AcNo <= '3999' THEN AcTr.AcAm*-1 ELSE 0 END) ,SUM(AcTr.AcAm*-1) FROM F0001.dbo.AcTr AcTr, F0001.dbo.Actor ActorWHERE (Actor.CustNo = AcTr.R4) AND (Actor.CreDt>='20140101') AND (AcTr.AcYr='2013') AND (AcTr.AcNo>='3000' AND AcTr.AcNo<='8999')GROUP BY AcTr.R4 ,Actor.Nm[/code]What is the relation between AcTr and Actor ? the join field ?Can you post some sample data ? for testing ...sabinWeb MCP |  
                                          |  |  |  
                                    | Martin.BergiusStarting Member
 
 
                                    3 Posts | 
                                        
                                          |  Posted - 2014-08-21 : 05:19:28 
 |  
                                          | Many Thanks. Actor.Nm in Group was the solution ;-) |  
                                          |  |  |  
                                    | stepsonAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2014-08-21 : 07:17:06 
 |  
                                          | Your welcome!sabinWeb MCP |  
                                          |  |  |  
                                |  |  |  |  |  |