I have two tables:
+-----------+
| Customer |
+-----------+
| ID | Name |
+----+------+
| 1 | Jack |
+----+------+
| 2 | John |
+----+------+
+----------------------------------------+
| Bill |
+----------------------------------------+
| ID | Customer_ID | date | amount |
+----+-------------+------------+--------+
| 1 | 1 | 01.01.2015 | 10$ |
+----+-------------+------------+--------+
| 2 | 1 | 01.01.2014 | 20$ |
+----+-------------+------------+--------+
| 3 | 2 | 01.01.2015 | 5$ |
+----+-------------+------------+--------+
| 4 | 2 | 01.02.2015 | 50$ |
+----+-------------+------------+--------+
| 5 | 2 | 01.01.2014 | 15$ |
+----+-------------+------------+--------+
I need to know the sum of all the bills a customer got in a year.
That's pretty easy:
SELECT
SUM(Bill.amount), Customer.Name
FROM
Customer
INNER JOIN
Bill ON Customer.ID = Bill.Customer_ID
WHERE
Bill.date BETWEEN #20150101# AND #20151231#
GROUP BY
Customer.Name
The difficult part is that i need to display the results of that query for multiple years in a single table like this:
+-------------------------------------------+
| sales to customer |
+-------------------------------------------+
| Customer_ID | Customer_Name | 2015 | 2014 |
+-------------+---------------+------+------+
| 1 | jack | 10$ | 20$ |
+-------------+---------------+------+------+
| 2 | john | 55$ | 20$ |
+-------------+---------------+------+------+
I'm using SQL Server 2005.
I'm very grateful for every answer.
sincerly Andahari