Hello Everyone,
I have the following query,
/*Parameter Area*/
/*SELECT FROM [dbo].[OSRT] P0*/
declare @StartDate as datetime
/* WHERE */
set @StartDate = /* P0.FromDate */ '[%0]'
/*SELECT FROM [dbo].[OSRT] P1*/
declare @EndDate as datetime
/* WHERE */
set @EndDate = /* P1.ToDate */ '[%1]'
/* SELECT FROM [dbo].[OCRD] P2 */
declare @Vendor as varchar(30)
set @Vendor = /* P2.CardCode */ '[%2]'
/* SELECT FROM [dbo].[OSRN] P4 */
declare @Serial as varchar(30)
set @Serial = /* P4.DistNumber */ '[%4]'
Select Q1.CardCode, Q1.ItemCode, Q1.Dscription, SUM(Q1.LineTotal)
FROM (SELECT P1.CardCode, P1.DocDate, P2.ItemCode, P2.Dscription, P2.U_Unit, P2.U_UnitSer, P2.LineTotal FROM OPCH P1 INNER JOIN PCH1 P2 ON P1.DocEntry=P2.DocEntry) Q1
WHERE (Q1.ItemCode Like 'PROM%' AND Q1.CardCode=@Vendor AND Q1.DocDate BETWEEN @StartDate and @EndDate) OR (Q1.ItemCode Like 'PROM%' AND Q1.U_UnitSer=@Serial AND Q1.DocDate BETWEEN @StartDate and @EndDate)
GROUP BY Q1.CardCode, Q1.ItemCode, Q1.Dscription
Order By Q1.CardCode ASC
FOR BROWSE
Which successfully creates an expense like report for a user defined date range. Now, for reporting purposes, I'm being asked to have the query break down the totals by months rather than one lump sum. I've been thinking about trying to add a pivot to the query, but my problem falls into this: The user can select dates that flow across multiple years and I would still need to break it down by months without accidentally grouping them into the same. It's kind of stumping me as to how best to dynamically assign the columns based on a user input like that.
For additional points: I know they would like to see totals per column (IE: the totals of all the expense items shown for that month), but I'm not even worried about that one right now as I don't think I could really add that to the query in a way that would look 'clean' (IE: the total at the bottom of the column instead of all of them in a column to themselves.)
Thank you very much for your time,