SQL别名/加入难题

时间:2016-01-11 17:43:49

标签: sql sql-server-2008 join

互联网上的公平人士!

如果我愿意,可以快速提问董事会的亲切天才......

我正在尝试'过滤'(因为缺少一个更好的术语)会产生数据集,而这实际上是我们拥有该组的ID然后是集体ID的情况。可能更容易说“租户参考”和“租赁参考”,因为这就是我正在使用的!

我已经能够得到我所追求的东西,虽然无疑是一种无礼的方式,而且......不是我需要的,因为有重复。

由于我正在使用的数据充满机密性,我无法发布任何数据,但这是我的查询:

SELECT DISTINCT

Pr.Ref             AS PPROPERTYID,
Ty.GlobalReference AS PPREVREF,
Ty.Tenancy_Id      AS TenancyId,
Tt1.Tenant_Id       AS TenantId,
Tt1.Title           AS Title,
Tt1.Forename + ' ' +

CASE
  WHEN Tt1.Middlename IS NULL
  THEN ''
  ELSE Tt1.Middlename
 END               AS FirstName,

Tt1.Surname         AS Surname,
Tt1.LandPhone       AS HomePhone,
Tt1.Mobile          AS Mobile,
Tt1.EMail           AS Email,
Tt2.Title           AS Title2,
Tt2.Forename + ' ' +

CASE
  WHEN Tt2.Middlename IS NULL
  THEN ''
  ELSE Tt2.Middlename
 END               AS FirstName2,

Tt2.Surname         AS Surname2,
Tt3.Title           AS Title3,
Tt3.Forename + ' ' +

CASE
  WHEN Tt3.Middlename IS NULL
  THEN ''
  ELSE Tt3.Middlename
 END               AS FirstName3,

Tt3.Surname         AS Surname3,
Tt4.Title           AS Title4,
Tt4.Forename + ' ' +

CASE
  WHEN Tt4.Middlename IS NULL
  THEN ''
  ELSE Tt4.Middlename
 END               AS FirstName4,

Tt4.Surname         AS Surname4,
Tt5.Title           AS Title5,
Tt5.Forename + ' ' +

CASE
  WHEN Tt5.Middlename IS NULL
  THEN ''
  ELSE Tt5.Middlename
 END               AS FirstName5,

Tt5.Surname         AS Surname5,
Tt6.Title           AS Title6,
Tt6.Forename + ' ' +

CASE
  WHEN Tt6.Middlename IS NULL
  THEN ''
  ELSE Tt6.Middlename
 END               AS FirstName6,

Tt6.Surname         AS Surname6,


'END1234'          AS 'END1234'

FROM Tenancy Ty

LEFT JOIN Tenancies Tys    ON Ty.Tenancies_Id  = Tys.Tenancies_Id
LEFT JOIN Property Pr      ON Tys.Property_Id  = Pr.Property_Id
LEFT JOIN Landlords LLs    ON Pr.Property_Id   = LLs.Property_Id
LEFT JOIN LandlordJoin LLJ ON LLs.Landlords_Id = LLJ.Landlords_Id1
LEFT JOIN Tenants Tts      ON Ty.Tenancy_Id    = Tts.Tenancy_Id
LEFT JOIN Tenant Tt        ON Tts.Tenants_Id   = Tt.Tenants_Id
LEFT JOIN Conditions Con   ON Ty.Tenancy_Id    = Con.Tenancy_Id
LEFT JOIN Tenant Tt1       ON Tts.Tenants_Id = Tt1.Tenants_Id 
LEFT JOIN Tenant Tt2       ON Tts.Tenants_Id = Tt2.Tenants_Id AND Tt2.Tenant_Id <> Tt1.Tenant_Id
LEFT JOIN Tenant Tt3       ON Tts.Tenants_Id = Tt3.Tenants_Id AND Tt3.Tenant_Id <> Tt2.Tenant_Id AND Tt3.Tenant_Id <> Tt1.Tenant_Id
LEFT JOIN Tenant Tt4       ON Tts.Tenants_Id = Tt4.Tenants_Id AND Tt4.Tenant_Id <> Tt3.Tenant_Id AND Tt4.Tenant_Id <> Tt2.Tenant_Id 
                                                          AND Tt4.Tenant_Id <> Tt1.Tenant_Id
LEFT JOIN Tenant Tt5       ON Tts.Tenants_Id = Tt5.Tenants_Id AND Tt5.Tenant_Id <> Tt4.Tenant_Id AND Tt5.Tenant_Id <> Tt3.Tenant_Id 
                                                          AND Tt5.Tenant_Id <> Tt2.Tenant_Id AND Tt5.Tenant_Id <> Tt1.Tenant_Id
LEFT JOIN Tenant Tt6       ON Tts.Tenants_Id = Tt6.Tenants_Id AND Tt6.Tenant_Id <> Tt5.Tenant_Id AND Tt6.Tenant_Id <> Tt4.Tenant_Id 
                                                          AND Tt6.Tenant_Id <> Tt3.Tenant_Id AND Tt6.Tenant_Id <> Tt2.Tenant_Id 
                                                          AND Tt6.Tenant_Id <> Tt1.Tenant_Id

请记住,这是一个“工作副本”,所以为加入大灾难的丑陋和纯粹的混乱道歉!

有没有人知道为什么结果可能会转换价值(多个Tenant_Id链接到一个Tenancy_Id)?查询似乎确实返回了正确数量的租户,但是正在复制行...而我希望有一个漂亮,整洁的单行,其中租户值整齐地显示在相应的列中。

提前谢谢大家 - 如果由于摄入咖啡因导致的咖啡因摄入量过高而导致过度摄入量过低而道歉,并且与单个人的摄取量相比更为一致!

罗布

0 个答案:

没有答案