Question : SQL to calculate sales per representative and per week

I have problems putting together a SQL command that calculates sales per representative.
With the command below I can show the total sales, but the result I want is the following:
a grid with on the x-axis the names of the representatives (eg John Paul George Ringo Total) and on the y-axis the dates of the orders.
The grid would be filled from the total sales per week and per representative.
How do I get this done?

Example:

                  Paul             George               Ringo          John            Total
2010-1-1      10                 15                       10             5                   40
2010-1-8       5                   10                        5           30                   50
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
SELECT [Orderdetails].Odt_dtmvDatum, SUM([Orders].Ord_bdrTotaal) as Total
FROM [Orders]
INNER JOIN [Orderdetails]
ON [Orders].Ord_ID = [Orderdetails].Odt_Ord_ID
GROUP BY [Orderdetails].Odt_dtmvDatum
HAVING YEAR([Orderdetails].Odt_dtmvdatum) = 2010
ORDER BY [Orderdetails].Odt_dtmvDatum

Answer : SQL to calculate sales per representative and per week

oops, missed the where clause
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
declare @cols varchar(max) 
declare @strSQL varchar(max) 
 
set @cols = stuff((select distinct '], [' + cast(representativecolumn as varchar) from Orders for xml path('')), 1, 2, '') + ']' 
 
set @strSQL = 'select week, ' + @cols + ' from  
( 
select dateadd(d, 7 - datepart(dw, b.Odt_dtmvdatum), b.Odt_dtmvdatum) as week,  
                a.Representativecolumn,  
                a.Ord_bdrTotaal  
                from Orders a 
                INNER JOIN [Orderdetails] b ON a.Ord_ID = b.Odt_Ord_ID  
                WHERE YEAR(b.Odt_dtmvdatum) = 2010 
) o  
pivot (sum(Ord_bdrTotaal) for Representativecolumn in ( ' + @cols + '))p 
' 
 
exec(@strSQL)
Random Solutions  
 
programming4us programming4us