For reporting purcposes, I need to Pivot results of a query that is unique on each record. My current statement is:
SELECT *
FROM Sales AS x
WHERE (select count(*) from Sales where customer_name=x.customer_name
and order_date>=x.order_date)<=5
ORDER BY customer_name, order_date, price;
A sample of the Query output is:
customer_name order_date price
Company A 2009-02-01 800
Company A 2009-03-01 100
Company A 2009-04-01 200
Company A 2009-05-01 300
Company A 2009-06-01 500
Company B 2009-02-01 100
Company B 2009-02-01 800
Company B 2009-04-01 200
Company B 2009-05-01 300
Company B 2009-06-01 500
Ultimately, the information needs to look like this:
Customer_Name order_date1 price1 order_date2 price2 order_date3 price3 order_date4 price4 order_date5 price5
Company A 2009-02-01 800 2009-03-01 100 2009-04-01 200 2009-05-01 300 2009-06-01 500
Company B 2009-02-01 100 2009-02-01 800 2009-04-01 200 2009-05-01 300 2009-06-01 500
I'm thinking that I need to add a column for "pivot_id" so that there is a common record for each grouping so the query result before the pivot will look like:
pivot_id customer_name order_date price
1 Company A 2009-02-01 800
2 Company A 2009-03-01 100
3 Company A 2009-04-01 200
4 Company A 2009-05-01 300
5 Company A 2009-06-01 500
1 Company B 2009-02-01 100
2 Company B 2009-02-01 800
3 Company B 2009-04-01 200
4 Company B 2009-05-01 300
5 Company B 2009-06-01 500
What SQL statment will generate an automatic record number for each purchase, but also start over with each customer_name??
Working in Access 2007 with SQL code builder. Would be great if solution is compatible with Access.
Sorry for the length. Thanks in advance.