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 |
|
NickC
Yak Posting Veteran
68 Posts |
Posted - 2010-06-01 : 07:11:41
|
| Hi I am trying to produce an MI Report but struggling to understand what I'm doing. I'm triyng to pull in number of policies , number of customers and thte totasl premium.I have this so farselect segment, count(distinct policy), count(distinct customername), sum(premium) from (select i.insurance_ref as Policy, m.description as segment, Sum(ral.premium_value) as Premium, p.name as customernamefrom insurance_file iinner join insurance_file_risk_link rlon i.insurance_file_cnt = rl.insurance_file_cntinner join risk ron rl.risk_cnt = r.risk_cntinner join ri_arrangement rion r.risk_cnt = ri.risk_cntinner join ri_model mon ri.ri_model_id = m.ri_model_Idinner join ri_arrangement_line ralon ri.ri_arrangement_id = ral.ri_arrangement_idinner join party pon i.lead_agent_cnt = p.party_cntinner join party_type pton p.party_type_id = pt.party_type_idinner join gis_policy_link plon r.risk_cnt = pl.risk_idinner join plusone_policy_binder pbon pl.gis_policy_link_id = pb.plusone_policy_binder_idinner join plusone_general g on pb.plusone_policy_binder_id = g.plusone_policy_binder_idinner join policy_status pson i.policy_status_id = ps.policy_status_idwhere ps.description = 'Current' and i.insurance_ref not in (select insurance_ref from MI_Excluded_Policies)group by m.description, i.cover_start_date, i.insurance_ref, p.name)as tableddgroup by segment --where ps.descripion = 'Current' and i.insurance_ref not in (select insurance_ref from MI_Excluded_Policies)but its producing crazy numbers and not sure why...the multiple joins are for other criteria I need to add on once I've got past this point, and then the where comment is to limit fields once Ive got where I need to.Any help would be fantastic |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-01 : 07:45:44
|
If you haveFROM Customer JOIN DevlieryAddresses ON X = Y and the Customer table contains a value for "premium" then SELECT SUM(Premium)will add the value for premium for EACH matching row in DevlieryAddressesso that may be your problem.What happens if there are two policy_status records for a given insurance_file?and i.insurance_ref not in (select insurance_ref from MI_Excluded_Policies) the "IN" test here will be extremely slow if there are large numbers in the list - I would define "large" as being thousands, or possibly only hundreds.NOT EXISTS or an OUTER JOIN with a WHERE clause statement excluding PK values from that table which are NULL would perform better. |
 |
|
|
|
|
|
|
|