1

I have a table like this:

CUSTOMER   DATE          QTY
----------------------------
1          2015-10-01    5
1          2015-10-02    4
1          2015-10-03    3
...
2          2015-10-01    9
2          2015-10-02    8
2          2015-10-03    7
...

I want to create a view (or perhaps a function) to return records like this:

CUSTOMER   "2015-10-01"  "2015-10-02"  "2015-10-03"  ...
--------------------------------------------------------
1          5             4             3
2          9             8             7

I need to return "all dates" in table (but will have the last 60 days tops). But if table has only 10 dates, there will be only 10 columns (plus customer id) and so on...

I tried to use crosstab function, but it needs to be informed all the "date column names", that (obviously) can vary depending of table contents.

Christian
  • 7,062
  • 9
  • 53
  • 79
  • This is not possible. The number of columns of a query (and a view is just a "stored" query) must be know _before_ the query is executed. –  Oct 28 '15 at 20:15
  • The question was closed hastily. There is no word on creating views in the indicated answer. See how to dynamically create a view in [this post](http://stackoverflow.com/questions/33322074/how-to-generate-multiple-time-series-in-one-sql-query/33324339#33324339). – klin Oct 28 '15 at 23:20

0 Answers0