Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL to split a value across multiple lines Expand / Collapse
Author
Message
Posted Thursday, November 01, 2012 3:24 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 13, 2013 7:33 AM
Points: 234, Visits: 441
Hi Guys,

I am currently generating a results set as follows;


Policy Claim Job Excess
A 1 E 50.00
A 1 F 0.00
A 2 X 50.00
A 2 Y 0.00
A 2 Z 0.00


I need to split the excess evenly over the lines of the claims, so for claim 1 = 25.00 on each line. on claim two it would need to be 16.66,16.67,16.67 (I don't care which line gets the 16.66)

If I didn't need to deal with rounding I could use sum(excess)/count(job) in a CTE and then join the CTE back to original data set.

I am thinking I might just use a Temp Table, the dataset is only 750K rows long but wanted to know if anyone had a code snippet that might do it as part of the query.

Post #1379684
Posted Thursday, November 01, 2012 3:46 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 1:30 AM
Points: 803, Visits: 2,124
Using the Floor/Ceiling with an accounting fudge (*100)/100.00 get it to work.

(
Policy char(1)
,Claim Int
,Job Char(1)
,Excess Decimal (19,2)
)

INSERT into #PolClaim
Values ('A',1,'E',50.00)
,('A',1,'F',50.00)
,('A',2,'X',50.00)
,('A',2,'Y',50.00)
,('A',2,'Z',50.00)


;
With PolicyClaimLines
AS
(
Select Policy,Claim,count(*) LineCount
From #PolClaim
Group by Policy,Claim
)
Select
Pol.Policy
,Pol.Claim
,Job
,Excess
,Excess/LineCount
,Row_Number() OVER (Partition By Pol.Policy,Pol.Claim ORDER by Job)
,CASE Row_Number() OVER (Partition By Pol.Policy,Pol.Claim ORDER by Job)
WHEN 1 then FLOOR((Excess/LineCount)*100)/100.00
ELSE CEILING((Excess/LineCount)*100)/100.00
END
From
#PolClaim Pol
JOIN PolicyClaimLines PCL
ON PCL.Policy=Pol.Policy
AND PCL.Claim=Pol.Claim



_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1379699
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse