Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-10-21 : 11:51:40
|
Hey Guys I hope you are well Aim –To produce a unique line by month for the FDMSAccountNoI have created the following query (see below) which produces the following result set FDMSAccountNo AvgVal Card_Type Pence_Percent fee_wholesale_date BegMonthDate878000001882 0.012900000 Credit Percent 2009-07-08 2009-07-01 00:00:00.000878000001882 0.015700000 Credit Percent 2011-02-24 2011-02-01 00:00:00.000878000001882 0.014700000 Credit Percent 2011-02-25 2011-02-01 00:00:00.000SELECT [FDMSAccountNo],AVG([fee_retail_amt]*1.0) AS AvgVal,Card_Type,[Pence_Percent],[fee_wholesale_date],DATEADD(mm,DATEDIFF(mm,0,[fee_wholesale_date]),0) AS BegMonthDateFROM(SELECT [FDMSAccountNo],[fee_sequence],[fee_retail_amt],[fee_wholesale_date],Dim_Fee_Codes.Card_Type,[Pence_Percent],ROW_NUMBER() OVER (PARTITION BY FDMSaccountno,fee_sequence ORDER BY Fee_Wholesale_date DESC) AS SeqFROM [FDMS].[dbo].[Audit_FDMS_Billing_Fees_Hist]INNER JOIN Dim_Fee_Codes ON Audit_FDMS_Billing_Fees_Hist.fee_sequence = Dim_Fee_Codes.Fee_Codewhere fee_retail_amt <> '0.00000000'and Card_Type <> 'N/A'and Card_Type = 'Credit'and Pence_Percent = 'Percent'and FDMSAccountNo = '878000001882')tWHERE Seq=1GROUP BY [FDMSAccountNo],[fee_wholesale_date],Card_Type,[Pence_Percent]Desired results FDMSAccountNo AvgVal Card_Type Pence_Percent fee_wholesale_date 878000001882 0.012900000 Credit Percent 2009-07-08878000001882 0.012900000 Credit Percent 2009-08-08878000001882 0.012900000 Credit Percent 2009-09-08878000001882 0.012900000 Credit Percent 2009-10-08878000001882 0.012900000 Credit Percent 2009-11-08878000001882 0.012900000 Credit Percent 2009-12-08878000001882 0.012900000 Credit Percent 2010-01-08878000001882 0.012900000 Credit Percent 2010-02-08Etc until the new fee_wholesale_date is found 878000001882 0.015700000 Credit Percent 2011-02-24878000001882 0.014700000 Credit Percent 2011-02-25 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 14:50:51
|
[code];With CTEAS(SELECT ROW_NUMBER() OVER (PARTITION BY FDMSAccountNo ORDER BY fee_wholesale_date) AS Seq,*FROM YourTable)SELECT c1.FDMSAccountNo,c1.AvgVal,c1.Card_Type,c1.Pence_Percent,DATEADD(mm,v.number,c1.fee_wholesale_date) AS fee_wholesale_dateFROM CTE c1INNER JOIN CTE c2ON c1.FDMSAccountNo = c2.FDMSAccountNo AND c1.Seq = c2.Seq -1CROSS JOIN master..spt_values vWHERE v.type = 'p'AND DATEADD(mm,v.number,c1.fee_wholesale_date) < c2.fee_wholesale_date[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|