Thursday, 5 September 2013

Getting Data In to a pivot table

Getting Data In to a pivot table

Well I really tried to use the stuff that I was taught in yesterday's
question and answers from @AaronBertrand to a similar type issue. I am
using in Microsoft SQL Server 2008 R2
I have data that looks like this:
SalesPersonID TransDate Order DateTotal
1108 8/2/2013 231.95 7713.8
1108 8/2/2013 5805.15 7713.8
1108 8/2/2013 1676.70 7713.8
1108 8/3/2013 159.95 3635.35
1108 8/3/2013 468.90 3635.35
1108 8/3/2013 1160.85 3635.35
1108 8/3/2013 209.95 3635.35
1108 8/3/2013 1161.85 3635.35
1108 8/3/2013 473.85 3635.35
1108 8/4/2013 149.98 3151.68
1108 8/4/2013 793.95 3151.68
1108 8/4/2013 55.00 3151.68
1108 8/4/2013 198.95 3151.68
1108 8/4/2013 398.00 3151.68
1108 8/4/2013 1255.85 3151.68
1108 8/4/2013 299.95 3151.68
1108 8/9/2013 223.95 1413.8
1108 8/9/2013 59.95 1413.8
1108 8/9/2013 1129.90 1413.8
1108 8/30/2013 1396.43 1396.43
1108 8/31/2013 89.95 1735.65
1108 8/31/2013 495.95 1735.65
1108 8/31/2013 495.95 1735.65
1108 8/31/2013 633.85 1735.65
1108 8/31/2013 19.95 1735.65
1205 8/3/2013 2389.09 2389.09
And using the answer that I got from an earlier question, [Here], I guess
I don't completely understand the SQL....
SO .....
I am needing the data to come out something like this:
SalesPersonID 1 2 3 4 5 6 7 8 9
…. 29 30 31
1108 0.00 7713.80 3635.35 3151.68 0.00 0.00 0.00 0.00
1413.80 0.00 1396.43 1735.65
1205 0.00 0.00 2389.09 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
The numbers across the top are the days of the selected month, I am using
August, If I selected June, it would only have 30.
I use this code to get my first and last days and dates.
DECLARE @BeginDate AS VARCHAR(10), @EndDate AS VARCHAR(10),
@SelectedMonthDays AS int
SET @BeginDate='08/15/13'
-- Last Day of Current Month
Set @SelectedMonthDays = DATEPART(day, DATEADD(s,-1,DATEADD(mm,
DATEDIFF(m,0,@BeginDate)+1,0)))
-- Last day Date of Current Month
SET @EndDate=cast(convert(date,DATEADD(s,-1,DATEADD(mm,
DATEDIFF(m,0,@BeginDate)+1,0))) as varchar(10))
-- Convert input date to the first day Date of current month
Set @BeginDate = cast(convert(date,DATEADD(s,0,DATEADD(mm,
DATEDIFF(m,0,@BeginDate),0))) as varchar(10))
To get days of the currently selected month, I have this code:
SELECT xhours = number FROM Master..spt_values WHERE type = N'P' and
number between 1 and @SelectedMonthDays ORDER BY number
Thanks for any help that you all can give.

No comments:

Post a Comment