对于基于第一个查询的计数生成动态查询的一些建议,我们将不胜感激。
例如:在757 BTID中,我们找到了最大记录数,因此它应该是第二个查询中的第一个表。 类似地,第二个查询中的706将是2nd,788-3rd,715将是4table。
每个用户的数量都会有所不同
首次查询:
SELECT btid, COUNT(*) FROM dbo.PERSON_EXPANDED
WHERE PID=31226 AND BTID IN (757,706,788,715)
GROUP BY btid
首次查询结果:
757 15
706 14
788 12
715 11
第二个查询:
SELECT *
FROM dbo.PERSON_EXPANDED E757
INNER JOIN dbo.PERSON_EXPANDED E706 ON E757.PID=E706.PID AND E706.BTID=706 AND E757.EffectiveDate BETWEEN E706.EffectiveDate AND E706.ExpirationDate
INNER JOIN dbo.PERSON_EXPANDED S788 ON E757.PID=S788.PID AND S788.BTID=788 AND E757.EffectiveDate BETWEEN S788.EffectiveDate AND S788.ExpirationDate
INNER JOIN dbo.PERSON_EXPANDED S715 ON E757.PID=S715.PID AND S715.BTID=715 AND E757.EffectiveDate BETWEEN S715.EffectiveDate AND S715.ExpirationDate
INNER JOIN dbo.EMPLOYEE E ON P.PID=E757.PID
WHERE E757.BTID=757
AND E.PID=31226
ORDER BY E757.EffectiveDate
谢谢...