• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

help with pivoting tabular dataset and prorating quantities by month

I have a stored procedure that prorates quantities based on either a contract start date, contract end date, or an order requested delivery date.

A sales contract is also referred to as a "blanket" type
A regular sales order is a "standard" type

Sales Contracts use Contract Start and End date to do prorate calculation
Regular Sales Orders use the Requested Delivery Date to do prorate calculation

Right now the results are in tabular format all quantities summed up

Bu I need a way to figure out how to split the pro-ration calculation and the quantities by month.

I am including a copy of my t-sql and also a copy of some sample data I am using to test.

I hope that by looking at these files, what I am trying to accomplish becomes clearer than my description of my problem.

I hope also, someone can have the time to offer a hand on working on this, because I am clueless how to do this type of pivot, with calculation.

All the formulas are included, any question, please shoot.

Thank you much.
stored-procedure-tabular.txt
salesdemand-proratedforecast.xlsx
0
metropia
Asked:
metropia
  • 3
  • 2
1 Solution
 
David ToddSenior DBACommented:
Hi,

Without looking too long at your code, there are a couple of dates there. Which date do you want to group by?

Anyway, I find it easy to group by months using something like this:

select getdate(), dateadd( month, datediff( month, 0, getdate()), 0 )

That is, its a development of the common way to remove the time from a datetime, or in other words, group by day. This works for most things other than week and second.

HTH
  David
0
 
metropiaAuthor Commented:
There is also another column I did not include at the time I uploaded the file, that is ItemNumber, I think I can group by that column.

I was not considering to group by any of the date fields, because regular sales orders use request delivery date,  and sales contracts used contract start and contract end date, and I do not understand how to group by using all these 3 cols.

Thank you
0
 
David ToddSenior DBACommented:
Hi,

The bottom line for all those dates - there is ONE date which is when the transaction is realised by the accounting system. That is, if you put an order in now for delivery in a week, then often the order date is now and the accounting system shows the debt against the debtors account as of now. That is the date to use I think.

What I showed above will get grouping by month. I don't think that grouping by item number is meaningful.

HTH
  David
0
 
metropiaAuthor Commented:
Hi David,

What do you mean by what you showed above? perhaps I am missing something from your post?

Thank you much.
0
 
David ToddSenior DBACommented:
Hi,

Have you run this piece of code?

select getdate(), dateadd( month, datediff( month, 0, getdate()), 0 )

Open in new window


Regards
  David
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now