Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fiddle example(not sure if this will save if I'm not logged in but works now: <a href="http://sqlfiddle.com/#!6/8c96a/3/15" rel="nofollow">http://sqlfiddle.com/#!6/8c96a/3/15</a></p> <p>I only pivoted 3 months, but you get the idea hopefully. Usually you have the nested Select after From, because you only want to pivot/group on certain columns. Then the Pivot occurs, where you list the values that you want to become column names <code>FOR mnth IN ( [1], [2], [3] )</code> which corresponds to the <code>Month(Date) as mnth</code> field.</p> <pre><code>Create table Sales ( EmpId int, Date DateTime, Value integer ); Insert Into Sales (EmpId, Date, Value) Values (1,'1/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'2/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'3/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'4/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'5/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'6/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (1,'7/1/2012', 12); Insert Into Sales (EmpId, Date, Value) Values (2,'1/1/2012', 112); Insert Into Sales (EmpId, Date, Value) Values (2,'1/15/2012', 112); Insert Into Sales (EmpId, Date, Value) Values (2,'2/1/2012', 312); Insert Into Sales (EmpId, Date, Value) Values (2,'3/1/2012', 512); Insert Into Sales (EmpId, Date, Value) Values (2,'4/1/2012', 2); Insert Into Sales (EmpId, Date, Value) Values (2,'5/1/2012', 3); Insert Into Sales (EmpId, Date, Value) Values (2,'6/1/2012', 1); Insert Into Sales (EmpId, Date, Value) Values (2,'7/1/2012', 0); SELECT EmpID, [1], [2], [3] FROM (SELECT EmpID, Month(Date) as mnth, Value FROM Sales) p PIVOT ( Sum (Value) FOR mnth IN ( [1], [2], [3] ) ) AS pvt ORDER BY pvt.EmpID; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload