我正试图带一个人,并显示他们目前的保险以及他们以前的保险.我想有人可以说我试图激发我对客户或人的看法.由于我的左连接子查询中存在多个记录,我遇到了一个问题,我正在收到多条记录.我希望通过在子查询中添加"TOP 1"来解决这个问题,但实际上并没有返回任何内容......
有任何想法吗?
SELECT p.person_id AS 'MIRID' , p.firstname AS 'FIRST' , p.lastname AS 'LAST' , pg.name AS 'GROUP' , e.name AS 'AOR' , p.leaddate AS 'CONTACT DATE' , [dbo].[GetPICampaignDisp](p.person_id, '2009') AS 'PI - 2009' , [dbo].[GetPICampaignDisp](p.person_id, '2008') AS 'PI - 2008' , [dbo].[GetPICampaignDisp](p.person_id, '2007') AS 'PI - 2007' , a_disp.name AS 'CURR DISP' , a_ins.name AS 'CURR INS' , a_prodtype.name AS 'CURR INS TYPE' , a_t.date AS 'CURR INS APP DATE' , a_t.effdate AS 'CURR INS EFF DATE' , b_disp.name AS 'PREV DISP' , b_ins.name AS 'PREV INS' , b_prodtype.name AS 'PREV INS TYPE' , b_t.date AS 'PREV INS APP DATE' , b_t.effdate AS 'PREV INS EFF DATE' , b_t.termdate AS 'PREV INS TERM DATE' FROM [person] p LEFT OUTER JOIN [employee] e ON e.employee_id = p.agentofrecord_id INNER JOIN [dbo].[person_physician] pp ON p.person_id = pp.person_id INNER JOIN [dbo].[physician] ph ON ph.physician_id = pp.physician_id INNER JOIN [dbo].[clinic] c ON c.clinic_id = ph.clinic_id INNER JOIN [dbo].[d_Physgroup] pg ON pg.d_physgroup_id = c.physgroup_id LEFT OUTER JOIN ( SELECT tr1.* FROM [transaction] tr1 LEFT OUTER JOIN [d_vendor] ins1 ON ins1.d_vendor_id = tr1.d_vendor_id LEFT OUTER JOIN [d_product_type] prodtype1 ON prodtype1.d_product_type_id = tr1.d_product_type_id LEFT OUTER JOIN [d_commission_type] ctype1 ON ctype1.d_commission_type_id = tr1.d_commission_type_id WHERE prodtype1.name <> 'Medicare Part D' AND tr1.termdate IS NULL ) AS a_t ON a_t.person_id = p.person_id LEFT OUTER JOIN [d_vendor] a_ins ON a_ins.d_vendor_id = a_t.d_vendor_id LEFT OUTER JOIN [d_product_type] a_prodtype ON a_prodtype.d_product_type_id = a_t.d_product_type_id LEFT OUTER JOIN [d_commission_type] a_ctype ON a_ctype.d_commission_type_id = a_t.d_commission_type_id LEFT OUTER JOIN [d_disposition] a_disp ON a_disp.d_disposition_id = a_t.d_disposition_id LEFT OUTER JOIN ( SELECT tr2.* FROM [transaction] tr2 LEFT OUTER JOIN [d_vendor] ins2 ON ins2.d_vendor_id = tr2.d_vendor_id LEFT OUTER JOIN [d_product_type] prodtype2 ON prodtype2.d_product_type_id = tr2.d_product_type_id LEFT OUTER JOIN [d_commission_type] ctype2 ON ctype2.d_commission_type_id = tr2.d_commission_type_id WHERE prodtype2.name <> 'Medicare Part D' AND tr2.termdate IS NOT NULL ) AS b_t ON b_t.person_id = p.person_id LEFT OUTER JOIN [d_vendor] b_ins ON b_ins.d_vendor_id = b_t.d_vendor_id LEFT OUTER JOIN [d_product_type] b_prodtype ON b_prodtype.d_product_type_id = b_t.d_product_type_id LEFT OUTER JOIN [d_commission_type] b_ctype ON b_ctype.d_commission_type_id = b_t.d_commission_type_id LEFT OUTER JOIN [d_disposition] b_disp ON b_disp.d_disposition_id = b_t.d_disposition_id WHERE pg.d_physgroup_id = @PhysGroupID
Robert Jeppe.. 16
在Sql server 2005中,您可以使用OUTER APPLY
SELECT p.person_id, s.e.employee_id FROM person p OUTER APPLY (SELECT TOP 1 * FROM Employee WHERE /*JOINCONDITION*/ ORDER BY /*Something*/ DESC) s
http://technet.microsoft.com/en-us/library/ms175156.aspx
在Sql server 2005中,您可以使用OUTER APPLY
SELECT p.person_id, s.e.employee_id FROM person p OUTER APPLY (SELECT TOP 1 * FROM Employee WHERE /*JOINCONDITION*/ ORDER BY /*Something*/ DESC) s
http://technet.microsoft.com/en-us/library/ms175156.aspx
我通常用于此的模式是:
选择任何
人员
LEFT JOIN子表AS s1
ON s1.personid = person.personid...
什么不存在
(从子表中选择1
WHERE personid = person.personid
AND orderbydate> s1.orderbydate)
这避免了TOP 1条款,也许使它更清晰一点.
顺便说一句,我喜欢你把这个查询放在一起的方式,除非我省略括号,假设你已经合理地命名了表和列; 你甚至可以通过列出tr1和tr2的列而不是"tr1.*"和"tr2.*"获得一些性能(但至少是优雅).