Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2005 pivot query syntax assistance
    primarykey
    data
    text
    <p>I've read up on pivot in SQL and looked at tons of examples, but I just can't get this to work the way I'd like. I have a transaction history table that tracks employee purchases.</p> <p>Example:</p> <pre><code>Employee | Date | Value 1234 | 2012-11-13 | 20.23 </code></pre> <p>I need to create a summary for all employees with total sales for each month like this:</p> <pre><code>Employee | Jan | Feb | Mar | ... | Dec| YearTotal </code></pre> <p>I'm confused about grouping and getting the monthly totals. Thanks for any help!</p> <p>EDIT Okay, I apologize if I came across as unprepared to request assistance. I've been working on this most of the day, and was getting frustrated and tired. I've got over 10 years of experience with SQL, however this is the first time I've tried to do a pivot query and I'm just not getting my head around how they are supposed to work, and how to make it work for my needs. I need to produce these numbers for Monday morning, so in about 7 hours. My last ditch effort will be to run a seperate query for each month, then dump the data into excel for the report. I'd like this to be seamless for the end user to run in the future.</p> <p>Here's a simplified version of my attempt that was reworked from a sample from MSDN:</p> <pre><code>SELECT CUSTOMER_NUMBER, [jan], [feb] FROM ( SELECT month(transaction_date) FROM customer_item_purchases WHERE TRANSACTION_DATE BETWEEN '2011-11-16' AND '2012-11-15' ) PIVOT (SUM(SALES_VALUE) IN ([JAN], [Feb])) AS pvt </code></pre> <p>I need total monthly sales for each month between the dates specified. Nov will show up at the beginning and end of the month columns. The source table is simple: Employee, transdate, receipt, location, etc.</p>
    singulars
    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.
 

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