Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

81 Posts

Posted - 03/26/2014 :  16:36:17  Show Profile  Reply with Quote
Hi, I have a sql 2008/r2 view that shows me always (vertically) the last 12 months of sales including the actual month,I need to sum the sold quantity of every item taking care of the customer,the year and the month and showing all this on columns mode(201403),(201402)...,by exemple : must shows per customer/item/and date :

debcode|debtor |year |month|itemcode|description |sum(sold) per month|
100 debtor1 2014 03 item1 item 1 demo 25
100 debtor1 2014 03 item2 item 2 demo 10
100 debotr1 2014 02 item1 item 3 demo 5
100 debotr1 2013 02 item2 item 1 demo 15

Here is my code, i hope you can help me, please
SELECT     TOP (100) PERCENT Year, Month, debcode, Debtor, Currency, SalesRep, ItemCode, ItemDescription, SUM(Quantity) AS Sold, InvoiceDate
FROM         dbo.View_VK_ORDERS_SUMARY
WHERE     (Sale_Price <> 0.01) AND (InvoiceDate >= DATEADD(MM, - 12, GETDATE()))
GROUP BY ItemCode, ItemDescription, Month, Year, debcode, Debtor, Currency, SalesRep, InvoiceDate
ORDER BY Year, Month, debcode, ItemCode

Edited by - mjimenezh on 03/26/2014 17:16:18

Yak Posting Veteran

81 Posts

Posted - 03/27/2014 :  16:00:50  Show Profile  Reply with Quote
Sorry, this is my code :

SELECT     [DebCode],[Debtor],ItemCode, ItemDescription,[Year],[1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12]
FROM       (SELECT DebCode,Debtor,ItemCode,ItemDescription,Year,Month,Sold
                   FROM View_VK_ORDERS_SUMARY_F) AS PivotData
                 (SUM(Sold) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12])) AS PIVOTING
Go to Top of Page

Yak Posting Veteran

81 Posts

Posted - 03/27/2014 :  20:34:41  Show Profile  Reply with Quote
Someboody to help me? I need columns with dynamic headers [201403],[201402],[201401],[201312]...

my code gives me one fully year from the current month so the next month all the header must be change.

My actual data looks like this :

100 deb100 item1 2013 1 0 3 5 2 0
100 deb100 item1 2014 0 5 5
101 deb101 item1 2013 3 3 9 0 0 2
101 deb101 item2 2014 2 2 6

must looks like this :

100 deb100 item1
101 deb101 item2

Thanks for your help.
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 03/28/2014 :  05:20:51  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
See if this helps


Failing to plan is Planning to fail
Go to Top of Page

Yak Posting Veteran

81 Posts

Posted - 03/28/2014 :  10:42:12  Show Profile  Reply with Quote
Thanks madhivanan but it's too advanced for me, I'm trying to learn sql and specially I'm trying to understand Pivot, this is turning me crazy.

Thanks again body..
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000