编写复杂查询后,我将列值放入#temp表
// Complex business logic code
...
...
SELECT Distinct
Jurisdiction,
JurisdictionX,
LicenceNumber,
DateIssued,
ExpirationDate,
Resident
FROM #TEMP
现在它的结果非常接近我的实际解决方案,如图所示......
见上图,其中有6列
如果您注意到有两行具有相同的状态“Alabama”。在一行许可证号中,DateIssued和ExpirationDate列为空。在第二行许可证号中,DateIssued和ExpirationDate列具有值。现在我希望在这种情况下它应该考虑许可证号,DateIssued和ExpirationDate列具有值的第二行,它应该消除许可证号,DateIssued和ExpirationDate列为空的另一行。
任何人都可以帮忙写一下查询吗???
答案 0 :(得分:0)
试试这个:
SELECT Distinct Jurisdiction, JurisdictionX, LicenceNumber, DateIssued, ExpirationDate, Resident
FROM #TEMP
WHERE LicenseNumber IS NOT NULL AND
DateIssued IS NOT NULL AND
ExpirationDate IS NOT NULL
UNION
SELECT Distinct Jurisdiction, JurisdictionX, LicenceNumber, DateIssued, ExpirationDate, Resident
FROM #TEMP
WHERE JurisdictionX NOT IN ( SELECT JurisdictionX from #TEMP
WHERE LicenseNumber IS NOT NULL AND
DateIssued IS NOT NULL AND
ExpirationDate IS NOT NULL);