title: T-SQL dynamic pivots
body: <p>I am trying to generate a pivot table with dynamic column names, but failing miserably.</p>
  
  <p>My table has the following structure:</p>
  
  <pre><code>id, int() PKEY
  prod_no, VARCHAR(20)
  f_month, INT
  f_year, INT
  f_value, INT
  </code></pre>
  
  <p>with sample data looking like this</p>
  
  <pre><code>-------------------
  AB1234|1|2016|15698
  -------------------
  AB1234|2|2016|25438
  -------------------
  AB1234|3|2016|53323
  -------------------
  AB1234|1|2017|34535
  -------------------
  AB1234|2|2017|66244
  -------------------
  AB1234|3|2017|54534
  -------------------
  CD9876|1|2016|43278
  -------------------
  CD9876|2|2016|11245
  -------------------
  CD9876|3|2016|82432
  -------------------
  CD9876|1|2017|93563
  -------------------
  CD9876|2|2017|89356
  -------------------
  CD9876|3|2017|45724
  -------------------
  </code></pre>
  
  <p>the result I'm after is something like this:</p>
  
  <pre><code>prod_no|1-2016|2-2016|3-2016|1-2017|2-2017|3-2017|
  --------------------------------------------------
  AB1234 |15698 |25438 |53323 |34535 |66244 |54534 |
  --------------------------------------------------
  CD9876 |43278 |11245 |82432 |93563 |89356 |45724 |
  </code></pre>
  
  <p>So the columns as prod_no, followed by dynamic columns being concatenation of f_month-f_year
  an the data as product number and value corresponding to the month-year in that column.</p>
  
  <p>I played around with some dynamic pivot examples from the web but so far no luck with getting this to work</p>